Refactoring: Use function chaining rather than matching

This commit is contained in:
Matthias Beyer 2018-01-04 23:09:30 +01:00
parent dac817f318
commit 66b0611103
4 changed files with 35 additions and 48 deletions

View file

@ -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<bool> {
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 {

View file

@ -142,20 +142,16 @@ fn check_changed<R: Ref>(r: &R) -> bool {
}
fn check_changed_content<R: Ref>(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: Ref>(_: &R) -> bool {

View file

@ -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<H: Hasher>(&self, h: &H) -> Result<String> {
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<PathBuf> {
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

View file

@ -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<String> {
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<PathBuf> {
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)
}