2022-08-13 14:09:48 +00:00
|
|
|
use super::matcher;
|
2022-08-26 15:34:15 +00:00
|
|
|
use rayon::prelude::*;
|
2022-08-13 14:09:48 +00:00
|
|
|
|
|
|
|
|
pub struct Match {
|
|
|
|
|
pub score: i64,
|
|
|
|
|
pub content: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Options {
|
|
|
|
|
pub pattern: String,
|
2022-08-26 15:40:12 +00:00
|
|
|
pub minimum_score: i64,
|
2022-08-13 14:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Options {
|
|
|
|
|
pub fn new(pattern: String) -> Self {
|
2022-08-26 09:25:05 +00:00
|
|
|
Self {
|
|
|
|
|
pattern,
|
2022-08-26 15:40:12 +00:00
|
|
|
minimum_score: 25,
|
2022-08-26 09:25:05 +00:00
|
|
|
}
|
2022-08-13 14:09:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 15:01:22 +00:00
|
|
|
pub fn sort_strings(options: Options, strings: Vec<String>) -> Vec<Match> {
|
2022-08-26 15:29:11 +00:00
|
|
|
let matcher = matcher::Matcher::new(options.pattern);
|
2022-08-13 14:09:48 +00:00
|
|
|
|
2022-08-26 15:29:11 +00:00
|
|
|
let mut matches = strings
|
2022-08-26 15:34:15 +00:00
|
|
|
.into_par_iter()
|
2023-11-30 14:07:00 +00:00
|
|
|
.filter_map(|candidate| {
|
|
|
|
|
let score = matcher.score(candidate.as_str());
|
|
|
|
|
if score < options.minimum_score {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(Match {
|
|
|
|
|
score,
|
|
|
|
|
content: candidate,
|
|
|
|
|
})
|
|
|
|
|
}
|
2022-08-13 14:09:48 +00:00
|
|
|
})
|
2022-08-26 15:29:11 +00:00
|
|
|
.collect::<Vec<Match>>();
|
2023-11-30 14:07:00 +00:00
|
|
|
|
2022-08-26 15:36:34 +00:00
|
|
|
matches.par_sort_unstable_by(|a, b| a.score.cmp(&b.score));
|
2022-08-26 09:25:05 +00:00
|
|
|
matches
|
2022-08-13 14:09:48 +00:00
|
|
|
}
|