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, expected: Vec<&'static 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]; 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(), }); } for ex in &expected { if !sections.contains_key(&ex.to_string()) { panic!("config: section '{}' not found", ex); } } { let traits = §ions["traits"]; if !traits.groups.contains(&"indexed_raw".to_string()) { panic!("config: no group 'indexed_raw' found in section 'traits'"); } if !traits.groups.contains(&"official_raw".to_string()) { panic!("config: no group 'official_raw' found in section 'traits'"); } let tags = §ions["tags"]; if !tags.groups.contains(&"tags_raw".to_string()) { panic!("config: no group 'tags_raw' found in section 'tags'"); } } Config { sections: sections } } }