ivy.nvim/rust/sorter.rs

44 lines
1 KiB
Rust
Raw Normal View History

use super::matcher;
use rayon::prelude::*;
2022-08-28 16:05:14 +00:00
use std::os::raw::{c_char, c_int};
use std::ffi::CString;
2022-08-28 16:05:14 +00:00
#[repr(C)]
pub struct Match {
2022-08-28 16:05:14 +00:00
pub score: c_int,
pub content: *const c_char
// pub score: i64,
// pub content: String,
}
2022-08-28 16:05:14 +00:00
unsafe impl Send for Match {}
pub struct Options {
pub pattern: String,
pub minimum_score: i64,
}
impl Options {
pub fn new(pattern: String) -> Self {
2022-08-26 09:25:05 +00:00
Self {
pattern,
minimum_score: 25,
2022-08-26 09:25:05 +00:00
}
}
}
pub fn sort_strings(options: Options, strings: Vec<String>) -> Vec<Match> {
let matcher = matcher::Matcher::new(options.pattern);
let mut matches = strings
.into_par_iter()
.map(|candidate| Match {
2022-08-28 16:05:14 +00:00
score: matcher.score(candidate.as_str()) as i32,
content: CString::new(candidate.clone().to_string()).unwrap().into_raw(),
})
2022-08-28 16:05:14 +00:00
.filter(|m| m.score > options.minimum_score as i32)
.collect::<Vec<Match>>();
matches.par_sort_unstable_by(|a, b| a.score.cmp(&b.score));
2022-08-26 09:25:05 +00:00
matches
}