Merge pull request #955 from matthiasbeyer/libimagrt/cleanup

libimagrt: cleanup
This commit is contained in:
Matthias Beyer 2017-06-21 18:28:30 +02:00 committed by GitHub
commit 22a4dc0929
4 changed files with 32 additions and 38 deletions

View file

@ -22,6 +22,7 @@ xdg-basedir = "1.0"
itertools = "0.5" itertools = "0.5"
tempfile = "2.1" tempfile = "2.1"
ansi_term = "0.9" ansi_term = "0.9"
is-match = "0.1"
[dependencies.libimagstore] [dependencies.libimagstore]
path = "../libimagstore" path = "../libimagstore"

View file

@ -97,7 +97,6 @@ impl Configuration {
self.editor.as_ref() self.editor.as_ref()
} }
#[allow(dead_code)] // Why do I actually need this annotation on a pub function?
/// Get the underlying configuration TOML object /// Get the underlying configuration TOML object
pub fn config(&self) -> &Value { pub fn config(&self) -> &Value {
&self.config &self.config
@ -129,32 +128,28 @@ impl Configuration {
v.into_iter() v.into_iter()
.map(|s| { debug!("Trying to process '{}'", s); s }) .map(|s| { debug!("Trying to process '{}'", s); s })
.filter_map(|s| { .filter_map(|s| match s.into_kv() {
match s.into_kv() { Some(kv) => Some(kv.into()),
Some(kv) => Some(kv.into()), None => {
None => { warn!("Could split at '=' - will be ignore override");
warn!("Could split at '=' - will be ignore override"); None
None
}
} }
}) })
.map(|(k, v)| { .map(|(k, v)| self
self.config .config
.read(&k[..]) .read(&k[..])
.map_err_into(CEK::TOMLParserError) .map_err_into(CEK::TOMLParserError)
.map(|toml| match toml { .map(|toml| match toml {
Some(value) => { Some(value) => match into_value(value, v) {
match into_value(value, v) { Some(v) => {
Some(v) => { info!("Successfully overridden: {} = {}", k, v);
info!("Successfully overridden: {} = {}", k, v); Ok(v)
Ok(v)
},
None => Err(CEK::ConfigOverrideTypeNotMatching.into_error()),
}
}, },
None => Err(CEK::ConfigOverrideKeyNotAvailable.into_error()), None => Err(CEK::ConfigOverrideTypeNotMatching.into_error()),
}) },
}) None => Err(CEK::ConfigOverrideKeyNotAvailable.into_error()),
})
)
.fold_result(|i| i) .fold_result(|i| i)
.map_err(Box::new) .map_err(Box::new)
.map_err(|e| CEK::ConfigOverrideError.into_error_with_cause(e)) .map_err(|e| CEK::ConfigOverrideError.into_error_with_cause(e))
@ -170,9 +165,9 @@ 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(Value::Integer),
Value::Float(_) => FromStr::from_str(&s[..]).ok().map(|f| Value::Float(f)), Value::Float(_) => FromStr::from_str(&s[..]).ok().map(Value::Float),
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)) }
@ -195,7 +190,7 @@ impl Deref for Configuration {
fn get_verbosity(v: &Value) -> bool { fn get_verbosity(v: &Value) -> bool {
match *v { match *v {
Value::Table(ref t) => t.get("verbose") Value::Table(ref t) => t.get("verbose")
.map_or(false, |v| match *v { Value::Boolean(b) => b, _ => false, }), .map_or(false, |v| is_match!(v, &Value::Boolean(true))),
_ => false, _ => false,
} }
} }
@ -250,7 +245,7 @@ fn fetch_config(rtp: &PathBuf) -> Result<Value> {
].iter() ].iter()
.flatten() .flatten()
.filter(|path| path.exists() && path.is_file()) .filter(|path| path.exists() && path.is_file())
.map(|path| { .filter_map(|path| if path.exists() && path.is_file() {
debug!("Reading {:?}", path); debug!("Reading {:?}", path);
let content = { let content = {
let mut s = String::new(); let mut s = String::new();
@ -271,8 +266,9 @@ fn fetch_config(rtp: &PathBuf) -> Result<Value> {
.map_err(|e| REK::Instantiate.into_error_with_cause(e)); .map_err(|e| REK::Instantiate.into_error_with_cause(e));
Some(toml) Some(toml)
} else {
None
}) })
.filter_map(|x| x)
.filter(|loaded| loaded.is_ok()) .filter(|loaded| loaded.is_ok())
.map(|inner| Value::Table(inner.unwrap())) .map(|inner| Value::Table(inner.unwrap()))
.nth(0) .nth(0)

View file

@ -42,6 +42,7 @@ extern crate ansi_term;
extern crate clap; extern crate clap;
extern crate toml; extern crate toml;
#[macro_use] extern crate is_match;
extern crate libimagstore; extern crate libimagstore;
extern crate libimagutil; extern crate libimagutil;

View file

@ -310,9 +310,7 @@ impl<'a> Runtime<'a> {
debug!("Init logger with {}", lvl); debug!("Init logger with {}", lvl);
Box::new(ImagLogger::new(lvl.to_log_level().unwrap()).with_color(colored)) Box::new(ImagLogger::new(lvl.to_log_level().unwrap()).with_color(colored))
}) })
.map_err(|_| { .map_err(|e| panic!("Could not setup logger: {:?}", e))
panic!("Could not setup logger");
})
.ok(); .ok();
} }
} }
@ -396,11 +394,9 @@ impl<'a> Runtime<'a> {
self.cli() self.cli()
.value_of("editor") .value_of("editor")
.map(String::from) .map(String::from)
.or({ .or(match self.configuration {
match self.configuration { Some(ref c) => c.editor().cloned(),
Some(ref c) => c.editor().cloned(), _ => None,
_ => None,
}
}) })
.or(env::var("EDITOR").ok()) .or(env::var("EDITOR").ok())
.map(Command::new) .map(Command::new)