From 66b061110356441f5b7c6a1576aa93658dde3359 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 4 Jan 2018 23:09:30 +0100 Subject: [PATCH] Refactoring: Use function chaining rather than matching --- lib/entry/libimagentryref/src/flags.rs | 12 +++----- lib/entry/libimagentryref/src/lister.rs | 12 +++----- lib/entry/libimagentryref/src/reference.rs | 36 +++++++++------------- lib/entry/libimagentryref/src/util.rs | 23 +++++++------- 4 files changed, 35 insertions(+), 48 deletions(-) diff --git a/lib/entry/libimagentryref/src/flags.rs b/lib/entry/libimagentryref/src/flags.rs index 2d87c7c0..21d822dd 100644 --- a/lib/entry/libimagentryref/src/flags.rs +++ b/lib/entry/libimagentryref/src/flags.rs @@ -21,6 +21,7 @@ use std::collections::BTreeMap; use toml::Value; +use error::RefError as RE; use error::RefErrorKind as REK; use error::Result; @@ -39,13 +40,10 @@ impl RefFlags { fn get_field(v: &Value, key: &str) -> Result { use toml_query::read::TomlValueReadExt; - v.read(key) - .map_err(From::from) - .and_then(|toml| match toml { - Some(&Value::Boolean(b)) => Ok(b), - Some(_) => Err(REK::HeaderTypeError.into()), - None => Err(REK::HeaderFieldMissingError.into()), - }) + v.read(key)? + .ok_or(RE::from_kind(REK::HeaderFieldMissingError))? + .as_bool() + .ok_or(REK::HeaderTypeError.into()) } Ok(RefFlags { diff --git a/lib/entry/libimagentryref/src/lister.rs b/lib/entry/libimagentryref/src/lister.rs index e3d9bdfa..3e06f003 100644 --- a/lib/entry/libimagentryref/src/lister.rs +++ b/lib/entry/libimagentryref/src/lister.rs @@ -142,20 +142,16 @@ fn check_changed(r: &R) -> bool { } fn check_changed_content(r: &R) -> bool { - let eq = r.get_current_hash() + r.get_current_hash() .and_then(|hash| r.get_stored_hash().map(|stored| (hash, stored))) - .map(|(hash, stored)| hash == stored); - - match eq { - Ok(eq) => eq, - Err(e) => { + .map(|(hash, stored)| hash == stored) + .unwrap_or_else(|e| { warn!("Could not check whether the ref changed on the FS"); trace_error(&e); // We continue here and tell the callee that this reference is unchanged false - }, - } + }) } fn check_changed_permiss(_: &R) -> bool { diff --git a/lib/entry/libimagentryref/src/reference.rs b/lib/entry/libimagentryref/src/reference.rs index c043b1e0..9c20011b 100644 --- a/lib/entry/libimagentryref/src/reference.rs +++ b/lib/entry/libimagentryref/src/reference.rs @@ -140,17 +140,12 @@ impl Ref for Entry { /// Get the hahs of the link target which is stored in the ref object, which is hashed with a /// custom Hasher instance. fn get_stored_hash_with_hasher(&self, h: &H) -> Result { - match self.get_header().read(&format!("ref.content_hash.{}", h.hash_name())[..])? { - // content hash stored... - Some(&Value::String(ref s)) => Ok(s.clone()), - - // content hash header field has wrong type - Some(_) => Err(RE::from_kind(REK::HeaderTypeError)), - - // content hash not stored - None => Err(RE::from_kind(REK::HeaderFieldMissingError)), - - } + self.get_header() + .read(&format!("ref.content_hash.{}", h.hash_name())[..])? + .ok_or(RE::from_kind(REK::HeaderFieldMissingError))? + .as_str() + .map(String::from) + .ok_or(RE::from_kind(REK::HeaderTypeError)) } /// Get the hash of the link target by reading the link target and hashing the contents @@ -207,11 +202,9 @@ impl Ref for Entry { .read("ref.permissions.ro") .chain_err(|| REK::HeaderFieldReadError) .and_then(|ro| { - match ro { - Some(&Value::Boolean(b)) => Ok(b), - Some(_) => Err(RE::from_kind(REK::HeaderTypeError)), - None => Err(RE::from_kind(REK::HeaderFieldMissingError)), - } + ro.ok_or(RE::from_kind(REK::HeaderFieldMissingError))? + .as_bool() + .ok_or(RE::from_kind(REK::HeaderTypeError)) }) .and_then(|ro| self.get_current_permissions().map(|perm| ro == perm.readonly())) .chain_err(|| REK::RefTargetCannotReadPermissions) @@ -251,11 +244,12 @@ impl Ref for Entry { /// Get the path of the file which is reffered to by this Ref fn fs_file(&self) -> Result { - match self.get_header().read("ref.path")? { - Some(&Value::String(ref s)) => Ok(PathBuf::from(s)), - Some(_) => Err(RE::from_kind(REK::HeaderTypeError)), - None => Err(RE::from_kind(REK::HeaderFieldMissingError)), - } + self.get_header() + .read("ref.path")? + .ok_or(RE::from_kind(REK::HeaderFieldMissingError))? + .as_str() + .map(PathBuf::from) + .ok_or(RE::from_kind(REK::HeaderTypeError)) } /// Re-find a referenced file diff --git a/lib/entry/libimagentryref/src/util.rs b/lib/entry/libimagentryref/src/util.rs index 39896d42..0055991d 100644 --- a/lib/entry/libimagentryref/src/util.rs +++ b/lib/entry/libimagentryref/src/util.rs @@ -25,7 +25,6 @@ use error::Result; use libimagstore::store::Entry; -use toml::Value; use toml_query::read::TomlValueReadExt; /// Creates a Hash from a PathBuf by making the PathBuf absolute and then running a hash @@ -34,22 +33,22 @@ pub fn hash_path(pb: &PathBuf) -> Result { use crypto::sha1::Sha1; use crypto::digest::Digest; - match pb.to_str() { - Some(s) => { + pb.to_str() + .ok_or(RE::from_kind(REK::PathUTF8Error)) + .map(|s| { let mut hasher = Sha1::new(); hasher.input_str(s); - Ok(hasher.result_str()) - }, - None => return Err(RE::from_kind(REK::PathUTF8Error)), - } + hasher.result_str() + }) } /// Read the reference from a file pub fn read_reference(refentry: &Entry) -> Result { - match refentry.get_header().read("ref.path")? { - Some(&Value::String(ref s)) => Ok(PathBuf::from(s)), - Some(_) => Err(RE::from_kind(REK::HeaderTypeError)), - None => Err(RE::from_kind(REK::HeaderFieldMissingError)), - } + refentry.get_header() + .read("ref.path")? + .ok_or(RE::from_kind(REK::HeaderFieldMissingError))? + .as_str() + .ok_or(RE::from_kind(REK::HeaderTypeError)) + .map(PathBuf::from) }