From 6350610ef5f7d73680853d39898094f2bf15febb Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 16 Mar 2021 11:19:53 +0100 Subject: feat: make regex of url to check if it is valid Currently it works only with GitHub --- src/helpers.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/helpers.rs') diff --git a/src/helpers.rs b/src/helpers.rs index d915a50..2dbacfd 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,3 +1,4 @@ +use regex::Regex; use uuid::Uuid; /// Returns a valid Uuid if `id` is not a valid Uuid @@ -7,3 +8,19 @@ pub fn uuid_from_string(id: &String) -> Uuid { Err(_) => Uuid::parse_str("00000000000000000000000000000000").unwrap(), }; } + +/// Check if a path is into the "valid git repositories" and returns the name +pub fn name_of_git_repository(url: &String) -> Option { + const GITHUB_RE: &str = r"^(http(s)?://)?(www.)?github.com/(?P[a-zA-Z0-9-]+)/(?P[a-zA-Z0-9-]+)"; + let re = Regex::new(GITHUB_RE).unwrap(); + + if !re.is_match(&url) { + return None; + } + + let captures = re.captures(&url).unwrap(); + let name = captures.name("username").unwrap().as_str(); + let repo = captures.name("repository").unwrap().as_str(); + + Some(format!("{}/{}", name, repo)) +} -- cgit v1.2.3-18-g5258