1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use super::*;

impl Authors {
    pub async fn query<Q>(&mut self, query: Q) -> Result<()>
    where
        Q: Into<AuthorQuery>,
    {
        match query.into() {
            AuthorQuery::Nothing => {}
            AuthorQuery::User(user) => collect_user_events(&user, self).await?,
            AuthorQuery::Repo(user, repo) => collect_repo_events(&user, &repo, self).await?,
        }
        Ok(())
    }
}

impl AuthorQuery {
    pub fn is_none(&self) -> bool {
        matches!(self, AuthorQuery::Nothing)
    }
    pub fn is_some(&self) -> bool {
        !self.is_none()
    }
}

impl From<&str> for AuthorQuery {
    fn from(value: &str) -> Self {
        match Url::parse(value) {
            Ok(o) => AuthorQuery::from(&o),
            Err(_) => AuthorQuery::Nothing,
        }
    }
}

impl From<&Url> for AuthorQuery {
    fn from(value: &Url) -> Self {
        let path = value.path().split("/").collect::<Vec<_>>();
        match path_slice(&path) {
            [user] => AuthorQuery::User(user.to_string()),
            [user, repo, ..] => AuthorQuery::Repo(user.to_string(), repo.to_string()),
            _ => AuthorQuery::Nothing,
        }
    }
}

fn path_slice<'v, 's>(path: &'v [&'s str]) -> &'v [&'s str] {
    let mut l = 0;
    let mut r = path.len();
    for ls in path {
        match ls.is_empty() {
            true => l += 1,
            false => break,
        }
    }
    for rs in path {
        match rs.is_empty() {
            true => r -= 1,
            false => break,
        }
    }
    &path[l..r]
}

#[test]
fn test() {
    println!("{:#?}", AuthorQuery::from("https://github.com/oovm/get-github-email"))
}