Move parsing into slice module
This commit is contained in:
parent
fdaf25a0ed
commit
c8e3040f90
29
src/main.rs
29
src/main.rs
|
@ -11,8 +11,6 @@ use std::collections::BTreeMap;
|
|||
use std::fs;
|
||||
|
||||
use clap::{App, Arg};
|
||||
use indexmap::IndexMap;
|
||||
use pest::Parser;
|
||||
|
||||
use crate::parsers::slice;
|
||||
|
||||
|
@ -40,31 +38,14 @@ fn main() {
|
|||
).get_matches();
|
||||
|
||||
let config = matches.value_of("config").unwrap();
|
||||
let unparsed_config = fs::read_to_string(config).expect(&format!("cannot read {}", config));
|
||||
|
||||
let parsed_config = slice::SliceParser::parse(slice::Rule::FILE, &unparsed_config)
|
||||
.expect("Not able to parse")
|
||||
.next()
|
||||
.unwrap();
|
||||
|
||||
let mut parsed_map = IndexMap::new();
|
||||
for line in parsed_config.into_inner() {
|
||||
match line.as_rule() {
|
||||
slice::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();
|
||||
parsed_map.insert(name, value);
|
||||
}
|
||||
slice::Rule::EOI => (),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
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 profile = matches.value_of("profile").unwrap();
|
||||
let profile = fs::read_to_string(profile).expect(&format!("cannot read {}", profile));
|
||||
let deserialized_map: BTreeMap<String, String> = serde_yaml::from_str(&profile).unwrap();
|
||||
for (k, v) in deserialized_map {
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -1,3 +1,27 @@
|
|||
use indexmap::IndexMap;
|
||||
use pest::error::Error;
|
||||
use pest::Parser;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[grammar = "grammars/slice.pest"]
|
||||
pub struct SliceParser;
|
||||
|
||||
pub fn parse_into_indexmap(config: &String) -> Result<IndexMap<String, String>, Error<Rule>> {
|
||||
let config = SliceParser::parse(Rule::FILE, &config)?.next().unwrap();
|
||||
|
||||
let mut map = IndexMap::new();
|
||||
for line in config.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);
|
||||
}
|
||||
Rule::EOI => (),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(map)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue