diff options
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..fc8ee03 --- /dev/null +++ b/src/config.rs | |||
@@ -0,0 +1,44 @@ | |||
1 | extern crate yaml_rust; | ||
2 | use self::yaml_rust::YamlLoader; | ||
3 | |||
4 | use std::collections::HashMap; | ||
5 | use std::fs::File; | ||
6 | use std::io::prelude::*; | ||
7 | |||
8 | pub struct SectionConfig { | ||
9 | pub pattern: String, | ||
10 | pub groups: Vec<String>, | ||
11 | } | ||
12 | |||
13 | pub struct Config { | ||
14 | pub sections: HashMap<String, SectionConfig>, | ||
15 | } | ||
16 | |||
17 | impl Config { | ||
18 | pub fn from_file(p: &str) -> Self { | ||
19 | let mut f = File::open(p).unwrap(); | ||
20 | let mut buf = String::new(); | ||
21 | f.read_to_string(&mut buf).unwrap(); | ||
22 | let docs = YamlLoader::load_from_str(&buf).unwrap(); | ||
23 | |||
24 | let doc = &docs[0]; | ||
25 | |||
26 | println!("{:?}", doc); | ||
27 | |||
28 | let mut sections: HashMap<String, SectionConfig> = HashMap::new(); | ||
29 | for (name, entry) in doc["sections"].as_hash().unwrap() { | ||
30 | sections.insert(name.as_str().unwrap().into(), | ||
31 | SectionConfig { | ||
32 | pattern: entry["pattern"].as_str().unwrap().into(), | ||
33 | groups: entry["groups"] | ||
34 | .as_vec() | ||
35 | .unwrap() | ||
36 | .into_iter() | ||
37 | .map(|v| v.as_str().unwrap().into()) | ||
38 | .collect(), | ||
39 | }); | ||
40 | } | ||
41 | |||
42 | Config { sections: sections } | ||
43 | } | ||
44 | } | ||