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
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::io;
pub enum ExtractFormat<'a> {
Binary(Box<&'a mut dyn self::io::Read>),
HttpHeaders(Box<&'a dyn MapCarrier>),
TextMap(Box<&'a dyn MapCarrier>)
}
pub enum InjectFormat<'a> {
Binary(Box<&'a mut dyn self::io::Write>),
HttpHeaders(Box<&'a mut dyn MapCarrier>),
TextMap(Box<&'a mut dyn MapCarrier>)
}
pub trait MapCarrier {
fn items(&self) -> Vec<(&String, &String)>;
fn get(&self, key: &str) -> Option<String>;
fn set(&mut self, key: &str, value: &str);
}
impl MapCarrier for HashMap<String, String> {
fn items(&self) -> Vec<(&String, &String)> {
self.iter().collect()
}
fn get(&self, key: &str) -> Option<String> {
self.get(key).cloned()
}
fn set(&mut self, key: &str, value: &str) {
self.insert(String::from(key), String::from(value));
}
}
impl MapCarrier for BTreeMap<String, String> {
fn items(&self) -> Vec<(&String, &String)> {
self.iter().collect()
}
fn get(&self, key: &str) -> Option<String> {
self.get(key).cloned()
}
fn set(&mut self, key: &str, value: &str) {
self.insert(String::from(key), String::from(value));
}
}
#[cfg(test)]
mod tests {
mod tree_map {
use std::collections::BTreeMap;
use super::super::MapCarrier;
#[test]
fn extract_keys() {
let mut tree: BTreeMap<String, String> = BTreeMap::new();
tree.insert(String::from("aa"), String::from("d"));
assert_eq!(tree.get("aa").unwrap(), "d");
}
#[test]
fn find_keys() {
let mut tree: BTreeMap<String, String> = BTreeMap::new();
tree.insert(String::from("aa"), String::from("d"));
tree.insert(String::from("ab"), String::from("e"));
tree.insert(String::from("bc"), String::from("f"));
let mut items = vec![];
for (key, value) in tree.items() {
if key.starts_with("a") {
items.push((key.clone(), value.clone()));
}
}
items.sort();
assert_eq!(items, [
(String::from("aa"), String::from("d")),
(String::from("ab"), String::from("e"))
]);
}
#[test]
fn inject_keys() {
let mut tree: BTreeMap<String, String> = BTreeMap::new();
tree.set("a", "d");
tree.set("b", "e");
tree.set("c", "f");
assert_eq!("d", tree.get("a").unwrap());
assert_eq!("e", tree.get("b").unwrap());
assert_eq!("f", tree.get("c").unwrap());
}
}
mod hash_map {
use std::collections::HashMap;
use super::super::MapCarrier;
#[test]
fn extract_keys() {
let mut map: HashMap<String, String> = HashMap::new();
map.insert(String::from("aa"), String::from("d"));
assert_eq!(map.get("aa").unwrap(), "d");
}
#[test]
fn find_keys() {
let mut map: HashMap<String, String> = HashMap::new();
map.insert(String::from("aa"), String::from("d"));
map.insert(String::from("ab"), String::from("e"));
map.insert(String::from("bc"), String::from("f"));
let mut items = vec![];
for (key, value) in map.items() {
if key.starts_with("a") {
items.push((key.clone(), value.clone()));
}
}
items.sort();
assert_eq!(items, [
(String::from("aa"), String::from("d")),
(String::from("ab"), String::from("e"))
]);
}
#[test]
fn inject_keys() {
let mut map: HashMap<String, String> = HashMap::new();
map.set("a", "d");
map.set("b", "e");
map.set("c", "f");
assert_eq!("d", map.get("a").unwrap());
assert_eq!("e", map.get("b").unwrap());
assert_eq!("f", map.get("c").unwrap());
}
}
}