Use Config object to handle configuration
This commit is contained in:
parent
c8e3040f90
commit
9065159c32
12
src/main.rs
12
src/main.rs
|
@ -39,19 +39,13 @@ fn main() {
|
|||
|
||||
let config = matches.value_of("config").unwrap();
|
||||
let config = fs::read_to_string(config).expect(&format!("cannot read {}", config));
|
||||
let mut parsed_map = slice::parse_into_indexmap(&config).expect("Unable to parse.");
|
||||
let mut parsed = slice::parse(&config).expect("Unable to parse.");
|
||||
|
||||
let profile = matches.value_of("profile").unwrap();
|
||||
let profile = fs::read_to_string(profile).expect(&format!("cannot read {}", profile));
|
||||
let profile_map: BTreeMap<String, String> = serde_yaml::from_str(&profile).unwrap();
|
||||
|
||||
for (k, v) in profile_map {
|
||||
if let Some(val) = parsed_map.get_mut(&k) {
|
||||
*val = v;
|
||||
}
|
||||
}
|
||||
parsed.update(profile_map);
|
||||
|
||||
for (k, v) in &parsed_map {
|
||||
println!("{} = {}", k, v);
|
||||
}
|
||||
parsed.print();
|
||||
}
|
||||
|
|
|
@ -1,27 +1,84 @@
|
|||
use std::collections::BTreeMap;
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use pest::error::Error;
|
||||
use pest::Parser;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[grammar = "grammars/slice.pest"]
|
||||
pub struct SliceParser;
|
||||
struct SliceParser;
|
||||
|
||||
pub fn parse_into_indexmap(config: &String) -> Result<IndexMap<String, String>, Error<Rule>> {
|
||||
let config = SliceParser::parse(Rule::FILE, &config)?.next().unwrap();
|
||||
/// zeroc configuration file.
|
||||
pub struct Config {
|
||||
map: IndexMap<String, String>,
|
||||
}
|
||||
|
||||
let mut map = IndexMap::new();
|
||||
for line in config.into_inner() {
|
||||
/// Create Config object from a string.
|
||||
pub fn parse(s: &String) -> Result<Config, Error<Rule>> {
|
||||
let parsed = SliceParser::parse(Rule::FILE, &s)?.next().unwrap();
|
||||
|
||||
let mut config = Config {
|
||||
map: IndexMap::new(),
|
||||
};
|
||||
|
||||
for line in parsed.into_inner() {
|
||||
match line.as_rule() {
|
||||
Rule::PROPERTY => {
|
||||
let mut inner_rules = line.into_inner();
|
||||
let name = inner_rules.next().unwrap().as_str().to_string();
|
||||
let value = inner_rules.next().unwrap().as_str().to_string();
|
||||
map.insert(name, value);
|
||||
config.map.insert(name, value);
|
||||
}
|
||||
Rule::EOI => (),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(map)
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
impl Config {
|
||||
/// Overwrite Config keys using the ones from map.
|
||||
pub fn update(&mut self, map: BTreeMap<String, String>) {
|
||||
for (k, v) in map {
|
||||
if let Some(val) = self.map.get_mut(&k) {
|
||||
*val = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print(&self) {
|
||||
for (k, v) in &self.map {
|
||||
println!("{} = {}", k, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_from_string() {
|
||||
let conf = "
|
||||
Author.name = eriol
|
||||
Author.like = rust, python
|
||||
".to_string();
|
||||
|
||||
let c = parse(&conf).unwrap();
|
||||
|
||||
assert_eq!(c.map["Author.name"], "eriol");
|
||||
assert_eq!(c.map["Author.like"], "rust, python");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update() {
|
||||
let conf = "
|
||||
Author.name = eriol
|
||||
Author.like = rust, python
|
||||
".to_string();
|
||||
|
||||
let mut m = BTreeMap::new();
|
||||
m.insert("Author.like".to_string(), "rust, python, c++".to_string());
|
||||
|
||||
let mut c = parse(&conf).unwrap();
|
||||
c.update(m);
|
||||
|
||||
assert_eq!(c.map["Author.like"], "rust, python, c++");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue