2022-08-13 14:09:48 +00:00
|
|
|
use super::matcher;
|
|
|
|
|
use super::thread_pool;
|
|
|
|
|
|
2022-08-26 15:01:22 +00:00
|
|
|
use std::sync::mpsc;
|
2022-08-13 14:09:48 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
pub struct Match {
|
|
|
|
|
pub score: i64,
|
|
|
|
|
pub content: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Options {
|
|
|
|
|
pub pattern: String,
|
|
|
|
|
pub minimun_score: i64,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Options {
|
|
|
|
|
pub fn new(pattern: String) -> Self {
|
2022-08-26 09:25:05 +00:00
|
|
|
Self {
|
|
|
|
|
pattern,
|
|
|
|
|
minimun_score: 20,
|
|
|
|
|
}
|
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
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|candidate| Match {
|
|
|
|
|
score: matcher.score(candidate.as_str()),
|
|
|
|
|
content: candidate,
|
2022-08-13 14:09:48 +00:00
|
|
|
})
|
2022-08-26 15:29:11 +00:00
|
|
|
.filter(|m| m.score > 25)
|
|
|
|
|
.collect::<Vec<Match>>();
|
2022-08-26 15:01:22 +00:00
|
|
|
matches.sort_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
|
|
|
}
|