fuzzy_cmp/fuzzy/
search.rs

1// use crate::prelude::*;
2use super::*;
3
4/// Search string in strings array with sorting by coof (0.0 - 0%, 1.0 - 100%)
5pub fn search<S>(v: &[S], s: &str, min_coef: f32, deep: bool) -> Vec<(f32, String)>
6where
7    S: AsRef<str>
8{
9    // search in array:
10    let mut results: Vec<(f32, String)> = v.iter()
11        .map(|vs| {
12            let vs = vs.as_ref().to_string();
13            let coof = if deep { deep_compare(&vs, s, min_coef) }else{ compare(&vs, s) };
14            
15            (coof, vs.clone())
16        })
17        .filter(|(c, _)| *c >= min_coef)
18        .collect();
19
20    // sort by coef:
21    results.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap());
22    results
23}
24
25/// Search string in custom type array with sorting by coof (0.0 - 0%, 1.0 - 100%)
26pub fn search_filter<T, F>(v: &[T], s: &str, min_coef: f32, deep: bool, extract: F) -> Vec<(f32, T)>
27where 
28    T: Clone,
29    F: Fn(&T) -> &str
30{
31    // search in array:
32    let mut results: Vec<(f32, T)> = v.iter()
33        .map(|item| {
34            let vs = extract(item);
35            let coof = if deep { deep_compare(&vs, s, min_coef) }else{ compare(&vs, s) };
36
37            (coof, item.clone())
38        })
39        .filter(|(c, _)| *c >= min_coef)
40        .collect();
41
42    // sort by coef:
43    results.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap());
44    results
45}