aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs70
-rw-r--r--src/main.rs1
2 files changed, 23 insertions, 48 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 1324f70..b286cb5 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -3,9 +3,10 @@ pub mod lex;
3pub mod parse; 3pub mod parse;
4mod utils; 4mod utils;
5 5
6use std::fmt; 6use qp_trie::{
7 7 wrapper::{BStr, BString},
8use radix_trie::{Trie, TrieCommon}; 8 Iter, Trie,
9};
9 10
10pub struct Dict { 11pub struct Dict {
11 inner: Trie<DictKey, DictValue>, 12 inner: Trie<DictKey, DictValue>,
@@ -16,66 +17,39 @@ impl Dict {
16 Self { inner: Trie::new() } 17 Self { inner: Trie::new() }
17 } 18 }
18 19
19 fn insert(&mut self, entry: DictKey, value: DictValue) { 20 fn insert(&mut self, entry: &'static str, value: DictValue) {
20 self.inner.map_with_default( 21 let key = BString::from(entry);
21 entry, 22 let dict_value = self.inner.entry(key).or_insert(Default::default());
22 |dict_value| { 23 for v in value.defn.into_iter() {
23 // TODO: this only merges defns, not notes/syns 24 dict_value.defn.push(v);
24 for v in value.defn.iter() { 25 }
25 dict_value.defn.push(v);
26 }
27 },
28 value.clone(),
29 );
30 } 26 }
31 27
32 pub fn search<'dict, 'search>( 28 pub fn search<'a, 's>(&'a self, search_term: &'s str) -> SearchResults<'a> {
33 &'dict self, 29 SearchResults(self.inner.iter_prefix_str(search_term))
34 search_term: &'search str,
35 ) -> SearchResults<'dict, 'search> {
36 self.inner
37 .subtrie(search_term)
38 .map_or(SearchResults::Empty, |subtrie| {
39 SearchResults::Hit(subtrie.iter())
40 })
41 } 30 }
42} 31}
43 32
44pub enum SearchResults<'dict, 'search> { 33pub struct SearchResults<'a>(Iter<'a, DictKey, DictValue>);
45 Empty,
46 Hit(radix_trie::iter::Iter<'dict, &'search str, DictValue>),
47}
48 34
49impl<'dict, 'search> SearchResults<'dict, 'search> { 35impl<'a> SearchResults<'a> {
50 // mutable ref here to advance the iterator present in Self::Hit 36 // mutable ref here to advance the iterator present in Self::Hit
51 pub fn print(&mut self) { 37 pub fn print(&mut self) {
52 match self { 38 while let Some((key, value)) = self.0.next() {
53 Self::Hit(results) => { 39 if value.defn.len() > 1 {
54 while let Some((key, value)) = results.next() { 40 for (def, idx) in value.defn.iter().zip(1..) {
55 if value.defn.len() > 1 { 41 println!("{}({}) {}", key.as_str(), idx, def.replace('\n', " "));
56 for (def, idx) in value.defn.iter().zip(1..) {
57 println!("{}({}) {}", key, idx, def.replace('\n', " "));
58 }
59 } else {
60 println!("{} {}", key, value.defn[0].replace('\n', " "));
61 }
62
63 // if let Some(note) = value.note {
64 // print!("\t{}", note);
65 // }
66 // if let Some(synonym) = value.synonym {
67 // print!("\t{}", synonym);
68 // }
69 } 42 }
43 } else {
44 println!("{} {}", key.as_str(), value.defn[0].replace('\n', " "));
70 } 45 }
71 Self::Empty => (),
72 } 46 }
73 } 47 }
74} 48}
75 49
76type DictKey = &'static str; 50type DictKey = BString;
77 51
78#[derive(Clone)] 52#[derive(Clone, Debug, Default)]
79pub struct DictValue { 53pub struct DictValue {
80 defn: Vec<&'static str>, 54 defn: Vec<&'static str>,
81 note: Option<&'static str>, 55 note: Option<&'static str>,
diff --git a/src/main.rs b/src/main.rs
index 9def90c..ffa7757 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -9,6 +9,7 @@ fn main() {
9 .filter_map(Result::ok) 9 .filter_map(Result::ok)
10 .fold(ParseState::new(), ParseState::advance) 10 .fold(ParseState::new(), ParseState::advance)
11 .finish() 11 .finish()
12 //.children();
12 .search(search_term.to_ascii_uppercase().as_str()) 13 .search(search_term.to_ascii_uppercase().as_str())
13 .print() 14 .print()
14} 15}