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
43
44
45
46
47
48
49
50
51
52
53
use fuzzy_matcher::skim::SkimMatcherV2;
use fuzzy_matcher::FuzzyMatcher;

pub(crate) struct Matcher<'a, T: Iterator<Item = &'a str>> {
    values: Option<T>,
}

impl<'a, T: Iterator<Item = &'a str>> Matcher<'a, T> {
    pub fn new(values: Option<T>) -> Self {
        Self { values }
    }

    pub(crate) fn match_with_key(&mut self, needle: &str) -> Option<Vec<usize>> {
        let matcher = SkimMatcherV2::default();
        let mut res = vec![];
        if let Some(iterator) = self.values.take() {
            for (i, haystack) in iterator.enumerate() {
                if matcher.fuzzy_match(haystack, needle).is_some() {
                    res.push(i);
                }
            }
        }
        Some(res)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_match() {
        let values = vec!["bello", "helloworld"];
        let values2 = values.clone();
        let values3 = values.clone();
        let mut m = Matcher {
            values: Some(values.into_iter()),
        };
        assert_eq!(m.match_with_key("bello"), Some(vec![0]));

        let mut m = Matcher {
            values: Some(values2.into_iter()),
        };

        assert_eq!(m.match_with_key("elo"), Some(vec![0, 1]));

        let mut m = Matcher {
            values: Some(values3.into_iter()),
        };

        assert_eq!(m.match_with_key("hello"), Some(vec![1]));
    }
}