diff --git a/src/lib.rs b/src/lib.rs index d31c21c..a452297 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,7 @@ pub mod args; pub mod stats; pub mod store; -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] pub enum SortMethod { Recent, Frequent, diff --git a/src/main.rs b/src/main.rs index e70a335..5836b84 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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::() { - 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); } } @@ -95,7 +95,7 @@ fn main() { // Truncate store to top N directories if let Some(n) = matches.value_of("truncate") { match n.parse::() { - 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), } } diff --git a/src/stats/mod.rs b/src/stats/mod.rs index a13f96a..94881cf 100644 --- a/src/stats/mod.rs +++ b/src/stats/mod.rs @@ -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), @@ -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!( @@ -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) ); } @@ -230,11 +230,11 @@ 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()); } @@ -242,11 +242,11 @@ mod tests { 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()); } diff --git a/src/store/mod.rs b/src/store/mod.rs index 7067f4d..733374d 100644 --- a/src/store/mod.rs +++ b/src/store/mod.rs @@ -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; @@ -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) { + pub fn print_sorted(&self, method: SortMethod, show_stats: bool, limit: Option) { let stdout = io::stdout(); let handle = stdout.lock(); let mut writer = BufWriter::new(handle); @@ -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 { + fn sorted(&self, sort_method: SortMethod) -> Vec { let mut new_vec = self.items.clone(); new_vec.sort_by(|item1, item2| item1.cmp_score(item2, sort_method).reverse()); @@ -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); } @@ -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); } @@ -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()); @@ -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()); @@ -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()); @@ -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());