gitmoji_changelog/
group.rs

1use std::cmp::Ordering;
2use std::collections::HashMap;
3
4use crate::commit::Commit;
5
6lazy_static! {
7    static ref GROUPS: Vec<Group> = {
8        let mut groups = vec![];
9
10        groups.push(Group::new(
11            "Added",
12            10,
13            vec![
14                "sparkles",
15                "tada",
16                "sparkles",
17                "tada",
18                "white_check_mark",
19                "construction_worker",
20                "chart_with_upwards_trend",
21                "heavy_plus_sign",
22            ],
23        ));
24
25        groups.push(Group::new(
26            "Changed",
27            20,
28            vec![
29                "art",
30                "zap",
31                "lipstick",
32                "rotating_light",
33                "arrow_down",
34                "arrow_up",
35                "pushpin",
36                "recycle",
37                "hammer",
38                "wrench",
39                "rewind",
40                "alien",
41                "truck",
42                "bento",
43                "wheelchair",
44                "speech_balloon",
45                "card_file_box",
46                "children_crossing",
47                "building_construction",
48                "iphone",
49            ],
50        ));
51
52        groups.push(Group::new("Breaking changes", 30, vec!["boom"]));
53
54        groups.push(Group::new("Deprecated", 40, vec![]));
55
56        groups.push(Group::new(
57            "Removed",
58            50,
59            vec!["fire", "heavy_minus_sign", "mute"],
60        ));
61
62        groups.push(Group::new(
63            "Fixed",
64            60,
65            vec![
66                "bug",
67                "ambulance",
68                "apple",
69                "penguin",
70                "checkered_flag",
71                "robot",
72                "green_apple",
73                "green_heart",
74                "pencil2",
75            ],
76        ));
77
78        groups.push(Group::new("Security", 70, vec!["lock"]));
79
80        groups.push(Group::new("Miscellaneous", 80, vec![]));
81
82        groups.push(Group::new("Useless", 90, vec!["bookmark"]));
83
84        groups
85    };
86}
87
88#[derive(Debug, Serialize, Eq, Clone)]
89pub struct Group {
90    pub order: usize,
91    pub name: &'static str,
92    pub codes: Vec<&'static str>, // TODO: remove this from here
93    pub commits: Vec<Commit>,
94}
95
96impl PartialEq for Group {
97    fn eq(&self, other: &Group) -> bool {
98        self.name == other.name
99    }
100}
101
102impl Ord for Group {
103    fn cmp(&self, other: &Group) -> Ordering {
104        self.order.cmp(&other.order)
105    }
106}
107
108impl PartialOrd for Group {
109    fn partial_cmp(&self, other: &Group) -> Option<Ordering> {
110        Some(self.cmp(other))
111    }
112}
113
114impl Group {
115    pub fn new(name: &'static str, order: usize, codes: Vec<&'static str>) -> Group {
116        Group {
117            order,
118            name,
119            codes,
120            commits: vec![],
121        }
122    }
123
124    pub fn from_commits(commits: Vec<Commit>) -> Vec<Group> {
125        let mut groups = HashMap::<&'static str, Group>::new();
126
127        for commit in commits {
128            // TODO: use a HashMap instead of doing this cardinal product
129            let group = match GROUPS
130                .iter()
131                .find(|group| group.codes.iter().any(|&code| code == commit.emoji_code))
132            {
133                None => Group::new("Miscellaneous", 80, vec![]),
134                Some(group) => group.clone(),
135            };
136
137            groups
138                .entry(group.name)
139                .or_insert_with(|| Group::new(group.name, group.order, vec![]));
140            groups
141                .entry(group.name)
142                .and_modify(|group| group.commits.push(commit));
143        }
144
145        // Transform the HashMap to a Vec
146        let mut vector = vec![];
147        for (_, group) in groups {
148            if group.name != "Useless" {
149                vector.push(group);
150            }
151        }
152
153        vector.sort();
154
155        vector
156    }
157}