diff options
author | Akshay <[email protected]> | 2023-04-27 17:32:07 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2023-04-27 17:32:07 +0100 |
commit | 2e57b250c6e931c13ed9ec1870e133c59151422b (patch) | |
tree | 3642b89cd7fadd9dad0dc63438160ecb6fcdbf6d /src | |
parent | 7f9f70d0c7f93f11fa896e5544a5da59319a56f9 (diff) |
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 70 | ||||
-rw-r--r-- | src/main.rs | 1 |
2 files changed, 23 insertions, 48 deletions
@@ -3,9 +3,10 @@ pub mod lex; | |||
3 | pub mod parse; | 3 | pub mod parse; |
4 | mod utils; | 4 | mod utils; |
5 | 5 | ||
6 | use std::fmt; | 6 | use qp_trie::{ |
7 | 7 | wrapper::{BStr, BString}, | |
8 | use radix_trie::{Trie, TrieCommon}; | 8 | Iter, Trie, |
9 | }; | ||
9 | 10 | ||
10 | pub struct Dict { | 11 | pub 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 | ||
44 | pub enum SearchResults<'dict, 'search> { | 33 | pub struct SearchResults<'a>(Iter<'a, DictKey, DictValue>); |
45 | Empty, | ||
46 | Hit(radix_trie::iter::Iter<'dict, &'search str, DictValue>), | ||
47 | } | ||
48 | 34 | ||
49 | impl<'dict, 'search> SearchResults<'dict, 'search> { | 35 | impl<'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 | ||
76 | type DictKey = &'static str; | 50 | type DictKey = BString; |
77 | 51 | ||
78 | #[derive(Clone)] | 52 | #[derive(Clone, Debug, Default)] |
79 | pub struct DictValue { | 53 | pub 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 | } |