diff options
author | Santo Cariotti <santo@dcariotti.me> | 2021-03-16 11:22:42 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-16 11:22:42 +0100 |
commit | b950072a3109d2c13881611a3950baa191caf097 (patch) | |
tree | ccfc5c2c26c56a496d0f34b3f4db0965c713e7bb /src/helpers.rs | |
parent | 48a9ac895b6e8b01622810ec4bf2f3a423426ca3 (diff) | |
parent | 6350610ef5f7d73680853d39898094f2bf15febb (diff) |
Merge pull request #11 from gico-net/feat/add-repositories
Add CRUD for repository
Diffstat (limited to 'src/helpers.rs')
-rw-r--r-- | src/helpers.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/helpers.rs b/src/helpers.rs new file mode 100644 index 0000000..2dbacfd --- /dev/null +++ b/src/helpers.rs @@ -0,0 +1,26 @@ +use regex::Regex; +use uuid::Uuid; + +/// Returns a valid Uuid if `id` is not a valid Uuid +pub fn uuid_from_string(id: &String) -> Uuid { + return match Uuid::parse_str(&id) { + Ok(x) => x, + 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<String> { + const GITHUB_RE: &str = r"^(http(s)?://)?(www.)?github.com/(?P<username>[a-zA-Z0-9-]+)/(?P<repository>[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)) +} |