diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..1324f70 --- /dev/null +++ b/src/lib.rs | |||
@@ -0,0 +1,83 @@ | |||
1 | pub mod consts; | ||
2 | pub mod lex; | ||
3 | pub mod parse; | ||
4 | mod utils; | ||
5 | |||
6 | use std::fmt; | ||
7 | |||
8 | use radix_trie::{Trie, TrieCommon}; | ||
9 | |||
10 | pub struct Dict { | ||
11 | inner: Trie<DictKey, DictValue>, | ||
12 | } | ||
13 | |||
14 | impl Dict { | ||
15 | fn new() -> Self { | ||
16 | Self { inner: Trie::new() } | ||
17 | } | ||
18 | |||
19 | fn insert(&mut self, entry: DictKey, value: DictValue) { | ||
20 | self.inner.map_with_default( | ||
21 | entry, | ||
22 | |dict_value| { | ||
23 | // TODO: this only merges defns, not notes/syns | ||
24 | for v in value.defn.iter() { | ||
25 | dict_value.defn.push(v); | ||
26 | } | ||
27 | }, | ||
28 | value.clone(), | ||
29 | ); | ||
30 | } | ||
31 | |||
32 | pub fn search<'dict, 'search>( | ||
33 | &'dict self, | ||
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 | } | ||
42 | } | ||
43 | |||
44 | pub enum SearchResults<'dict, 'search> { | ||
45 | Empty, | ||
46 | Hit(radix_trie::iter::Iter<'dict, &'search str, DictValue>), | ||
47 | } | ||
48 | |||
49 | impl<'dict, 'search> SearchResults<'dict, 'search> { | ||
50 | // mutable ref here to advance the iterator present in Self::Hit | ||
51 | pub fn print(&mut self) { | ||
52 | match self { | ||
53 | Self::Hit(results) => { | ||
54 | while let Some((key, value)) = results.next() { | ||
55 | if value.defn.len() > 1 { | ||
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 | } | ||
70 | } | ||
71 | Self::Empty => (), | ||
72 | } | ||
73 | } | ||
74 | } | ||
75 | |||
76 | type DictKey = &'static str; | ||
77 | |||
78 | #[derive(Clone)] | ||
79 | pub struct DictValue { | ||
80 | defn: Vec<&'static str>, | ||
81 | note: Option<&'static str>, | ||
82 | synonym: Option<&'static str>, | ||
83 | } | ||