Skip to content

Commit

Permalink
Add struct for curses
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sondr3 committed Dec 28, 2018
1 parent f8dfa34 commit 59d9994
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
14 changes: 9 additions & 5 deletions src/author.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::curse::Curse;
use hashbrown::HashMap;
use std::fmt;

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

impl fmt::Display for Author {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}: ({}/{}) naughty commits/commits\n{:#?}",
self.name, self.total_curses, self.total_commits, self.curses
"{}: ({}/{}) naughty commits/commits\n{}",
self.name,
self.total_curses,
self.total_commits,
Curse::sort(&self.curses)
)
}
}
Expand All @@ -39,9 +43,9 @@ impl Author {
pub fn update_occurrence(&mut self, curse: &str) {
self.curses
.get_mut(curse)
.map(|c| *c += 1)
.map(|c| c.count += 1)
.unwrap_or_else(|| {
self.curses.insert(curse.to_owned(), 1);
self.curses.insert(curse.into(), Curse::new(curse, 1));
})
}

Expand Down
34 changes: 34 additions & 0 deletions src/curse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use hashbrown::HashMap;
use std::fmt;

#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)]
pub struct Curse {
pub count: usize,
pub curse: String,
}

impl fmt::Display for Curse {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "\t{}: {}", self.curse, self.count)
}
}

impl Curse {
pub fn new(curse: impl Into<String>, count: usize) -> Self {
Curse {
curse: curse.into(),
count,
}
}

pub fn sort(curses: &HashMap<String, Curse>) -> String {
let mut curses: Vec<_> = curses.iter().map(|c| c.1).collect();
curses.sort_unstable();
curses.reverse();
let mut result = String::new();
for curse in &curses {
result.push_str(&format!("{}\n", curse));
}
result
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
)]
mod author;
mod core;
mod curse;
mod repo;

pub use crate::author::Author;
Expand Down
19 changes: 11 additions & 8 deletions src/repo.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::author::Author;
use crate::{author::Author, curse::Curse};
use hashbrown::HashMap;
use std::fmt;

Expand All @@ -12,7 +12,7 @@ pub struct Repo {
/// Count of the total amount of curses used in the commits.
pub total_curses: usize,
/// HashMap of all the naughty words used by the authors.
pub curses: HashMap<String, usize>,
pub curses: HashMap<String, Curse>,
/// HashMap of all the authors that have been committed.
pub authors: HashMap<String, Author>,
}
Expand All @@ -21,8 +21,11 @@ impl fmt::Display for Repo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}: ({}/{}) naughty commits/commits\n{:#?}",
self.name, self.total_curses, self.total_commits, self.curses
"{}: ({}/{}) naughty commits/commits:\n{}",
self.name,
self.total_curses,
self.total_commits,
Curse::sort(&self.curses)
)
}
}
Expand Down Expand Up @@ -54,11 +57,11 @@ impl Repo {
/// Counts all the naughty words used by authors.
pub fn count_curses(&mut self) {
for author in self.authors.values() {
for (curse, count) in &author.curses {
for (name, curse) in &author.curses {
self.curses
.entry(curse.to_string())
.and_modify(|c| *c += count)
.or_insert(*count);
.entry(name.to_string())
.and_modify(|c| c.count += curse.count)
.or_insert(Curse::new(name.as_str(), curse.count));
}
}
}
Expand Down

0 comments on commit 59d9994

Please sign in to comment.