aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
blob: fc8ee0333349fdfe4998d138b9d6a0d8debd53ef (plain)
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
extern crate yaml_rust;
use self::yaml_rust::YamlLoader;

use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::*;

pub struct SectionConfig {
    pub pattern: String,
    pub groups: Vec<String>,
}

pub struct Config {
    pub sections: HashMap<String, SectionConfig>,
}

impl Config {
    pub fn from_file(p: &str) -> Self {
        let mut f = File::open(p).unwrap();
        let mut buf = String::new();
        f.read_to_string(&mut buf).unwrap();
        let docs = YamlLoader::load_from_str(&buf).unwrap();

        let doc = &docs[0];

        println!("{:?}", doc);

        let mut sections: HashMap<String, SectionConfig> = HashMap::new();
        for (name, entry) in doc["sections"].as_hash().unwrap() {
            sections.insert(name.as_str().unwrap().into(),
                            SectionConfig {
                                pattern: entry["pattern"].as_str().unwrap().into(),
                                groups: entry["groups"]
                                    .as_vec()
                                    .unwrap()
                                    .into_iter()
                                    .map(|v| v.as_str().unwrap().into())
                                    .collect(),
                            });
        }

        Config { sections: sections }
    }
}