lcode/
fuzzy_search.rs

1use atoi::atoi;
2use inquire::Select;
3use lcode_config::global::G_USER_CONFIG;
4use leetcode_api::dao::query::Query;
5use miette::Result;
6
7// use rayon::prelude::*;
8
9pub async fn select_a_question() -> Result<u32> {
10    let questions = Query::query_all_index().await?;
11
12    let indexs = questions
13        .into_iter()
14        .map(|v| v.to_string())
15        .collect();
16
17    let selected = Select::new("Select question ❓:", indexs)
18        .with_formatter(&|v| format!("{:.10}", v.to_string()))
19        .with_page_size(G_USER_CONFIG.config.page_size)
20        .prompt()
21        .unwrap_or_default();
22
23    let selected: Vec<&str> = selected.split('[').collect();
24    let id_str = selected
25        .get(1)
26        .copied()
27        .unwrap_or_default();
28
29    let id = atoi::<u32>(id_str.as_bytes()).unwrap_or_default();
30
31    Ok(id)
32}
33
34#[inline]
35pub fn filter(input: &str, string_value: &str) -> bool {
36    use simsearch::SimSearch;
37    let mut search_engine = SimSearch::new();
38    search_engine.insert(string_value, string_value);
39    let res = search_engine.search(input);
40
41    !res.is_empty()
42        || string_value
43            .to_lowercase()
44            .contains(&input.to_lowercase())
45}
46
47// pub fn new_filter<T>(a: Vec<T>, pat: &str) -> Vec<(T, u32)>
48// where
49//     T: Display + Sized + Send + Sync,
50// {
51//     use nucleo::{
52//         pattern::{CaseMatching, Pattern},
53//         Config, Matcher,
54//     };
55//
56//     let a: Vec<String> = a
57//         .into_par_iter()
58//         .map(|v| v.to_string())
59//         .collect();
60//
61//     let mut matcher = Matcher::new(Config::DEFAULT.match_paths());
62//     let matches = Pattern::parse(pat, CaseMatching::Ignore).match_list(a, &mut matcher);
63//
64//     let a = vec![];
65//     a
66// }