git_digger/
lib.rs

1use regex::Regex;
2use once_cell::sync::Lazy;
3
4const URL_REGEXES: [&str; 2] = [
5    "^https://(github).com/([^/]+)/([^/]+)/?.*$",
6    "^https://(gitlab).com/([^/]+)/([^/]+)/?.*$",
7];
8
9
10pub fn get_owner_and_repo(repository: &str) -> (String, String, String) {
11    static REGS: Lazy<Vec<Regex>> = Lazy::new(|| {
12        URL_REGEXES
13            .iter()
14            .map(|reg| Regex::new(reg).unwrap())
15            .collect::<Vec<Regex>>()
16    });
17
18    for re in REGS.iter() {
19        if let Some(repo_url) = re.captures(repository) {
20            let host = repo_url[1].to_lowercase();
21            let owner = repo_url[2].to_lowercase();
22            let repo = repo_url[3].to_lowercase();
23            return (host, owner, repo);
24        }
25    }
26
27    log::warn!("No match for repo in '{}'", &repository);
28    (String::new(), String::new(), String::new())
29}
30
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn test_get_owner_and_repo() {
38        assert_eq!(
39            get_owner_and_repo("https://github.com/szabgab/rust-digger"),
40            (
41                "github".to_string(),
42                "szabgab".to_string(),
43                "rust-digger".to_string()
44            )
45        );
46        assert_eq!(
47            get_owner_and_repo("https://github.com/szabgab/rust-digger/"),
48            (
49                "github".to_string(),
50                "szabgab".to_string(),
51                "rust-digger".to_string()
52            )
53        );
54        assert_eq!(
55            get_owner_and_repo(
56                "https://github.com/crypto-crawler/crypto-crawler-rs/tree/main/crypto-market-type"
57            ),
58            (
59                "github".to_string(),
60                "crypto-crawler".to_string(),
61                "crypto-crawler-rs".to_string()
62            )
63        );
64        assert_eq!(
65            get_owner_and_repo("https://gitlab.com/szabgab/rust-digger"),
66            (
67                "gitlab".to_string(),
68                "szabgab".to_string(),
69                "rust-digger".to_string()
70            )
71        );
72        assert_eq!(
73            get_owner_and_repo("https://gitlab.com/Szabgab/Rust-digger/"),
74            (
75                "gitlab".to_string(),
76                "szabgab".to_string(),
77                "rust-digger".to_string()
78            )
79        );
80    }
81}
82