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
use pest::Parser; use std::collections::BTreeMap; use std::path::Path; use std::fs::File; use std::io::prelude::*; #[derive(Parser)] #[grammar = "iceconfig.pest"] pub struct PropertyParser; #[derive(Clone)] pub struct Properties { properties: BTreeMap<String, String> } impl Properties { pub fn new() -> Properties { Properties { properties: BTreeMap::new() } } pub fn get(&self, key: &str) -> Option<&String> { return self.properties.get(key) } pub fn has(&self, key: &str) -> bool { return self.properties.contains_key(key) } pub fn load(&mut self, config_file: &str) -> Result<(), Box<dyn std::error::Error>> { let mut content = String::new(); let mut file = File::open(Path::new(&config_file))?; file.read_to_string(&mut content)?; let mut pairs = PropertyParser::parse(Rule::iceconfig, &content).unwrap(); let mut key = ""; let config = pairs.next().unwrap(); if config.as_rule() == Rule::iceconfig { for pair in config.into_inner() { match pair.as_rule() { Rule::property_key => { key = pair.as_str(); } Rule::property_value => { self.properties.insert(String::from(key), String::from(pair.as_str())); } _ => {} } } } Ok(()) } }