Refactoring: Use function chaining rather than matching
This commit is contained in:
parent
dac817f318
commit
66b0611103
4 changed files with 35 additions and 48 deletions
|
@ -21,6 +21,7 @@ use std::collections::BTreeMap;
|
||||||
|
|
||||||
use toml::Value;
|
use toml::Value;
|
||||||
|
|
||||||
|
use error::RefError as RE;
|
||||||
use error::RefErrorKind as REK;
|
use error::RefErrorKind as REK;
|
||||||
use error::Result;
|
use error::Result;
|
||||||
|
|
||||||
|
@ -39,13 +40,10 @@ impl RefFlags {
|
||||||
fn get_field(v: &Value, key: &str) -> Result<bool> {
|
fn get_field(v: &Value, key: &str) -> Result<bool> {
|
||||||
use toml_query::read::TomlValueReadExt;
|
use toml_query::read::TomlValueReadExt;
|
||||||
|
|
||||||
v.read(key)
|
v.read(key)?
|
||||||
.map_err(From::from)
|
.ok_or(RE::from_kind(REK::HeaderFieldMissingError))?
|
||||||
.and_then(|toml| match toml {
|
.as_bool()
|
||||||
Some(&Value::Boolean(b)) => Ok(b),
|
.ok_or(REK::HeaderTypeError.into())
|
||||||
Some(_) => Err(REK::HeaderTypeError.into()),
|
|
||||||
None => Err(REK::HeaderFieldMissingError.into()),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(RefFlags {
|
Ok(RefFlags {
|
||||||
|
|
|
@ -142,20 +142,16 @@ fn check_changed<R: Ref>(r: &R) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_changed_content<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)))
|
.and_then(|hash| r.get_stored_hash().map(|stored| (hash, stored)))
|
||||||
.map(|(hash, stored)| hash == stored);
|
.map(|(hash, stored)| hash == stored)
|
||||||
|
.unwrap_or_else(|e| {
|
||||||
match eq {
|
|
||||||
Ok(eq) => eq,
|
|
||||||
Err(e) => {
|
|
||||||
warn!("Could not check whether the ref changed on the FS");
|
warn!("Could not check whether the ref changed on the FS");
|
||||||
trace_error(&e);
|
trace_error(&e);
|
||||||
|
|
||||||
// We continue here and tell the callee that this reference is unchanged
|
// We continue here and tell the callee that this reference is unchanged
|
||||||
false
|
false
|
||||||
},
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_changed_permiss<R: Ref>(_: &R) -> bool {
|
fn check_changed_permiss<R: Ref>(_: &R) -> bool {
|
||||||
|
|
|
@ -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
|
/// Get the hahs of the link target which is stored in the ref object, which is hashed with a
|
||||||
/// custom Hasher instance.
|
/// custom Hasher instance.
|
||||||
fn get_stored_hash_with_hasher<H: Hasher>(&self, h: &H) -> Result<String> {
|
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())[..])? {
|
self.get_header()
|
||||||
// content hash stored...
|
.read(&format!("ref.content_hash.{}", h.hash_name())[..])?
|
||||||
Some(&Value::String(ref s)) => Ok(s.clone()),
|
.ok_or(RE::from_kind(REK::HeaderFieldMissingError))?
|
||||||
|
.as_str()
|
||||||
// content hash header field has wrong type
|
.map(String::from)
|
||||||
Some(_) => Err(RE::from_kind(REK::HeaderTypeError)),
|
.ok_or(RE::from_kind(REK::HeaderTypeError))
|
||||||
|
|
||||||
// content hash not stored
|
|
||||||
None => Err(RE::from_kind(REK::HeaderFieldMissingError)),
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the hash of the link target by reading the link target and hashing the contents
|
/// 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")
|
.read("ref.permissions.ro")
|
||||||
.chain_err(|| REK::HeaderFieldReadError)
|
.chain_err(|| REK::HeaderFieldReadError)
|
||||||
.and_then(|ro| {
|
.and_then(|ro| {
|
||||||
match ro {
|
ro.ok_or(RE::from_kind(REK::HeaderFieldMissingError))?
|
||||||
Some(&Value::Boolean(b)) => Ok(b),
|
.as_bool()
|
||||||
Some(_) => Err(RE::from_kind(REK::HeaderTypeError)),
|
.ok_or(RE::from_kind(REK::HeaderTypeError))
|
||||||
None => Err(RE::from_kind(REK::HeaderFieldMissingError)),
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.and_then(|ro| self.get_current_permissions().map(|perm| ro == perm.readonly()))
|
.and_then(|ro| self.get_current_permissions().map(|perm| ro == perm.readonly()))
|
||||||
.chain_err(|| REK::RefTargetCannotReadPermissions)
|
.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
|
/// Get the path of the file which is reffered to by this Ref
|
||||||
fn fs_file(&self) -> Result<PathBuf> {
|
fn fs_file(&self) -> Result<PathBuf> {
|
||||||
match self.get_header().read("ref.path")? {
|
self.get_header()
|
||||||
Some(&Value::String(ref s)) => Ok(PathBuf::from(s)),
|
.read("ref.path")?
|
||||||
Some(_) => Err(RE::from_kind(REK::HeaderTypeError)),
|
.ok_or(RE::from_kind(REK::HeaderFieldMissingError))?
|
||||||
None => Err(RE::from_kind(REK::HeaderFieldMissingError)),
|
.as_str()
|
||||||
}
|
.map(PathBuf::from)
|
||||||
|
.ok_or(RE::from_kind(REK::HeaderTypeError))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Re-find a referenced file
|
/// Re-find a referenced file
|
||||||
|
|
|
@ -25,7 +25,6 @@ use error::Result;
|
||||||
|
|
||||||
use libimagstore::store::Entry;
|
use libimagstore::store::Entry;
|
||||||
|
|
||||||
use toml::Value;
|
|
||||||
use toml_query::read::TomlValueReadExt;
|
use toml_query::read::TomlValueReadExt;
|
||||||
|
|
||||||
/// Creates a Hash from a PathBuf by making the PathBuf absolute and then running a hash
|
/// 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::sha1::Sha1;
|
||||||
use crypto::digest::Digest;
|
use crypto::digest::Digest;
|
||||||
|
|
||||||
match pb.to_str() {
|
pb.to_str()
|
||||||
Some(s) => {
|
.ok_or(RE::from_kind(REK::PathUTF8Error))
|
||||||
|
.map(|s| {
|
||||||
let mut hasher = Sha1::new();
|
let mut hasher = Sha1::new();
|
||||||
hasher.input_str(s);
|
hasher.input_str(s);
|
||||||
Ok(hasher.result_str())
|
hasher.result_str()
|
||||||
},
|
})
|
||||||
None => return Err(RE::from_kind(REK::PathUTF8Error)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read the reference from a file
|
/// Read the reference from a file
|
||||||
pub fn read_reference(refentry: &Entry) -> Result<PathBuf> {
|
pub fn read_reference(refentry: &Entry) -> Result<PathBuf> {
|
||||||
match refentry.get_header().read("ref.path")? {
|
refentry.get_header()
|
||||||
Some(&Value::String(ref s)) => Ok(PathBuf::from(s)),
|
.read("ref.path")?
|
||||||
Some(_) => Err(RE::from_kind(REK::HeaderTypeError)),
|
.ok_or(RE::from_kind(REK::HeaderFieldMissingError))?
|
||||||
None => Err(RE::from_kind(REK::HeaderFieldMissingError)),
|
.as_str()
|
||||||
}
|
.ok_or(RE::from_kind(REK::HeaderTypeError))
|
||||||
|
.map(PathBuf::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue