-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c5647b
commit 293889b
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher}; | ||
|
||
#[derive(Default)] | ||
pub enum Matcher { | ||
#[default] | ||
Substring, | ||
Fuzzy(Box<SkimMatcherV2>), | ||
} | ||
|
||
impl Matcher { | ||
pub fn substring() -> Self { | ||
Matcher::Substring | ||
} | ||
|
||
pub fn fuzzy() -> Self { | ||
Matcher::Fuzzy(Box::default()) | ||
} | ||
|
||
pub fn match_indices(&self, text: &str, pattern: &str) -> Option<Vec<usize>> { | ||
match self { | ||
Matcher::Substring => text | ||
.find(pattern) | ||
.map(|pos| (pos..pos + pattern.len()).collect()), | ||
Matcher::Fuzzy(matcher) => matcher | ||
.fuzzy_indices(text, pattern) | ||
.map(|(_, indices)| indices), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_matcher_substring() { | ||
let matcher = Matcher::substring(); | ||
assert_eq!(matcher.match_indices("hello", "he"), Some(vec![0, 1])); | ||
assert_eq!(matcher.match_indices("hello", "lo"), Some(vec![3, 4])); | ||
assert_eq!(matcher.match_indices("hello", "ho"), None); | ||
assert_eq!(matcher.match_indices("hello", "wr"), None); | ||
} | ||
|
||
#[test] | ||
fn test_matcher_fuzzy() { | ||
let matcher = Matcher::fuzzy(); | ||
assert_eq!(matcher.match_indices("hello", "he"), Some(vec![0, 1])); | ||
assert_eq!(matcher.match_indices("hello", "lo"), Some(vec![3, 4])); | ||
assert_eq!(matcher.match_indices("hello", "ho"), Some(vec![0, 4])); | ||
assert_eq!(matcher.match_indices("hello", "wr"), None); | ||
} | ||
} |