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
use std::{
    collections::BTreeMap,
    fmt::{Debug, Formatter},
};

use serde::{Deserialize, Serialize};
use url::Url;

use crate::{collect_repo_events, collect_user_events, Result};

pub mod query;

#[derive(Default, Serialize, Deserialize)]
pub struct Authors {
    inner: BTreeMap<String, CommitAuthor>,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum AuthorQuery {
    Nothing,
    User(String),
    Repo(String, String),
}

#[derive(Debug, Serialize, Deserialize)]
pub struct CommitAuthor {
    pub name: String,
    pub email: String,
    pub count: usize,
}

impl Authors {
    pub fn get(&self, name: &str) -> Option<&CommitAuthor> {
        self.inner.get(name)
    }
    pub fn insert(&mut self, author: CommitAuthor) {
        match self.inner.get_mut(&author.name) {
            Some(s) => s.count += author.count,
            None => self.insert_new(author),
        }
    }

    fn insert_new(&mut self, author: CommitAuthor) {
        self.inner.insert(author.name.clone(), author);
    }
}

impl Debug for Authors {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_list().entries(self.inner.iter().map(|s| s.1)).finish()
    }
}