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
#![deny(missing_docs)]
#![deny(missing_crate_level_docs)]
mod parser;
pub use parser::Property;
use std::collections::HashMap;
pub fn parse(input: &[u8]) -> Result<Vec<Property>, nom::Err<nom::error::Error<&[u8]>>> {
match parser::parser(input) {
Ok((_, v)) => Ok(v),
Err(e) => Err(e),
}
}
pub fn to_map(props: Vec<Property>) -> HashMap<String, String> {
let mut map = HashMap::with_capacity(props.len());
for prop in props.iter() {
map.insert(prop.key.clone(), prop.value.clone());
}
map
}
#[cfg(test)]
mod tests {
use super::parse;
use super::to_map;
#[test]
pub fn test_parse_simple() {
let v = br"
property=test
property2=test
";
let res = parse(v);
assert_eq!(2, res.unwrap().len());
}
#[test]
pub fn test_broken_parse() {
let v = br"=test
";
assert_eq!(true, parse(v).is_err());
}
#[test]
pub fn test_map_conversion() {
let v = br"
property=test
property2=test
property=t
";
let res = to_map(parse(v).unwrap());
assert_eq!(2, res.len());
assert_eq!("t", res.get("property").unwrap());
assert_eq!("test", res.get("property2").unwrap());
}
}