pub mod consts; pub mod lex; pub mod parse; mod utils; use qp_trie::{ wrapper::{BStr, BString}, Iter, Trie, }; pub struct Dict { inner: Trie, } impl Dict { fn new() -> Self { Self { inner: Trie::new() } } fn insert(&mut self, entry: &'static str, value: DictValue) { let key = BString::from(entry); let dict_value = self.inner.entry(key).or_insert(Default::default()); for v in value.defn.into_iter() { dict_value.defn.push(v); } } pub fn search<'a, 's>(&'a self, search_term: &'s str) -> SearchResults<'a> { SearchResults(self.inner.iter_prefix_str(search_term)) } } pub struct SearchResults<'a>(Iter<'a, DictKey, DictValue>); impl<'a> SearchResults<'a> { // mutable ref here to advance the iterator present in Self::Hit pub fn print(&mut self) { while let Some((key, value)) = self.0.next() { if value.defn.len() > 1 { for (def, idx) in value.defn.iter().zip(1..) { println!("{}({}) {}", key.as_str(), idx, def.replace('\n', " ")); } } else { println!("{} {}", key.as_str(), value.defn[0].replace('\n', " ")); } } } } type DictKey = BString; #[derive(Clone, Debug, Default)] pub struct DictValue { defn: Vec<&'static str>, note: Option<&'static str>, synonym: Option<&'static str>, }