diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2017-05-03 21:19:06 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-03 21:19:06 +0200 |
commit | 4257ec10268fef06d5888c48f9fc8f9e6f35c5ba (patch) | |
tree | a4b6b8abce76e05d365d8146ac5cfad93514bb44 | |
parent | 1da56c6d9df689150c94631bbb5147c36070b75c (diff) | |
parent | db36a6a5ad2e431390bbec3b3665b08c21997a07 (diff) |
Merge pull request #930 from matthiasbeyer/update-toml
Dependency: toml: 0.2.* -> 0.4.*
-rw-r--r-- | .travis.yml | 2 | ||||
-rw-r--r-- | imag-link/Cargo.toml | 2 | ||||
-rw-r--r-- | imag-store/Cargo.toml | 2 | ||||
-rw-r--r-- | imag-tag/Cargo.toml | 2 | ||||
-rw-r--r-- | imag-view/Cargo.toml | 2 | ||||
-rw-r--r-- | libimagannotation/Cargo.toml | 2 | ||||
-rw-r--r-- | libimagentryfilter/Cargo.toml | 2 | ||||
-rw-r--r-- | libimagentrylink/Cargo.toml | 2 | ||||
-rw-r--r-- | libimagentrylist/Cargo.toml | 2 | ||||
-rw-r--r-- | libimagentrytag/Cargo.toml | 2 | ||||
-rw-r--r-- | libimagentryview/Cargo.toml | 2 | ||||
-rw-r--r-- | libimagentryview/src/builtin/stdout.rs | 4 | ||||
-rw-r--r-- | libimaginteraction/Cargo.toml | 2 | ||||
-rw-r--r-- | libimagnotes/Cargo.toml | 2 | ||||
-rw-r--r-- | libimagref/Cargo.toml | 2 | ||||
-rw-r--r-- | libimagref/src/error.rs | 1 | ||||
-rw-r--r-- | libimagref/src/flags.rs | 15 | ||||
-rw-r--r-- | libimagrt/Cargo.toml | 2 | ||||
-rw-r--r-- | libimagrt/src/configuration.rs | 63 | ||||
-rw-r--r-- | libimagstore/Cargo.toml | 2 | ||||
-rw-r--r-- | libimagstore/src/store.rs | 6 | ||||
-rw-r--r-- | libimagstore/src/toml_ext.rs | 11 | ||||
-rw-r--r-- | libimagstorestdhook/Cargo.toml | 2 | ||||
-rw-r--r-- | libimagstorestdhook/src/denylinkeddelete.rs | 16 | ||||
-rw-r--r-- | libimagstorestdhook/src/vcs/git/config.rs | 135 |
25 files changed, 161 insertions, 124 deletions
diff --git a/.travis.yml b/.travis.yml index d3fd9427..9858c85d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ language: rust matrix: include: - - rust: 1.13.0 + - rust: 1.15.0 cache: cargo: true diff --git a/imag-link/Cargo.toml b/imag-link/Cargo.toml index b4266339..d346fe98 100644 --- a/imag-link/Cargo.toml +++ b/imag-link/Cargo.toml @@ -18,7 +18,7 @@ semver = "0.5.1" clap = ">=2.17" log = "0.3" version = "2.0.1" -toml = "0.2.*" +toml = "^0.4" url = "1.2" [dependencies.libimagstore] diff --git a/imag-store/Cargo.toml b/imag-store/Cargo.toml index 6bbd072f..fba77d9a 100644 --- a/imag-store/Cargo.toml +++ b/imag-store/Cargo.toml @@ -18,7 +18,7 @@ clap = ">=2.17" log = "0.3" version = "2.0.1" semver = "0.5" -toml = "0.2.*" +toml = "^0.4" [dependencies.libimagstore] path = "../libimagstore" diff --git a/imag-tag/Cargo.toml b/imag-tag/Cargo.toml index 6a0e0094..2cdc7823 100644 --- a/imag-tag/Cargo.toml +++ b/imag-tag/Cargo.toml @@ -18,7 +18,7 @@ clap = "2.*" log = "0.3" version = "2.0.1" semver = "0.2" -toml = "0.2.*" +toml = "^0.4" [dependencies.libimagstore] path = "../libimagstore" diff --git a/imag-view/Cargo.toml b/imag-view/Cargo.toml index 5b7d8120..a8b3ef89 100644 --- a/imag-view/Cargo.toml +++ b/imag-view/Cargo.toml @@ -17,7 +17,7 @@ homepage = "http://imag-pim.org" clap = ">=2.17" log = "0.3" semver = "0.5" -toml = "0.2.*" +toml = "^0.4" version = "2.0.1" [dependencies.libimagstore] diff --git a/libimagannotation/Cargo.toml b/libimagannotation/Cargo.toml index 4d15b2df..a94353db 100644 --- a/libimagannotation/Cargo.toml +++ b/libimagannotation/Cargo.toml @@ -6,7 +6,7 @@ authors = ["Matthias Beyer <mail@beyermatthias.de>"] [dependencies] uuid = { version = "0.3.1", features = ["v4"] } lazy_static = "0.1.15" -toml = "0.2.*" +toml = "^0.4" [dependencies.libimagstore] path = "../libimagstore" diff --git a/libimagentryfilter/Cargo.toml b/libimagentryfilter/Cargo.toml index c3425680..eff48c61 100644 --- a/libimagentryfilter/Cargo.toml +++ b/libimagentryfilter/Cargo.toml @@ -20,7 +20,7 @@ itertools = "0.5" log = "0.3" regex = "0.2" semver = "0.5.*" -toml = "0.2.*" +toml = "^0.4" [dependencies.libimagstore] path = "../libimagstore" diff --git a/libimagentrylink/Cargo.toml b/libimagentrylink/Cargo.toml index bfe83f1e..02d6f0a7 100644 --- a/libimagentrylink/Cargo.toml +++ b/libimagentrylink/Cargo.toml @@ -16,7 +16,7 @@ homepage = "http://imag-pim.org" [dependencies] itertools = "0.5" log = "0.3" -toml = "0.2.*" +toml = "^0.4" semver = "0.5" url = "1.2" rust-crypto = "0.2" diff --git a/libimagentrylist/Cargo.toml b/libimagentrylist/Cargo.toml index 3e26495a..99ba8a62 100644 --- a/libimagentrylist/Cargo.toml +++ b/libimagentrylist/Cargo.toml @@ -16,7 +16,7 @@ homepage = "http://imag-pim.org" [dependencies] clap = ">=2.17" log = "0.3" -toml = "0.2.*" +toml = "^0.4" prettytable-rs = "0.6.*" [dependencies.libimagstore] diff --git a/libimagentrytag/Cargo.toml b/libimagentrytag/Cargo.toml index 2ff5c790..81565f2f 100644 --- a/libimagentrytag/Cargo.toml +++ b/libimagentrytag/Cargo.toml @@ -17,7 +17,7 @@ homepage = "http://imag-pim.org" clap = ">=2.17" log = "0.3" regex = "0.2" -toml = "0.2.*" +toml = "^0.4" itertools = "0.5" [dependencies.libimagstore] diff --git a/libimagentryview/Cargo.toml b/libimagentryview/Cargo.toml index fa1dfddb..f0175497 100644 --- a/libimagentryview/Cargo.toml +++ b/libimagentryview/Cargo.toml @@ -15,7 +15,7 @@ homepage = "http://imag-pim.org" [dependencies] log = "0.3" -toml = "0.2.*" +toml = "^0.4" glob = "0.2" [dependencies.libimagrt] diff --git a/libimagentryview/src/builtin/stdout.rs b/libimagentryview/src/builtin/stdout.rs index 4d431f2e..3d9fea05 100644 --- a/libimagentryview/src/builtin/stdout.rs +++ b/libimagentryview/src/builtin/stdout.rs @@ -19,7 +19,7 @@ use libimagstore::store::Entry; -use toml::encode_str; +use toml::ser::to_string; use viewer::Viewer; use result::Result; @@ -44,7 +44,7 @@ impl Viewer for StdoutViewer { fn view_entry(&self, e: &Entry) -> Result<()> { if self.view_header { - println!("{}", encode_str(e.get_header())); + println!("{}", to_string(e.get_header()).unwrap_or(String::from("TOML Parser error"))); } if self.view_content { diff --git a/libimaginteraction/Cargo.toml b/libimaginteraction/Cargo.toml index 054895b3..9d5d6a0d 100644 --- a/libimaginteraction/Cargo.toml +++ b/libimaginteraction/Cargo.toml @@ -20,7 +20,7 @@ interactor = "0.1" lazy_static = "0.2.*" log = "0.3" regex = "0.2" -toml = "0.2.1" +toml = "^0.4" spinner = "0.4" rustyline = "1.0" diff --git a/libimagnotes/Cargo.toml b/libimagnotes/Cargo.toml index 9c840cb2..7853772f 100644 --- a/libimagnotes/Cargo.toml +++ b/libimagnotes/Cargo.toml @@ -16,7 +16,7 @@ homepage = "http://imag-pim.org" [dependencies] semver = "0.5" log = "0.3" -toml = "0.2.*" +toml = "^0.4" [dependencies.libimagstore] path = "../libimagstore" diff --git a/libimagref/Cargo.toml b/libimagref/Cargo.toml index 27028b99..be49b0fc 100644 --- a/libimagref/Cargo.toml +++ b/libimagref/Cargo.toml @@ -18,7 +18,7 @@ itertools = "0.5" log = "0.3" rust-crypto = "0.2" semver = "0.5" -toml = "0.2.*" +toml = "^0.4" version = "2.0.1" walkdir = "1.0.*" diff --git a/libimagref/src/error.rs b/libimagref/src/error.rs index 4f782f6c..56c1e869 100644 --- a/libimagref/src/error.rs +++ b/libimagref/src/error.rs @@ -24,6 +24,7 @@ generate_error_module!( IOError => "IO Error", UTF8Error => "UTF8 Error", StoreIdError => "Error with storeid", + HeaderTomlError => "Error while working with TOML Header", HeaderTypeError => "Header type error", HeaderFieldMissingError => "Header field missing error", HeaderFieldWriteError => "Header field cannot be written", diff --git a/libimagref/src/flags.rs b/libimagref/src/flags.rs index 11cd838d..6ddf2990 100644 --- a/libimagref/src/flags.rs +++ b/libimagref/src/flags.rs @@ -37,11 +37,16 @@ impl RefFlags { /// It assumes that this is a Map with Key = <name of the setting> and Value = boolean. pub fn read(v: &Value) -> Result<RefFlags> { fn get_field(v: &Value, key: &str) -> Result<bool> { - match v.lookup(key) { - Some(&Value::Boolean(b)) => Ok(b), - Some(_) => Err(REK::HeaderTypeError.into()), - None => Err(REK::HeaderFieldMissingError.into()), - } + use libimagstore::toml_ext::TomlValueExt; + use error::MapErrInto; + + v.read(key) + .map_err_into(REK::HeaderTomlError) + .and_then(|toml| match toml { + Some(Value::Boolean(b)) => Ok(b), + Some(_) => Err(REK::HeaderTypeError.into()), + None => Err(REK::HeaderFieldMissingError.into()), + }) } Ok(RefFlags { diff --git a/libimagrt/Cargo.toml b/libimagrt/Cargo.toml index fdc7ba22..29cf9953 100644 --- a/libimagrt/Cargo.toml +++ b/libimagrt/Cargo.toml @@ -16,7 +16,7 @@ homepage = "http://imag-pim.org" [dependencies] clap = ">=2.17" env_logger = "0.3" -toml = "0.2.*" +toml = "^0.4" log = "0.3" xdg-basedir = "1.0" itertools = "0.5" diff --git a/libimagrt/src/configuration.rs b/libimagrt/src/configuration.rs index 63d24e15..e6c6c726 100644 --- a/libimagrt/src/configuration.rs +++ b/libimagrt/src/configuration.rs @@ -21,10 +21,11 @@ use std::path::PathBuf; use std::result::Result as RResult; use std::ops::Deref; -use toml::{Parser, Value}; +use toml::Value; generate_error_module!( generate_error_types!(ConfigError, ConfigErrorKind, + TOMLParserError => "TOML Parsing error", NoConfigFileFound => "No config file found", ConfigOverrideError => "Config override error", @@ -119,7 +120,9 @@ impl Configuration { use libimagutil::key_value_split::*; use libimagutil::iter::*; use self::error::ConfigErrorKind as CEK; + use self::error::MapErrInto; use libimagerror::into::IntoError; + use libimagstore::toml_ext::TomlValueExt; v.into_iter() .map(|s| { debug!("Trying to process '{}'", s); s }) @@ -133,19 +136,21 @@ impl Configuration { } }) .map(|(k, v)| { - match self.config.lookup_mut(&k[..]) { - Some(value) => { - match into_value(value, v) { - Some(v) => { - *value = v; - info!("Successfully overridden: {} = {}", k, value); - Ok(()) - }, - None => Err(CEK::ConfigOverrideTypeNotMatching.into_error()), - } - }, - None => Err(CEK::ConfigOverrideKeyNotAvailable.into_error()), - } + 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()), + } + }, + None => Err(CEK::ConfigOverrideKeyNotAvailable.into_error()), + }) }) .fold_result(|i| i) .map_err(Box::new) @@ -158,19 +163,19 @@ impl Configuration { /// Returns None if string cannot be converted. /// /// 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; 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::Boolean(_) => { + 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::Boolean(_) => { if s == "true" { Some(Value::Boolean(true)) } else if s == "false" { Some(Value::Boolean(false)) } else { None } } - &Value::Datetime(_) => Some(Value::Datetime(s)), + Value::Datetime(_) => Value::try_from(s).ok(), _ => None, } } @@ -223,6 +228,7 @@ fn fetch_config(rtp: &PathBuf) -> Result<Value> { use itertools::Itertools; use libimagutil::variants::generate_variants as gen_vars; + use libimagerror::trace::trace_error; let variants = vec!["config", "config.toml", "imagrc", "imagrc.toml"]; let modifier = |base: &PathBuf, v: &'static str| { @@ -255,18 +261,13 @@ fn fetch_config(rtp: &PathBuf) -> Result<Value> { s }; - let mut parser = Parser::new(&content[..]); - let res = parser.parse(); - - if res.is_none() { - write!(stderr(), "Config file parser error:").ok(); - for error in parser.errors { - write!(stderr(), "At [{}][{}] <> {}", error.lo, error.hi, error).ok(); - write!(stderr(), "in: '{}'", &content[error.lo..error.hi]).ok(); + match ::toml::de::from_str(&content[..]) { + Ok(res) => res, + Err(e) => { + write!(stderr(), "Config file parser error:").ok(); + trace_error(&e); + None } - None - } else { - res } }) .filter(|loaded| loaded.is_some()) diff --git a/libimagstore/Cargo.toml b/libimagstore/Cargo.toml index 4e5ecbf9..f2e12cc0 100644 --- a/libimagstore/Cargo.toml +++ b/libimagstore/Cargo.toml @@ -20,7 +20,7 @@ lazy_static = "0.2.*" log = "0.3" regex = "0.2" semver = "0.5" -toml = "0.2.*" +toml = "^0.4" version = "2.0.1" crossbeam = "0.2.*" walkdir = "1.0.*" diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 39f59788..ef5725f5 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -1252,7 +1252,7 @@ impl Entry { /// disk). pub fn to_str(&self) -> String { format!("---\n{header}---\n{content}", - header = ::toml::encode_str(&self.header), + header = ::toml::ser::to_string(&self.header).unwrap(), content = self.content) } @@ -1915,9 +1915,9 @@ mod store_hook_tests { use self::test_hook::TestHook; fn get_store_with_config() -> Store { - use toml::Parser; + use toml::de::from_str; - let cfg = Parser::new(mini_config()).parse().unwrap(); + let cfg : ::toml::Value = from_str(mini_config()).unwrap(); println!("Config parsed: {:?}", cfg); Store::new(PathBuf::from("/"), Some(cfg.get("store").cloned().unwrap())).unwrap() } diff --git a/libimagstore/src/toml_ext.rs b/libimagstore/src/toml_ext.rs index 1ac4cf11..9c0ae540 100644 --- a/libimagstore/src/toml_ext.rs +++ b/libimagstore/src/toml_ext.rs @@ -20,7 +20,7 @@ use std::result::Result as RResult; use std::collections::BTreeMap; -use toml::{Table, Value}; +use toml::Value; use store::Result; use error::StoreError as SE; @@ -28,6 +28,8 @@ use error::StoreErrorKind as SEK; use error::{ParserErrorKind, ParserError}; use libimagerror::into::IntoError; +type Table = BTreeMap<String, Value>; + pub trait TomlValueExt { fn insert_with_sep(&mut self, spec: &str, sep: char, v: Value) -> Result<bool>; fn set_with_sep(&mut self, spec: &str, sep: char, v: Value) -> Result<Option<Value>>; @@ -378,11 +380,10 @@ impl Header for Value { } fn parse(s: &str) -> EntryResult<Value> { - use toml::Parser; + use toml::de::from_str; - let mut parser = Parser::new(s); - parser.parse() - .ok_or(ParserErrorKind::TOMLParserErrors.into()) + from_str(s) + .map_err(|_| ParserErrorKind::TOMLParserErrors.into()) .and_then(verify_header_consistency) .map(Value::Table) } diff --git a/libimagstorestdhook/Cargo.toml b/libimagstorestdhook/Cargo.toml index eec78cb7..3e70f61a 100644 --- a/libimagstorestdhook/Cargo.toml +++ b/libimagstorestdhook/Cargo.toml @@ -14,7 +14,7 @@ repository = "https://github.com/matthiasbeyer/imag" homepage = "http://imag-pim.org" [dependencies] -toml = "0.2.*" +toml = "^0.4" log = "0.3" fs2 = "0.3" git2 = "0.5" diff --git a/libimagstorestdhook/src/denylinkeddelete.rs b/libimagstorestdhook/src/denylinkeddelete.rs index 98f3a01a..6186535a 100644 --- a/libimagstorestdhook/src/denylinkeddelete.rs +++ b/libimagstorestdhook/src/denylinkeddelete.rs @@ -26,8 +26,9 @@ use libimagstore::hook::accessor::HookDataAccessorProvider; use libimagstore::hook::accessor::NonMutableHookDataAccessor; use libimagstore::hook::result::HookResult; use libimagstore::store::FileLockEntry; +use libimagstore::toml_ext::TomlValueExt; use libimagentrylink::internal::InternalLinker; - +use libimagerror::trace::trace_error; mod error { generate_error_imports!(); @@ -60,17 +61,22 @@ impl Hook for DenyDeletionOfLinkedEntriesHook { } fn set_config(&mut self, v: &Value) { - self.abort = match v.lookup("aborting") { - Some(&Value::Boolean(b)) => b, - Some(_) => { + self.abort = match v.read("aborting") { + Ok(Some(Value::Boolean(b))) => b, + Ok(Some(_)) => { warn!("Configuration error, 'aborting' must be a Boolean (true|false)."); warn!("Assuming 'true' now."); true }, - None => { + Ok(None) => { warn!("No key 'aborting' - Assuming 'true'"); true }, + Err(e) => { + error!("Error parsing TOML:"); + trace_error(&e); + false + }, }; } diff --git a/libimagstorestdhook/src/vcs/git/config.rs b/libimagstorestdhook/src/vcs/git/config.rs index 0d33c708..4c44db93 100644 --- a/libimagstorestdhook/src/vcs/git/config.rs +++ b/libimagstorestdhook/src/vcs/git/config.rs @@ -20,7 +20,9 @@ use toml::Value; use libimagerror::into::IntoError; +use libimagerror::trace::trace_error; use libimagstore::storeid::StoreId; +use libimagstore::toml_ext::TomlValueExt; use vcs::git::error::GitHookErrorKind as GHEK; use vcs::git::error::MapErrInto; @@ -32,59 +34,72 @@ use git2::Repository; /// Check the configuration whether we should commit interactively pub fn commit_interactive(config: &Value, action: &StoreAction) -> bool { - match config.lookup("commit.interactive") { - Some(&Value::Boolean(b)) => b, - Some(_) => { + match config.read("commit.interactive") { + Ok(Some(Value::Boolean(b))) => b, + Ok(Some(_)) => { warn!("Configuration error, 'store.hooks.stdhook_git_{}.commit.interactive' must be a Boolean.", action); warn!("Defaulting to commit.interactive = false"); false } - None => { + Ok(None) => { warn!("Unavailable configuration for"); warn!("\t'store.hooks.stdhook_git_{}.commit.interactive'", action); warn!("Defaulting to false"); false - } + }, + Err(e) => { + error!("Error parsing TOML:"); + trace_error(&e); + false + }, } } /// Check the configuration whether we should commit with the editor fn commit_with_editor(config: &Value, action: &StoreAction) -> bool { - match config.lookup("commit.interactive_editor") { - Some(&Value::Boolean(b)) => b, - Some(_) => { + match config.read("commit.interactive_editor") { + Ok(Some(Value::Boolean(b))) => b, + Ok(Some(_)) => { warn!("Configuration error, 'store.hooks.stdhook_git_{}.commit.interactive_editor' must be a Boolean.", action); warn!("Defaulting to commit.interactive_editor = false"); false } - None => { + Ok(None) => { warn!("Unavailable configuration for"); warn!("\t'store.hooks.stdhook_git_{}.commit.interactive_editor'", action); warn!("Defaulting to false"); false - } + }, + Err(e) => { + error!("Error parsing TOML:"); + trace_error(&e); + false + }, } } /// Get the commit default message -fn commit_default_msg<'a>(config: &'a Value, action: &'a StoreAction) -> &'a str { - match config.lookup("commit.message") { - Some(&Value::String(ref b)) => b, - Some(_) => { - warn!("Configuration error, 'store.hooks.stdhook_git_{}.commit.message' must be a String.", - action); - warn!("Defaulting to commit.message = '{}'", action.as_commit_message()); - action.as_commit_message() - } - None => { - warn!("Unavailable configuration for"); - warn!("\t'store.hooks.stdhook_git_{}.commit.message'", action); - warn!("Defaulting to commit.message = '{}'", action.as_commit_message()); - action.as_commit_message() - } - } +fn commit_default_msg<'a>(config: &'a Value, action: &'a StoreAction) -> Result<String> { + config.read("commit.message") + .map(|m| match m { + Some(Value::String(b)) => String::from(b), + Some(_) => { + warn!("Configuration error, 'store.hooks.stdhook_git_{}.commit.message' must be a String.", + action); + warn!("Defaulting to commit.message = '{}'", action.as_commit_message()); + String::from(action.as_commit_message()) + }, + None => { + warn!("Unavailable configuration for"); + warn!("\t'store.hooks.stdhook_git_{}.commit.message'", action); + warn!("Defaulting to commit.message = '{}'", action.as_commit_message()); + String::from(action.as_commit_message()) + }, + }) + .map_err_into(GHEK::ConfigError) + } /// Get the commit template @@ -129,7 +144,7 @@ pub fn commit_message(repo: &Repository, config: &Value, action: StoreAction, id Ok(ask_string("Commit Message", None, false, false, None, "> ")) } } else { - Ok(String::from(commit_default_msg(config, &action))) + commit_default_msg(config, &action) } } @@ -145,18 +160,20 @@ pub fn abort_on_repo_init_err(cfg: &Value) -> bool { pub fn ensure_branch(cfg: Option<&Value>) -> Result<Option<String>> { match cfg { Some(cfg) => { - match cfg.lookup("ensure_branch") { - Some(&Value::String(ref s)) => Ok(Some(s.clone())), - Some(_) => { - warn!("Configuration error, 'ensure_branch' must be a String."); - Err(GHEK::ConfigTypeError.into_error()) - .map_err_into(GHEK::ConfigTypeError) - }, - None => { - debug!("No key `ensure_branch'"); - Ok(None) - }, - } + cfg.read("ensure_branch") + .map_err_into(GHEK::ConfigError) + .and_then(|toml| match toml { + Some(Value::String(ref s)) => Ok(Some(s.clone())), + Some(_) => { + warn!("Configuration error, 'ensure_branch' must be a String."); + Err(GHEK::ConfigTypeError.into_error()) + .map_err_into(GHEK::ConfigTypeError) + }, + None => { + debug!("No key `ensure_branch'"); + Ok(None) + }, + }) }, None => Ok(None), } @@ -170,17 +187,22 @@ pub fn do_checkout_ensure_branch(cfg: Option<&Value>) -> bool { /// Helper to get a boolean value from the configuration. fn get_bool_cfg(cfg: Option<&Value>, name: &str, on_fail: bool, on_unavail: bool) -> bool { cfg.map(|cfg| { - match cfg.lookup(name) { - Some(&Value::Boolean(b)) => b, - Some(_) => { + match cfg.read(name) { + Ok(Some(Value::Boolean(b))) => b, + Ok(Some(_)) => { warn!("Configuration error, '{}' must be a Boolean (true|false).", name); warn!("Assuming '{}' now.", on_fail); on_fail }, - None => { + Ok(None) => { warn!("No key '{}' - Assuming '{}'", name, on_unavail); on_unavail }, + Err(e) => { + error!("Error parsing TOML:"); + trace_error(&e); + false + }, } }) .unwrap_or_else(|| { @@ -197,19 +219,20 @@ pub fn is_enabled(cfg: &Value) -> bool { /// Check whether committing is enabled for a hook. pub fn committing_is_enabled(cfg: &Value) -> Result<bool> { - match cfg.lookup("commit.enabled") { - Some(&Value::Boolean(b)) => Ok(b), - Some(_) => { - warn!("Config setting whether committing is enabled or not has wrong type."); - warn!("Expected Boolean"); - Err(GHEK::ConfigTypeError.into_error()) - }, - None => { - warn!("No config setting whether committing is enabled or not."); - Err(GHEK::NoConfigError.into_error()) - }, - } - .map_err_into(GHEK::ConfigError) + cfg.read("commit.enabled") + .map_err_into(GHEK::ConfigError) + .and_then(|toml| match toml { + Some(Value::Boolean(b)) => Ok(b), + Some(_) => { + warn!("Config setting whether committing is enabled or not has wrong type."); + warn!("Expected Boolean"); + Err(GHEK::ConfigTypeError.into_error()) + }, + None => { + warn!("No config setting whether committing is enabled or not."); + Err(GHEK::NoConfigError.into_error()) + }, + }) } pub fn add_wt_changes_before_committing(cfg: &Value) -> bool { |