|
|
|
@ -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<String, String>, |
|
|
|
|
warnings: Vec<String>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Create Config object from a string.
|
|
|
|
@ -24,6 +29,7 @@ pub fn parse(s: &String) -> Result<Config, Error<Rule>> {
|
|
|
|
|
|
|
|
|
|
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<Config, Error<Rule>> {
|
|
|
|
|
|
|
|
|
|
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<String, String>) { |
|
|
|
|
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."); |
|
|
|
|
} |
|
|
|
|