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, } pub struct Config { pub sections: HashMap, } 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 = 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 } } }