2022-08-13 14:09:48 +00:00
|
|
|
use fuzzy_matcher::FuzzyMatcher;
|
|
|
|
|
use fuzzy_matcher::skim::SkimMatcherV2;
|
|
|
|
|
|
|
|
|
|
pub struct Matcher {
|
|
|
|
|
/// The search pattern that we want to match against some text
|
|
|
|
|
pub pattern: String,
|
2022-08-25 18:42:22 +00:00
|
|
|
matcher: SkimMatcherV2,
|
2022-08-13 14:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Matcher {
|
2022-08-25 18:42:22 +00:00
|
|
|
pub fn new(pattern: String) -> Self {
|
|
|
|
|
return Self {
|
|
|
|
|
pattern,
|
|
|
|
|
matcher: SkimMatcherV2::default(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-13 14:09:48 +00:00
|
|
|
pub fn score(self: &Self, text: String) -> i64 {
|
2022-08-25 18:42:22 +00:00
|
|
|
if let Some((score, _indices)) = self.matcher.fuzzy_indices(&text, &self.pattern) {
|
2022-08-13 14:09:48 +00:00
|
|
|
return score;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|