From d3d6e830d7faa47b73482c50ad29df018632d4dd Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 3 Jun 2017 14:43:02 +0200 Subject: [PATCH 1/8] Print error while panicing --- libimagrt/src/runtime.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/libimagrt/src/runtime.rs b/libimagrt/src/runtime.rs index 60ec08f3..e1c9a9d4 100644 --- a/libimagrt/src/runtime.rs +++ b/libimagrt/src/runtime.rs @@ -310,9 +310,7 @@ impl<'a> Runtime<'a> { debug!("Init logger with {}", lvl); Box::new(ImagLogger::new(lvl.to_log_level().unwrap()).with_color(colored)) }) - .map_err(|_| { - panic!("Could not setup logger"); - }) + .map_err(|e| panic!("Could not setup logger: {:?}", e)) .ok(); } } From 0f47c4afc7f06fa2b4e53fc0f224842439fb54cb Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 3 Jun 2017 14:43:46 +0200 Subject: [PATCH 2/8] Minify match --- libimagrt/src/runtime.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/libimagrt/src/runtime.rs b/libimagrt/src/runtime.rs index e1c9a9d4..e0df57fc 100644 --- a/libimagrt/src/runtime.rs +++ b/libimagrt/src/runtime.rs @@ -394,11 +394,9 @@ impl<'a> Runtime<'a> { self.cli() .value_of("editor") .map(String::from) - .or({ - match self.configuration { - Some(ref c) => c.editor().cloned(), - _ => None, - } + .or(match self.configuration { + Some(ref c) => c.editor().cloned(), + _ => None, }) .or(env::var("EDITOR").ok()) .map(Command::new) From 1e06b90eea54c07648362a967a92618758d3e2bd Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 3 Jun 2017 14:44:57 +0200 Subject: [PATCH 3/8] Try to remove annotation ...which was necessary some time ago --- libimagrt/src/configuration.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/libimagrt/src/configuration.rs b/libimagrt/src/configuration.rs index dc19dca4..0487fc94 100644 --- a/libimagrt/src/configuration.rs +++ b/libimagrt/src/configuration.rs @@ -97,7 +97,6 @@ impl Configuration { self.editor.as_ref() } - #[allow(dead_code)] // Why do I actually need this annotation on a pub function? /// Get the underlying configuration TOML object pub fn config(&self) -> &Value { &self.config From e5fa71d1e7e024421d4fd4c09b16692849741da3 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 3 Jun 2017 14:46:28 +0200 Subject: [PATCH 4/8] Minify mapping --- libimagrt/src/configuration.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/libimagrt/src/configuration.rs b/libimagrt/src/configuration.rs index 0487fc94..80bf50c6 100644 --- a/libimagrt/src/configuration.rs +++ b/libimagrt/src/configuration.rs @@ -128,13 +128,11 @@ impl Configuration { v.into_iter() .map(|s| { debug!("Trying to process '{}'", s); s }) - .filter_map(|s| { - match s.into_kv() { - Some(kv) => Some(kv.into()), - None => { - warn!("Could split at '=' - will be ignore override"); - None - } + .filter_map(|s| match s.into_kv() { + Some(kv) => Some(kv.into()), + None => { + warn!("Could split at '=' - will be ignore override"); + None } }) .map(|(k, v)| { From eff4f547ba29c8ece3cf4fae43e9141a7713517a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 3 Jun 2017 14:47:29 +0200 Subject: [PATCH 5/8] Minify mapping --- libimagrt/src/configuration.rs | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/libimagrt/src/configuration.rs b/libimagrt/src/configuration.rs index 80bf50c6..f1656059 100644 --- a/libimagrt/src/configuration.rs +++ b/libimagrt/src/configuration.rs @@ -135,23 +135,21 @@ impl Configuration { None } }) - .map(|(k, v)| { - self.config - .read(&k[..]) - .map_err_into(CEK::TOMLParserError) - .map(|toml| match toml { - Some(value) => { - match into_value(value, v) { - Some(v) => { - info!("Successfully overridden: {} = {}", k, v); - Ok(v) - }, - None => Err(CEK::ConfigOverrideTypeNotMatching.into_error()), - } + .map(|(k, v)| self + .config + .read(&k[..]) + .map_err_into(CEK::TOMLParserError) + .map(|toml| match toml { + Some(value) => match into_value(value, v) { + Some(v) => { + info!("Successfully overridden: {} = {}", k, v); + Ok(v) }, - None => Err(CEK::ConfigOverrideKeyNotAvailable.into_error()), - }) - }) + None => Err(CEK::ConfigOverrideTypeNotMatching.into_error()), + }, + None => Err(CEK::ConfigOverrideKeyNotAvailable.into_error()), + }) + ) .fold_result(|i| i) .map_err(Box::new) .map_err(|e| CEK::ConfigOverrideError.into_error_with_cause(e)) From e35d78fedb629c8fb54e4f3bd272ce6ae138f4aa Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 3 Jun 2017 14:48:53 +0200 Subject: [PATCH 6/8] Minify mapping --- libimagrt/src/configuration.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libimagrt/src/configuration.rs b/libimagrt/src/configuration.rs index f1656059..96d32010 100644 --- a/libimagrt/src/configuration.rs +++ b/libimagrt/src/configuration.rs @@ -165,9 +165,9 @@ fn into_value(value: Value, s: String) -> Option { use std::str::FromStr; match value { - Value::String(_) => Some(Value::String(s)), - 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::String(_) => Some(Value::String(s)), + Value::Integer(_) => FromStr::from_str(&s[..]).ok().map(Value::Integer), + Value::Float(_) => FromStr::from_str(&s[..]).ok().map(Value::Float), Value::Boolean(_) => { if s == "true" { Some(Value::Boolean(true)) } else if s == "false" { Some(Value::Boolean(false)) } From fe7bde38826074f461c1710ee42543681dd1eae7 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 3 Jun 2017 14:50:18 +0200 Subject: [PATCH 7/8] Minify mapping with dependency: is-match = 0.1 --- libimagrt/Cargo.toml | 1 + libimagrt/src/configuration.rs | 2 +- libimagrt/src/lib.rs | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/libimagrt/Cargo.toml b/libimagrt/Cargo.toml index a812e697..870b0410 100644 --- a/libimagrt/Cargo.toml +++ b/libimagrt/Cargo.toml @@ -22,6 +22,7 @@ xdg-basedir = "1.0" itertools = "0.5" tempfile = "2.1" ansi_term = "0.9" +is-match = "0.1" [dependencies.libimagstore] path = "../libimagstore" diff --git a/libimagrt/src/configuration.rs b/libimagrt/src/configuration.rs index 96d32010..38de0789 100644 --- a/libimagrt/src/configuration.rs +++ b/libimagrt/src/configuration.rs @@ -190,7 +190,7 @@ impl Deref for Configuration { fn get_verbosity(v: &Value) -> bool { match *v { 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, } } diff --git a/libimagrt/src/lib.rs b/libimagrt/src/lib.rs index 1670418f..63a49c45 100644 --- a/libimagrt/src/lib.rs +++ b/libimagrt/src/lib.rs @@ -42,6 +42,7 @@ extern crate ansi_term; extern crate clap; extern crate toml; +#[macro_use] extern crate is_match; extern crate libimagstore; extern crate libimagutil; From e7c53d84ca9ad778d0b179e12567557990906121 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 3 Jun 2017 14:53:52 +0200 Subject: [PATCH 8/8] Minify map/filter_map --- libimagrt/src/configuration.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libimagrt/src/configuration.rs b/libimagrt/src/configuration.rs index 38de0789..fe1c1614 100644 --- a/libimagrt/src/configuration.rs +++ b/libimagrt/src/configuration.rs @@ -245,7 +245,7 @@ fn fetch_config(rtp: &PathBuf) -> Result { ].iter() .flatten() .filter(|path| path.exists() && path.is_file()) - .map(|path| { + .filter_map(|path| if path.exists() && path.is_file() { debug!("Reading {:?}", path); let content = { let mut s = String::new(); @@ -266,8 +266,9 @@ fn fetch_config(rtp: &PathBuf) -> Result { .map_err(|e| REK::Instantiate.into_error_with_cause(e)); Some(toml) + } else { + None }) - .filter_map(|x| x) .filter(|loaded| loaded.is_ok()) .map(|inner| Value::Table(inner.unwrap())) .nth(0)