Skip to content

Commit 59d9994

Browse files
committed
Add struct for curses
Enables us to sort the curses by occurrence and also fixes how the output looks at the same time by implementing Display. Fixes #3 and fixes #2.
1 parent f8dfa34 commit 59d9994

File tree

4 files changed

+55
-13
lines changed

4 files changed

+55
-13
lines changed

src/author.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::curse::Curse;
12
use hashbrown::HashMap;
23
use std::fmt;
34

@@ -11,15 +12,18 @@ pub struct Author {
1112
/// Total count of curses used by author.
1213
pub total_curses: usize,
1314
/// HashMap of all the curses the author used.
14-
pub curses: HashMap<String, usize>,
15+
pub curses: HashMap<String, Curse>,
1516
}
1617

1718
impl fmt::Display for Author {
1819
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1920
write!(
2021
f,
21-
"{}: ({}/{}) naughty commits/commits\n{:#?}",
22-
self.name, self.total_curses, self.total_commits, self.curses
22+
"{}: ({}/{}) naughty commits/commits\n{}",
23+
self.name,
24+
self.total_curses,
25+
self.total_commits,
26+
Curse::sort(&self.curses)
2327
)
2428
}
2529
}
@@ -39,9 +43,9 @@ impl Author {
3943
pub fn update_occurrence(&mut self, curse: &str) {
4044
self.curses
4145
.get_mut(curse)
42-
.map(|c| *c += 1)
46+
.map(|c| c.count += 1)
4347
.unwrap_or_else(|| {
44-
self.curses.insert(curse.to_owned(), 1);
48+
self.curses.insert(curse.into(), Curse::new(curse, 1));
4549
})
4650
}
4751

src/curse.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use hashbrown::HashMap;
2+
use std::fmt;
3+
4+
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)]
5+
pub struct Curse {
6+
pub count: usize,
7+
pub curse: String,
8+
}
9+
10+
impl fmt::Display for Curse {
11+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
12+
write!(f, "\t{}: {}", self.curse, self.count)
13+
}
14+
}
15+
16+
impl Curse {
17+
pub fn new(curse: impl Into<String>, count: usize) -> Self {
18+
Curse {
19+
curse: curse.into(),
20+
count,
21+
}
22+
}
23+
24+
pub fn sort(curses: &HashMap<String, Curse>) -> String {
25+
let mut curses: Vec<_> = curses.iter().map(|c| c.1).collect();
26+
curses.sort_unstable();
27+
curses.reverse();
28+
let mut result = String::new();
29+
for curse in &curses {
30+
result.push_str(&format!("{}\n", curse));
31+
}
32+
result
33+
}
34+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
)]
8383
mod author;
8484
mod core;
85+
mod curse;
8586
mod repo;
8687

8788
pub use crate::author::Author;

src/repo.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::author::Author;
1+
use crate::{author::Author, curse::Curse};
22
use hashbrown::HashMap;
33
use std::fmt;
44

@@ -12,7 +12,7 @@ pub struct Repo {
1212
/// Count of the total amount of curses used in the commits.
1313
pub total_curses: usize,
1414
/// HashMap of all the naughty words used by the authors.
15-
pub curses: HashMap<String, usize>,
15+
pub curses: HashMap<String, Curse>,
1616
/// HashMap of all the authors that have been committed.
1717
pub authors: HashMap<String, Author>,
1818
}
@@ -21,8 +21,11 @@ impl fmt::Display for Repo {
2121
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2222
write!(
2323
f,
24-
"{}: ({}/{}) naughty commits/commits\n{:#?}",
25-
self.name, self.total_curses, self.total_commits, self.curses
24+
"{}: ({}/{}) naughty commits/commits:\n{}",
25+
self.name,
26+
self.total_curses,
27+
self.total_commits,
28+
Curse::sort(&self.curses)
2629
)
2730
}
2831
}
@@ -54,11 +57,11 @@ impl Repo {
5457
/// Counts all the naughty words used by authors.
5558
pub fn count_curses(&mut self) {
5659
for author in self.authors.values() {
57-
for (curse, count) in &author.curses {
60+
for (name, curse) in &author.curses {
5861
self.curses
59-
.entry(curse.to_string())
60-
.and_modify(|c| *c += count)
61-
.or_insert(*count);
62+
.entry(name.to_string())
63+
.and_modify(|c| c.count += curse.count)
64+
.or_insert(Curse::new(name.as_str(), curse.count));
6265
}
6366
}
6467
}

0 commit comments

Comments
 (0)