git_anger_management/
author.rs

1use serde::Serialize;
2use std::collections::HashMap;
3
4/// An author of a git commit.
5#[derive(Debug, Serialize)]
6pub struct Author {
7    /// Name of the author.
8    pub name: String,
9    /// Total count of commits by author.
10    pub total_commits: usize,
11    /// Total count of curses used by author.
12    pub total_curses: usize,
13    /// HashMap of all the curses the author used.
14    pub curses: HashMap<String, usize>,
15}
16
17impl Author {
18    /// Initialize a new author from a name.
19    pub fn new(name: impl Into<String>) -> Self {
20        Author {
21            name: name.into(),
22            curses: HashMap::new(),
23            total_commits: 0,
24            total_curses: 0,
25        }
26    }
27
28    /// Update a previously used curse or add a new one.
29    pub fn update_occurrence(&mut self, curse: &str) {
30        self.curses
31            .get_mut(curse)
32            .map(|c| *c += 1)
33            .unwrap_or_else(|| {
34                self.curses.insert(curse.into(), 1);
35            })
36    }
37
38    /// `git-anger-management` knows if you've been naughty or not
39    pub fn is_naughty(&self) -> bool {
40        !self.curses.is_empty()
41    }
42}