blob: fb53d1c032ab2a6217d771fe5f65aba57973fd59 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
pub trait Matcher: Send + Sync {
fn score(&self, choice: &str, pattern: &str) -> Option<i32>;
}
impl<T: fuzzy_matcher::FuzzyMatcher> Matcher for T {
fn score(&self, choice: &str, pattern: &str) -> Option<i32> {
fuzzy_matcher::FuzzyMatcher::fuzzy_match(self, choice, pattern)
}
}
#[derive(Debug)]
pub struct Prefix;
impl Matcher for Prefix {
fn score(&self, choice: &str, pattern: &str) -> Option<i32> {
if choice.starts_with(pattern) {
Some(-(choice.len() as i32))
} else {
None
}
}
}
#[derive(Debug)]
pub struct PrefixIgnoreCase;
impl Matcher for PrefixIgnoreCase {
fn score(&self, choice: &str, pattern: &str) -> Option<i32> {
if pattern.len() > choice.len() {
return None;
}
let mut cs = choice.chars();
for p in pattern.chars() {
if !cs.next().unwrap().eq_ignore_ascii_case(&p) {
return None;
}
}
Some(-(choice.len() as i32))
}
}
|