diff --git a/src/main.rs b/src/main.rs index 8a1a54e..9e99ed4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,6 +42,7 @@ fn main() { .takes_value(true) .value_name("FILE"), ) + .arg(Arg::with_name("warning").help("Show warnings").short("w")) .get_matches(); // We can unwrap because config is required. @@ -70,7 +71,7 @@ fn main() { config.update(profile); - config.print(); + config.print(matches.is_present("warning")); } // Read the content of a file and return it as String. diff --git a/src/parsers/slice.rs b/src/parsers/slice.rs index bf64a20..6186342 100644 --- a/src/parsers/slice.rs +++ b/src/parsers/slice.rs @@ -4,11 +4,15 @@ // license that can be found in the LICENSE file. use std::collections::BTreeMap; +use std::vec::Vec; +use colored::Colorize; use indexmap::IndexMap; use pest::error::Error; use pest::Parser; +const SEPARATOR: &str = "="; + #[derive(Parser)] #[grammar = "grammars/slice.pest"] struct SliceParser; @@ -16,6 +20,7 @@ struct SliceParser; /// zeroc configuration file. pub struct Config { map: IndexMap, + warnings: Vec, } /// Create Config object from a string. @@ -24,6 +29,7 @@ pub fn parse(s: &String) -> Result> { let mut config = Config { map: IndexMap::new(), + warnings: Vec::new(), }; for line in parsed.into_inner() { @@ -44,18 +50,34 @@ pub fn parse(s: &String) -> Result> { impl Config { /// Overwrite Config keys using the ones from map. + /// + /// If a key is only in the profile, it will not be added to the + /// configuration file, and a warning will be saved. pub fn update(&mut self, map: BTreeMap) { for (k, v) in map { if let Some(val) = self.map.get_mut(&k) { *val = v; + } else { + self.warnings.push(format!( + "{}: {} key is not present in config file.", + "Warning".yellow(), + k + )); } } } /// Print Config to stdout. - pub fn print(&self) { + /// + /// If show_warnings is true, warnings will be printed to stderr. + pub fn print(&self, show_warnings: bool) { + if show_warnings { + for warning in &self.warnings { + eprintln!("{}", warning); + } + } for (k, v) in &self.map { - println!("{} = {}", k, v); + println!("{} {} {}", k, SEPARATOR, v); } } } @@ -83,13 +105,18 @@ fn update() { " .to_string(); - let mut m = BTreeMap::new(); - m.insert("Author.like".to_string(), "rust, python, c++".to_string()); - m.insert("Author.web".to_string(), "example.org:8080".to_string()); + let mut profile = BTreeMap::new(); + profile.insert("Author.like".to_string(), "rust, python, c++".to_string()); + profile.insert("Author.web".to_string(), "example.org:8080".to_string()); + profile.insert("Only.here".to_string(), "value".to_string()); let mut c = parse(&conf).unwrap(); - c.update(m); + c.update(profile); assert_eq!(c.map["Author.like"], "rust, python, c++"); assert_eq!(c.map["Author.web"], "example.org:8080"); + + assert_eq!(c.warnings.len(), 1); + let m: Vec<&str> = c.warnings.first().unwrap().split(":").collect(); + assert_eq!(m[1].trim(), "Only.here key is not present in config file."); }