1
0
Fork 0

Show warnings for keys present only in profiles

This commit is contained in:
Daniele Tricoli 2018-12-08 18:25:39 +01:00
parent ee33f214bc
commit 4041f0d2ae
2 changed files with 35 additions and 7 deletions

View File

@ -42,6 +42,7 @@ fn main() {
.takes_value(true) .takes_value(true)
.value_name("FILE"), .value_name("FILE"),
) )
.arg(Arg::with_name("warning").help("Show warnings").short("w"))
.get_matches(); .get_matches();
// We can unwrap because config is required. // We can unwrap because config is required.
@ -70,7 +71,7 @@ fn main() {
config.update(profile); config.update(profile);
config.print(); config.print(matches.is_present("warning"));
} }
// Read the content of a file and return it as String. // Read the content of a file and return it as String.

View File

@ -4,11 +4,15 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::vec::Vec;
use colored::Colorize;
use indexmap::IndexMap; use indexmap::IndexMap;
use pest::error::Error; use pest::error::Error;
use pest::Parser; use pest::Parser;
const SEPARATOR: &str = "=";
#[derive(Parser)] #[derive(Parser)]
#[grammar = "grammars/slice.pest"] #[grammar = "grammars/slice.pest"]
struct SliceParser; struct SliceParser;
@ -16,6 +20,7 @@ struct SliceParser;
/// zeroc configuration file. /// zeroc configuration file.
pub struct Config { pub struct Config {
map: IndexMap<String, String>, map: IndexMap<String, String>,
warnings: Vec<String>,
} }
/// Create Config object from a string. /// Create Config object from a string.
@ -24,6 +29,7 @@ pub fn parse(s: &String) -> Result<Config, Error<Rule>> {
let mut config = Config { let mut config = Config {
map: IndexMap::new(), map: IndexMap::new(),
warnings: Vec::new(),
}; };
for line in parsed.into_inner() { for line in parsed.into_inner() {
@ -44,18 +50,34 @@ pub fn parse(s: &String) -> Result<Config, Error<Rule>> {
impl Config { impl Config {
/// Overwrite Config keys using the ones from map. /// 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<String, String>) { pub fn update(&mut self, map: BTreeMap<String, String>) {
for (k, v) in map { for (k, v) in map {
if let Some(val) = self.map.get_mut(&k) { if let Some(val) = self.map.get_mut(&k) {
*val = v; *val = v;
} else {
self.warnings.push(format!(
"{}: {} key is not present in config file.",
"Warning".yellow(),
k
));
} }
} }
} }
/// Print Config to stdout. /// 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 { for (k, v) in &self.map {
println!("{} = {}", k, v); println!("{} {} {}", k, SEPARATOR, v);
} }
} }
} }
@ -83,13 +105,18 @@ fn update() {
" "
.to_string(); .to_string();
let mut m = BTreeMap::new(); let mut profile = BTreeMap::new();
m.insert("Author.like".to_string(), "rust, python, c++".to_string()); profile.insert("Author.like".to_string(), "rust, python, c++".to_string());
m.insert("Author.web".to_string(), "example.org:8080".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(); 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.like"], "rust, python, c++");
assert_eq!(c.map["Author.web"], "example.org:8080"); 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.");
} }