From adaeba1a907dfee5f33afe832e9e9a7e9f59b1f2 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Sun, 29 Sep 2024 22:46:58 +0200 Subject: implemented search mode (for entries only ATM) --- src/backend/search.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/backend/search.rs (limited to 'src/backend') diff --git a/src/backend/search.rs b/src/backend/search.rs new file mode 100644 index 0000000..ae874db --- /dev/null +++ b/src/backend/search.rs @@ -0,0 +1,40 @@ +use nucleo_matcher::{ + pattern::{CaseMatching, Normalization, Pattern}, + Config, Matcher, +}; +use std::collections::HashMap; + +// Stringify inner Vec by joining/concat +fn convert_to_string(inner_vec: &Vec) -> String { + inner_vec.join(" ") +} + +// Return a filtered entry list +pub fn search_entry_list(search_pattern: &str, orig_list: Vec>) -> Vec> { + // Create a hashmap to connect stingified entry with entry vec + let mut entry_string_hm: HashMap> = HashMap::new(); + + // Convert all entries to string and insert them into the hashmap + // next to the original inner Vec of the entry list + for entry in orig_list { + entry_string_hm.insert(convert_to_string(&entry), entry); + } + + // Set up matcher (TODO: One time needed only, move to higher level) + let mut matcher = Matcher::new(Config::DEFAULT); + + // Filter the stringified entries and collect them into a vec + let filtered_matches: Vec = { + let matches = Pattern::parse(search_pattern, CaseMatching::Ignore, Normalization::Smart) + .match_list(entry_string_hm.keys(), &mut matcher); + matches.into_iter().map(|f| f.0.to_string()).collect() + }; + + // Create filtered entry list and push the inner entry vec's to it + // Use the filtered stringified hm-key as index + let mut filtered_list: Vec> = Vec::new(); + for m in filtered_matches { + filtered_list.push(entry_string_hm[&m].to_owned()); + } + filtered_list +} -- cgit v1.2.3