From fa2e06fccfd0a3e5bbb3c6bfe8f4078f01e348f3 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Mon, 29 Apr 2019 20:37:53 +0200 Subject: [PATCH] Add Service trait to remove code duplication --- src/service.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/service.rs diff --git a/src/service.rs b/src/service.rs new file mode 100644 index 0000000..f234ff3 --- /dev/null +++ b/src/service.rs @@ -0,0 +1,47 @@ +pub(crate) trait Service { + fn domain() -> &'static str; + fn url_path() -> &'static str; + fn commit_url(repo: &str, commit_ref: &str) -> String; +} + +pub(crate) struct GitHub; + +impl Service for GitHub { + fn domain() -> &'static str { + "github.com" + } + fn url_path() -> &'static str { + "github" + } + fn commit_url(repo: &str, commit_ref: &str) -> String { + format!("https://{}/{}/commit/{}", Self::domain(), repo, commit_ref) + } +} + +pub(crate) struct Gitlab; + +impl Service for Gitlab { + fn domain() -> &'static str { + "gitlab.com" + } + fn url_path() -> &'static str { + "gitlab" + } + fn commit_url(repo: &str, commit_ref: &str) -> String { + format!("https://{}/{}/commit/{}", Self::domain(), repo, commit_ref) + } +} + +pub(crate) struct Bitbucket; + +impl Service for Bitbucket { + fn domain() -> &'static str { + "bitbucket.org" + } + fn url_path() -> &'static str { + "bitbucket" + } + fn commit_url(repo: &str, commit_ref: &str) -> String { + format!("https://{}/{}/commits/{}", Self::domain(), repo, commit_ref) + } +}