Adapt libimagrt config parsing to toml 0.4

This commit is contained in:
Matthias Beyer 2017-05-03 20:21:54 +02:00
parent 537b2b5a00
commit c1596d9c61

View file

@ -21,10 +21,11 @@ use std::path::PathBuf;
use std::result::Result as RResult; use std::result::Result as RResult;
use std::ops::Deref; use std::ops::Deref;
use toml::{Parser, Value}; use toml::Value;
generate_error_module!( generate_error_module!(
generate_error_types!(ConfigError, ConfigErrorKind, generate_error_types!(ConfigError, ConfigErrorKind,
TOMLParserError => "TOML Parsing error",
NoConfigFileFound => "No config file found", NoConfigFileFound => "No config file found",
ConfigOverrideError => "Config override error", ConfigOverrideError => "Config override error",
@ -119,7 +120,9 @@ impl Configuration {
use libimagutil::key_value_split::*; use libimagutil::key_value_split::*;
use libimagutil::iter::*; use libimagutil::iter::*;
use self::error::ConfigErrorKind as CEK; use self::error::ConfigErrorKind as CEK;
use self::error::MapErrInto;
use libimagerror::into::IntoError; use libimagerror::into::IntoError;
use libimagstore::toml_ext::TomlValueExt;
v.into_iter() v.into_iter()
.map(|s| { debug!("Trying to process '{}'", s); s }) .map(|s| { debug!("Trying to process '{}'", s); s })
@ -133,19 +136,21 @@ impl Configuration {
} }
}) })
.map(|(k, v)| { .map(|(k, v)| {
match self.config.lookup_mut(&k[..]) { self.config
.read(&k[..])
.map_err_into(CEK::TOMLParserError)
.map(|toml| match toml {
Some(value) => { Some(value) => {
match into_value(value, v) { match into_value(value, v) {
Some(v) => { Some(v) => {
*value = v; info!("Successfully overridden: {} = {}", k, v);
info!("Successfully overridden: {} = {}", k, value); Ok(v)
Ok(())
}, },
None => Err(CEK::ConfigOverrideTypeNotMatching.into_error()), None => Err(CEK::ConfigOverrideTypeNotMatching.into_error()),
} }
}, },
None => Err(CEK::ConfigOverrideKeyNotAvailable.into_error()), None => Err(CEK::ConfigOverrideKeyNotAvailable.into_error()),
} })
}) })
.fold_result(|i| i) .fold_result(|i| i)
.map_err(Box::new) .map_err(Box::new)
@ -158,19 +163,19 @@ impl Configuration {
/// Returns None if string cannot be converted. /// Returns None if string cannot be converted.
/// ///
/// Arrays and Tables are not supported and will yield `None`. /// Arrays and Tables are not supported and will yield `None`.
fn into_value(value: &Value, s: String) -> Option<Value> { fn into_value(value: Value, s: String) -> Option<Value> {
use std::str::FromStr; use std::str::FromStr;
match value { match value {
&Value::String(_) => Some(Value::String(s)), Value::String(_) => Some(Value::String(s)),
&Value::Integer(_) => FromStr::from_str(&s[..]).ok().map(|i| Value::Integer(i)), Value::Integer(_) => FromStr::from_str(&s[..]).ok().map(|i| Value::Integer(i)),
&Value::Float(_) => FromStr::from_str(&s[..]).ok().map(|f| Value::Float(f)), Value::Float(_) => FromStr::from_str(&s[..]).ok().map(|f| Value::Float(f)),
&Value::Boolean(_) => { Value::Boolean(_) => {
if s == "true" { Some(Value::Boolean(true)) } if s == "true" { Some(Value::Boolean(true)) }
else if s == "false" { Some(Value::Boolean(false)) } else if s == "false" { Some(Value::Boolean(false)) }
else { None } else { None }
} }
&Value::Datetime(_) => Some(Value::Datetime(s)), Value::Datetime(_) => Value::try_from(s).ok(),
_ => None, _ => None,
} }
} }
@ -223,6 +228,7 @@ fn fetch_config(rtp: &PathBuf) -> Result<Value> {
use itertools::Itertools; use itertools::Itertools;
use libimagutil::variants::generate_variants as gen_vars; use libimagutil::variants::generate_variants as gen_vars;
use libimagerror::trace::trace_error;
let variants = vec!["config", "config.toml", "imagrc", "imagrc.toml"]; let variants = vec!["config", "config.toml", "imagrc", "imagrc.toml"];
let modifier = |base: &PathBuf, v: &'static str| { let modifier = |base: &PathBuf, v: &'static str| {
@ -255,18 +261,13 @@ fn fetch_config(rtp: &PathBuf) -> Result<Value> {
s s
}; };
let mut parser = Parser::new(&content[..]); match ::toml::de::from_str(&content[..]) {
let res = parser.parse(); Ok(res) => res,
Err(e) => {
if res.is_none() {
write!(stderr(), "Config file parser error:").ok(); write!(stderr(), "Config file parser error:").ok();
for error in parser.errors { trace_error(&e);
write!(stderr(), "At [{}][{}] <> {}", error.lo, error.hi, error).ok();
write!(stderr(), "in: '{}'", &content[error.lo..error.hi]).ok();
}
None None
} else { }
res
} }
}) })
.filter(|loaded| loaded.is_some()) .filter(|loaded| loaded.is_some())