git_anger_library/
author.rs

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