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> {
|
|
|
|
|
let mut matches = Vec::new();
|
|
|
|
|
let matcher = Arc::new(matcher::Matcher::new(options.pattern));
|
2022-08-13 14:09:48 +00:00
|
|
|
|
|
|
|
|
let pool = thread_pool::ThreadPool::new(std::thread::available_parallelism().unwrap().get());
|
|
|
|
|
|
2022-08-26 15:01:22 +00:00
|
|
|
let (tx, rx) = mpsc::channel::<Match>();
|
|
|
|
|
|
2022-08-13 14:09:48 +00:00
|
|
|
for string in strings {
|
|
|
|
|
let thread_matcher = Arc::clone(&matcher);
|
2022-08-26 15:01:22 +00:00
|
|
|
let thread_transmitter = tx.clone();
|
2022-08-13 14:09:48 +00:00
|
|
|
pool.execute(move || {
|
2022-08-26 15:01:22 +00:00
|
|
|
let score = thread_matcher.score(string.to_string());
|
2022-08-13 14:09:48 +00:00
|
|
|
if score > 25 {
|
2022-08-26 15:01:22 +00:00
|
|
|
thread_transmitter
|
|
|
|
|
.send(Match {
|
|
|
|
|
score,
|
|
|
|
|
content: string,
|
|
|
|
|
})
|
|
|
|
|
.expect("Failed to push data to channel");
|
2022-08-13 14:09:48 +00:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
drop(pool);
|
2022-08-26 15:01:22 +00:00
|
|
|
drop(tx);
|
2022-08-13 14:09:48 +00:00
|
|
|
|
2022-08-26 15:01:22 +00:00
|
|
|
while let Ok(result) = rx.recv() {
|
|
|
|
|
matches.push(result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|