1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use super::{InlineElementContainer, LE};
use derive_more::Constructor;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::hash::{Hash, Hasher};
pub type Term = InlineElementContainer;
pub type Definition = InlineElementContainer;
#[derive(Constructor, Clone, Debug, Serialize, Deserialize)]
pub struct TermAndDefinitions {
pub term: Term,
pub definitions: Vec<Definition>,
}
impl Eq for TermAndDefinitions {}
impl PartialEq for TermAndDefinitions {
fn eq(&self, other: &Self) -> bool {
self.term.to_string() == other.term.to_string()
}
}
impl PartialEq<InlineElementContainer> for TermAndDefinitions {
fn eq(&self, other: &InlineElementContainer) -> bool {
self.term.to_string() == other.to_string()
}
}
impl PartialEq<String> for TermAndDefinitions {
fn eq(&self, other: &String) -> bool {
&self.term.to_string() == other
}
}
impl PartialEq<&str> for TermAndDefinitions {
fn eq(&self, other: &&str) -> bool {
&self.term.to_string() == other
}
}
impl Hash for TermAndDefinitions {
fn hash<H: Hasher>(&self, state: &mut H) {
self.term.to_string().hash(state);
}
}
#[derive(
Constructor, Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize,
)]
pub struct DefinitionList {
terms_and_definitions: HashSet<TermAndDefinitions>,
}
impl DefinitionList {
pub fn get(&self, term: &str) -> Option<&TermAndDefinitions> {
self.terms_and_definitions.get(&TermAndDefinitions {
term: Term::from(LE::from(term)),
definitions: vec![],
})
}
pub fn iter(&self) -> impl Iterator<Item = &TermAndDefinitions> {
self.terms_and_definitions.iter()
}
pub fn terms(&self) -> impl Iterator<Item = &Term> {
self.terms_and_definitions.iter().map(|td| &td.term)
}
pub fn defs_for_term(
&self,
term: &str,
) -> Option<impl Iterator<Item = &Definition>> {
self.get(term).map(|td| td.definitions.iter())
}
}
impl From<Vec<TermAndDefinitions>> for DefinitionList {
fn from(mut term_and_definitions: Vec<TermAndDefinitions>) -> Self {
let mut dl = Self::default();
for td in term_and_definitions.drain(..) {
dl.terms_and_definitions.insert(td);
}
dl
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::elements::{
DecoratedText, DecoratedTextContent, Decoration, InlineElement,
};
#[test]
fn term_and_definitions_should_equal_other_instance_if_names_are_same() {
let td1 = TermAndDefinitions::new(Term::from(LE::from("term")), vec![]);
let td2 = TermAndDefinitions::new(
Term::from(LE::from("term")),
vec![Definition::from(LE::from("definition"))],
);
assert_eq!(td1, td2);
}
#[test]
fn term_and_definitions_should_equal_lc_string_if_name_equals_string() {
let td = TermAndDefinitions::new(Term::from(LE::from("term")), vec![]);
let other = Term::from(LE::from("term"));
assert_eq!(td, other);
}
#[test]
fn term_and_definitions_should_equal_string_if_name_equals_string() {
let td = TermAndDefinitions::new(Term::from(LE::from("term")), vec![]);
let other = String::from("term");
assert_eq!(td, other);
}
#[test]
fn term_and_definitions_should_equal_str_slice_if_name_equals_str_slice() {
let td = TermAndDefinitions::new(Term::from(LE::from("term")), vec![]);
let other = "term";
assert_eq!(td, other);
}
#[test]
fn term_and_definitions_should_hash_using_its_name() {
let td1 = TermAndDefinitions::new(Term::from(LE::from("term")), vec![]);
let td2 = TermAndDefinitions::new(
Term::from(LE::from("term")),
vec![Term::from(LE::from("definition"))],
);
let mut hs = HashSet::new();
hs.insert(td1);
assert_eq!(hs.len(), 1);
assert!(hs.get(&td2).is_some());
}
#[test]
fn definition_list_should_be_able_to_get_term_and_definitions_by_term_name()
{
let dl = DefinitionList::from(vec![TermAndDefinitions::new(
Term::from(LE::from("term")),
vec![Definition::from(LE::from("definition"))],
)]);
assert!(dl.get("term").is_some());
}
#[test]
fn definition_list_should_be_able_to_iterate_through_terms() {
let dl = DefinitionList::from(vec![
TermAndDefinitions::new(
Term::from(LE::from("term1")),
vec![Definition::from(LE::from("definition"))],
),
TermAndDefinitions::new(Term::from(LE::from("term2")), vec![]),
]);
let term_names =
dl.terms().map(|t| t.to_string()).collect::<Vec<String>>();
assert_eq!(term_names.len(), 2);
assert!(term_names.contains(&"term1".to_string()));
assert!(term_names.contains(&"term2".to_string()));
}
#[test]
fn definition_list_should_be_able_to_iterate_through_definitions_for_term()
{
let dl = DefinitionList::from(vec![
TermAndDefinitions::new(
Term::from(LE::from("term1")),
vec![Definition::from(LE::from("definition"))],
),
TermAndDefinitions::new(Term::from(LE::from("term2")), vec![]),
]);
let defs = dl
.defs_for_term("term1")
.expect("Failed to find term")
.map(|d| d.to_string())
.collect::<Vec<String>>();
assert_eq!(defs.len(), 1);
assert!(defs.contains(&"definition".to_string()));
let defs = dl
.defs_for_term("term2")
.expect("Failed to find term")
.map(|d| d.to_string())
.collect::<Vec<String>>();
assert!(defs.is_empty());
assert!(dl.defs_for_term("term-unknown").is_none());
}
#[test]
fn definition_list_should_support_lookup_with_terms_containing_other_inline_elements(
) {
let dl = DefinitionList::from(vec![TermAndDefinitions::new(
Term::new(vec![
LE::from(InlineElement::DecoratedText(DecoratedText::new(
vec![LE::from(DecoratedTextContent::Text(
"term".to_string(),
))],
Decoration::Bold,
))),
LE::from(InlineElement::Text(" 1".to_string())),
]),
vec![Definition::from(LE::from("definition"))],
)]);
assert!(dl.get("term 1").is_some());
}
}