From 0537a7af6e548b3aab809b3791c7841765a8c4c3 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Mon, 27 Nov 2023 16:08:01 +0100 Subject: [PATCH] fzf: rename `with_case_sensitivity` to `set_case_sensitivity` and add docs to it --- benches/common.rs | 4 ++-- benches/fzf_v1.rs | 4 ++-- benches/fzf_v2.rs | 4 ++-- fuzz/fuzz_targets/fzf.rs | 4 ++-- src/algos/fzf/fzf_v1.rs | 22 ++++++++++++++++++++-- src/algos/fzf/fzf_v2.rs | 22 ++++++++++++++++++++-- tests/fzf_common.rs | 12 ++++++------ tests/fzf_v1.rs | 12 ++++++------ tests/fzf_v2.rs | 12 ++++++------ 9 files changed, 66 insertions(+), 30 deletions(-) diff --git a/benches/common.rs b/benches/common.rs index a82890a..4c70950 100644 --- a/benches/common.rs +++ b/benches/common.rs @@ -22,7 +22,7 @@ pub trait Metric { ranges: &mut Vec>, ); - fn with_case_sensitivity( + fn set_case_sensitivity( &mut self, case_sensitivity: CaseSensitivity, ) -> &mut Self; @@ -76,7 +76,7 @@ fn for_all_cases_and_ranges( CaseSensitivity::Smart, ] { for with_ranges in [true, false] { - metric.with_case_sensitivity(case); + metric.set_case_sensitivity(case); let param = param(case, with_ranges, suffix); let mut ranges = with_ranges.then(Vec::new); fun( diff --git a/benches/fzf_v1.rs b/benches/fzf_v1.rs index c764ffd..6d6ad68 100644 --- a/benches/fzf_v1.rs +++ b/benches/fzf_v1.rs @@ -40,11 +40,11 @@ impl bench::Metric for FzfV1 { ) { self.distance_and_ranges(query, candidate, ranges); } - fn with_case_sensitivity( + fn set_case_sensitivity( &mut self, case_sensitivity: CaseSensitivity, ) -> &mut Self { - self.with_case_sensitivity(case_sensitivity) + self.set_case_sensitivity(case_sensitivity) } } diff --git a/benches/fzf_v2.rs b/benches/fzf_v2.rs index e35d82f..5c79e13 100644 --- a/benches/fzf_v2.rs +++ b/benches/fzf_v2.rs @@ -40,11 +40,11 @@ impl bench::Metric for FzfV2 { ) { self.distance_and_ranges(query, candidate, ranges); } - fn with_case_sensitivity( + fn set_case_sensitivity( &mut self, case_sensitivity: CaseSensitivity, ) -> &mut Self { - self.with_case_sensitivity(case_sensitivity) + self.set_case_sensitivity(case_sensitivity) } } diff --git a/fuzz/fuzz_targets/fzf.rs b/fuzz/fuzz_targets/fzf.rs index f7575f9..2eb9b36 100644 --- a/fuzz/fuzz_targets/fzf.rs +++ b/fuzz/fuzz_targets/fzf.rs @@ -45,7 +45,7 @@ fuzz_target!(|data: (Query, Candidate)| { with_opts(|case_sensitivity, normalization, scheme| { let _ = fzf_v1 - .with_case_sensitivity(case_sensitivity) + .set_case_sensitivity(case_sensitivity) .set_normalization(normalization) .with_scoring_scheme(scheme) .distance_and_ranges(query, candidate, &mut ranges); @@ -55,7 +55,7 @@ fuzz_target!(|data: (Query, Candidate)| { } let _ = fzf_v2 - .with_case_sensitivity(case_sensitivity) + .set_case_sensitivity(case_sensitivity) .set_normalization(normalization) .with_scoring_scheme(scheme) .distance_and_ranges(query, candidate, &mut ranges); diff --git a/src/algos/fzf/fzf_v1.rs b/src/algos/fzf/fzf_v1.rs index ef49368..01f68c9 100644 --- a/src/algos/fzf/fzf_v1.rs +++ b/src/algos/fzf/fzf_v1.rs @@ -93,9 +93,27 @@ impl FzfV1 { &self.scheme } - /// TODO: docs + /// Sets the case sensitivity to use when comparing the characters of the + /// query and the candidate. The default is [`CaseSensitivity::Smart`]. + /// + /// # Example + /// + /// ```rust + /// # use norm::fzf::{FzfV1, FzfParser}; + /// # use norm::{Metric, CaseSensitivity}; + /// let mut fzf = FzfV1::new(); + /// let mut parser = FzfParser::new(); + /// + /// // FzfV1 uses smart case sensitivity by default. + /// assert!(fzf.distance(parser.parse("abc"), "ABC").is_some()); + /// + /// fzf.set_case_sensitivity(CaseSensitivity::Sensitive); + /// + /// // Now it's case sensitive, so the query won't match the candidate. + /// assert!(fzf.distance(parser.parse("abc"), "ABC").is_none()); + /// ``` #[inline(always)] - pub fn with_case_sensitivity( + pub fn set_case_sensitivity( &mut self, case_sensitivity: CaseSensitivity, ) -> &mut Self { diff --git a/src/algos/fzf/fzf_v2.rs b/src/algos/fzf/fzf_v2.rs index 07db388..f3fc727 100644 --- a/src/algos/fzf/fzf_v2.rs +++ b/src/algos/fzf/fzf_v2.rs @@ -104,9 +104,27 @@ impl FzfV2 { &self.scheme } - /// TODO: docs + /// Sets the case sensitivity to use when comparing the characters of the + /// query and the candidate. The default is [`CaseSensitivity::Smart`]. + /// + /// # Example + /// + /// ```rust + /// # use norm::fzf::{FzfV2, FzfParser}; + /// # use norm::{Metric, CaseSensitivity}; + /// let mut fzf = FzfV2::new(); + /// let mut parser = FzfParser::new(); + /// + /// // FzfV2 uses smart case sensitivity by default. + /// assert!(fzf.distance(parser.parse("abc"), "ABC").is_some()); + /// + /// fzf.set_case_sensitivity(CaseSensitivity::Sensitive); + /// + /// // Now it's case sensitive, so the query won't match the candidate. + /// assert!(fzf.distance(parser.parse("abc"), "ABC").is_none()); + /// ``` #[inline(always)] - pub fn with_case_sensitivity( + pub fn set_case_sensitivity( &mut self, case_sensitivity: CaseSensitivity, ) -> &mut Self { diff --git a/tests/fzf_common.rs b/tests/fzf_common.rs index 6f81bfa..2eb1597 100644 --- a/tests/fzf_common.rs +++ b/tests/fzf_common.rs @@ -562,7 +562,7 @@ mod utils { Default + for<'a> Metric = FzfQuery<'a>, Distance = FzfDistance> { - fn with_case_sensitivity( + fn set_case_sensitivity( &mut self, case_sensitivity: CaseSensitivity, ) -> &mut Self; @@ -571,11 +571,11 @@ mod utils { } impl Fzf for FzfV1 { - fn with_case_sensitivity( + fn set_case_sensitivity( &mut self, case_sensitivity: CaseSensitivity, ) -> &mut Self { - self.with_case_sensitivity(case_sensitivity) + self.set_case_sensitivity(case_sensitivity) } fn scheme(&self) -> &norm::fzf::Scheme { @@ -592,11 +592,11 @@ mod utils { } impl Fzf for FzfV2 { - fn with_case_sensitivity( + fn set_case_sensitivity( &mut self, case_sensitivity: CaseSensitivity, ) -> &mut Self { - self.with_case_sensitivity(case_sensitivity) + self.set_case_sensitivity(case_sensitivity) } fn scheme(&self) -> &norm::fzf::Scheme { @@ -619,7 +619,7 @@ mod utils { ) -> (F, Option) { let mut fzf = F::default(); - fzf.with_case_sensitivity(case_sensitivity); + fzf.set_case_sensitivity(case_sensitivity); let mut parser = FzfParser::new(); diff --git a/tests/fzf_v1.rs b/tests/fzf_v1.rs index aa73847..dd0c5a0 100644 --- a/tests/fzf_v1.rs +++ b/tests/fzf_v1.rs @@ -225,7 +225,7 @@ fn fzf_v1_score_1() { let mut ranges = Vec::new(); let _ = fzf - .with_case_sensitivity(CaseSensitivity::Sensitive) + .set_case_sensitivity(CaseSensitivity::Sensitive) .distance_and_ranges(parser.parse("ZZ"), "ӥZZZ", &mut ranges) .unwrap(); @@ -241,7 +241,7 @@ fn fzf_v1_score_2() { let query = parser.parse("^\\$ ]]%]]'\0\0\0\0\0\0"); let mach = fzf - .with_case_sensitivity(CaseSensitivity::Sensitive) + .set_case_sensitivity(CaseSensitivity::Sensitive) .distance(query, "\0"); assert!(mach.is_none()); @@ -256,7 +256,7 @@ fn fzf_v1_score_3() { let query = parser.parse("^\\$"); let mach = fzf - .with_case_sensitivity(CaseSensitivity::Sensitive) + .set_case_sensitivity(CaseSensitivity::Sensitive) .distance(query, " "); assert!(mach.is_none()); @@ -275,7 +275,7 @@ fn fzf_v1_score_4() { let candidate = "ZZ\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\u{65e}\nZ\u{65e}"; let _ = fzf - .with_case_sensitivity(CaseSensitivity::Insensitive) + .set_case_sensitivity(CaseSensitivity::Insensitive) .distance_and_ranges(query, candidate, &mut ranges) .unwrap(); @@ -291,7 +291,7 @@ fn fzf_v1_score_5() { let mut ranges = Vec::new(); let _ = fzf - .with_case_sensitivity(CaseSensitivity::Sensitive) + .set_case_sensitivity(CaseSensitivity::Sensitive) .set_normalization(true) .distance_and_ranges( parser.parse("e !"), @@ -314,7 +314,7 @@ fn fzf_v1_score_6() { let query = parser.parse("^e"); let _ = fzf - .with_case_sensitivity(CaseSensitivity::Insensitive) + .set_case_sensitivity(CaseSensitivity::Insensitive) .set_normalization(true) .distance_and_ranges(query, "\u{364}", &mut ranges); diff --git a/tests/fzf_v2.rs b/tests/fzf_v2.rs index 88cc367..b9e9b12 100644 --- a/tests/fzf_v2.rs +++ b/tests/fzf_v2.rs @@ -225,7 +225,7 @@ fn fzf_v2_score_1() { let mut ranges = Vec::new(); let distance = fzf - .with_case_sensitivity(CaseSensitivity::Sensitive) + .set_case_sensitivity(CaseSensitivity::Sensitive) .distance_and_ranges(parser.parse("jelly"), "jellyfish", &mut ranges) .unwrap(); @@ -247,7 +247,7 @@ fn fzf_v2_score_2() { let mut parser = FzfParser::new(); let distance = fzf - .with_case_sensitivity(CaseSensitivity::Sensitive) + .set_case_sensitivity(CaseSensitivity::Sensitive) .distance(parser.parse("!$"), "$$2"); assert!(distance.is_none()); @@ -262,7 +262,7 @@ fn fzf_v2_score_3() { let mut ranges = Vec::new(); let _ = fzf - .with_case_sensitivity(CaseSensitivity::Sensitive) + .set_case_sensitivity(CaseSensitivity::Sensitive) .distance_and_ranges( parser.parse("\0\0"), "\0#B\0\u{364}\0\0", @@ -282,7 +282,7 @@ fn fzf_v2_score_4() { let mut ranges = Vec::new(); let _ = fzf - .with_case_sensitivity(CaseSensitivity::Sensitive) + .set_case_sensitivity(CaseSensitivity::Sensitive) .set_normalization(true) .distance_and_ranges( parser.parse("e !"), @@ -303,7 +303,7 @@ fn fzf_v2_score_5() { let mut ranges = Vec::new(); let _ = fzf - .with_case_sensitivity(CaseSensitivity::Insensitive) + .set_case_sensitivity(CaseSensitivity::Insensitive) .set_normalization(true) .distance_and_ranges(parser.parse("E"), "\u{364}E", &mut ranges) .unwrap(); @@ -322,7 +322,7 @@ fn fzf_v2_score_6() { let query = parser.parse("!2\t\0\0\0WWHHWHWWWWWWWZ !I"); let distance = fzf - .with_case_sensitivity(CaseSensitivity::Insensitive) + .set_case_sensitivity(CaseSensitivity::Insensitive) .set_normalization(true) .distance_and_ranges( query,