Skip to content

Commit

Permalink
make SortMethod Copy
Browse files Browse the repository at this point in the history
  • Loading branch information
camdencheek committed Nov 27, 2023
1 parent d1d274c commit bba4e61
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub mod args;
pub mod stats;
pub mod store;

#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub enum SortMethod {
Recent,
Frequent,
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ fn main() {
// If a limit is specified, parse it and use it
if let Some(s) = matches.value_of("limit") {
match s.parse::<usize>() {
Ok(l) => usage.print_sorted(&sort_method, matches.is_present("stat"), Some(l)),
Ok(l) => usage.print_sorted(sort_method, matches.is_present("stat"), Some(l)),
Err(_) => error_and_exit!("invalid limit '{}'", s),
};
} else {
usage.print_sorted(&sort_method, matches.is_present("stat"), None);
usage.print_sorted(sort_method, matches.is_present("stat"), None);
}
}

Expand Down Expand Up @@ -95,7 +95,7 @@ fn main() {
// Truncate store to top N directories
if let Some(n) = matches.value_of("truncate") {
match n.parse::<usize>() {
Ok(keep_num) => usage.truncate(keep_num, &sort_method),
Ok(keep_num) => usage.truncate(keep_num, sort_method),
Err(_) => error_and_exit!("invalid truncate limit '{}'", n),
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/stats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl ItemStats {
}

/// Compare the score of two items given a sort method
pub fn cmp_score(&self, other: &ItemStats, method: &SortMethod) -> Ordering {
pub fn cmp_score(&self, other: &ItemStats, method: SortMethod) -> Ordering {
match method {
SortMethod::Frequent => self.cmp_frequent(other),
SortMethod::Recent => self.cmp_recent(other),
Expand Down Expand Up @@ -104,7 +104,7 @@ impl ItemStats {
}

/// sort method if `show_stats` is `true`
pub fn to_string(&self, method: &SortMethod, show_stats: bool) -> String {
pub fn to_string(&self, method: SortMethod, show_stats: bool) -> String {
if show_stats {
match method {
SortMethod::Recent => format!(
Expand Down Expand Up @@ -185,15 +185,15 @@ mod tests {

assert_eq!(
Ordering::Less,
low_item_stats.cmp_score(&high_item_stats, &SortMethod::Frecent)
low_item_stats.cmp_score(&high_item_stats, SortMethod::Frecent)
);
assert_eq!(
Ordering::Less,
low_item_stats.cmp_score(&high_item_stats, &SortMethod::Recent)
low_item_stats.cmp_score(&high_item_stats, SortMethod::Recent)
);
assert_eq!(
Ordering::Less,
low_item_stats.cmp_score(&high_item_stats, &SortMethod::Frequent)
low_item_stats.cmp_score(&high_item_stats, SortMethod::Frequent)
);
}

Expand Down Expand Up @@ -230,23 +230,23 @@ mod tests {
fn to_string_no_stats() {
let low_item_stats = create_item();

assert_that!(low_item_stats.to_string(&SortMethod::Frecent, false))
assert_that!(low_item_stats.to_string(SortMethod::Frecent, false))
.is_equal_to("/test/item\n".to_string());
assert_that!(low_item_stats.to_string(&SortMethod::Recent, false))
assert_that!(low_item_stats.to_string(SortMethod::Recent, false))
.is_equal_to("/test/item\n".to_string());
assert_that!(low_item_stats.to_string(&SortMethod::Frequent, false))
assert_that!(low_item_stats.to_string(SortMethod::Frequent, false))
.is_equal_to("/test/item\n".to_string());
}

#[test]
fn to_string_stats() {
let low_item_stats = create_item();

assert_that!(low_item_stats.to_string(&SortMethod::Frecent, true))
assert_that!(low_item_stats.to_string(SortMethod::Frecent, true))
.is_equal_to("0.000\t/test/item\n".to_string());
assert_that!(low_item_stats.to_string(&SortMethod::Recent, true))
assert_that!(low_item_stats.to_string(SortMethod::Recent, true))
.is_equal_to("0.000\t/test/item\n".to_string());
assert_that!(low_item_stats.to_string(&SortMethod::Frequent, true))
assert_that!(low_item_stats.to_string(SortMethod::Frequent, true))
.is_equal_to("0\t/test/item\n".to_string());
}

Expand Down
18 changes: 9 additions & 9 deletions src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Default for FrecencyStore {

impl FrecencyStore {
/// Remove all but the top N (sorted by `sort_method`) from the `UsageStore`
pub fn truncate(&mut self, keep_num: usize, sort_method: &SortMethod) {
pub fn truncate(&mut self, keep_num: usize, sort_method: SortMethod) {
let mut sorted_vec = self.sorted(sort_method);
sorted_vec.truncate(keep_num);
self.items = sorted_vec;
Expand Down Expand Up @@ -109,7 +109,7 @@ impl FrecencyStore {
}

/// Print out all the items, sorted by `method`, with an optional maximum of `limit`
pub fn print_sorted(&self, method: &SortMethod, show_stats: bool, limit: Option<usize>) {
pub fn print_sorted(&self, method: SortMethod, show_stats: bool, limit: Option<usize>) {
let stdout = io::stdout();
let handle = stdout.lock();
let mut writer = BufWriter::new(handle);
Expand All @@ -128,7 +128,7 @@ impl FrecencyStore {
}

/// Return a sorted vector of all the items in the store, sorted by `sort_method`
fn sorted(&self, sort_method: &SortMethod) -> Vec<ItemStats> {
fn sorted(&self, sort_method: SortMethod) -> Vec<ItemStats> {
let mut new_vec = self.items.clone();
new_vec.sort_by(|item1, item2| item1.cmp_score(item2, sort_method).reverse());

Expand Down Expand Up @@ -227,7 +227,7 @@ mod tests {
usage.add("dir1");
usage.add("dir2");

usage.truncate(1, &SortMethod::Recent);
usage.truncate(1, SortMethod::Recent);

assert_that!(usage.items.len()).is_equal_to(1);
}
Expand All @@ -238,7 +238,7 @@ mod tests {
usage.add("dir1");
usage.add("dir2");

usage.truncate(3, &SortMethod::Recent);
usage.truncate(3, SortMethod::Recent);

assert_that!(usage.items.len()).is_equal_to(2);
}
Expand All @@ -250,7 +250,7 @@ mod tests {
usage.add("dir2");
usage.get("dir2").update_frecency(1000.0);

let sorted = usage.sorted(&SortMethod::Frecent);
let sorted = usage.sorted(SortMethod::Frecent);

assert_that!(sorted.len()).is_equal_to(2);
assert_that!(sorted[0].item).is_equal_to("dir2".to_string());
Expand All @@ -263,7 +263,7 @@ mod tests {
usage.add("dir2");
usage.get("dir1").update_frecency(1000.0);

let sorted = usage.sorted(&SortMethod::Frecent);
let sorted = usage.sorted(SortMethod::Frecent);

assert_that!(sorted.len()).is_equal_to(2);
assert_that!(sorted[0].item).is_equal_to("dir1".to_string());
Expand All @@ -278,7 +278,7 @@ mod tests {
.get("dir2")
.update_last_access(current_time_secs() + 100.0);

let sorted = usage.sorted(&SortMethod::Recent);
let sorted = usage.sorted(SortMethod::Recent);

assert_that!(sorted.len()).is_equal_to(2);
assert_that!(sorted[0].item).is_equal_to("dir2".to_string());
Expand All @@ -291,7 +291,7 @@ mod tests {
usage.add("dir2");
usage.get("dir2").update_num_accesses(100);

let sorted = usage.sorted(&SortMethod::Frequent);
let sorted = usage.sorted(SortMethod::Frequent);

assert_that!(sorted.len()).is_equal_to(2);
assert_that!(sorted[0].item).is_equal_to("dir2".to_string());
Expand Down

0 comments on commit bba4e61

Please sign in to comment.