From d58b97fdf1cf979720a01f8d23f3ce0c59c4eafc Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 27 Aug 2017 21:08:23 +0200 Subject: [PATCH] Light refactoring Use .map_err_into() instead of manual wrapping, some boilerplate minimized. --- .../libimagentryref/src/hashers/nbytes.rs | 11 +--- lib/entry/libimagentryref/src/reference.rs | 37 +++-------- lib/entry/libimagentryref/src/refstore.rs | 63 +++++++------------ 3 files changed, 34 insertions(+), 77 deletions(-) diff --git a/lib/entry/libimagentryref/src/hashers/nbytes.rs b/lib/entry/libimagentryref/src/hashers/nbytes.rs index 995e3dd8..5d190840 100644 --- a/lib/entry/libimagentryref/src/hashers/nbytes.rs +++ b/lib/entry/libimagentryref/src/hashers/nbytes.rs @@ -24,8 +24,6 @@ use std::result::Result as RResult; use crypto::sha1::Sha1; use crypto::digest::Digest; -use libimagerror::into::IntoError; - use hasher::Hasher; use result::Result; use error::RefErrorKind as REK; @@ -54,16 +52,13 @@ impl Hasher for NBytesHasher { } fn create_hash(&mut self, _: &PathBuf, contents: &mut R) -> Result { - let s = contents + let s = try!(contents .bytes() .take(self.n) .collect::, _>>() .map_err_into(REK::IOError) - .and_then(|v| String::from_utf8(v).map_err_into(REK::IOError)) - .map_err(Box::new) - .map_err(|e| REK::UTF8Error.into_error_with_cause(e)) - .map_err_into(REK::IOError); - self.hasher.input_str(&try!(s)[..]); + .and_then(|v| String::from_utf8(v).map_err_into(REK::UTF8Error))); + self.hasher.input_str(&s[..]); Ok(self.hasher.result_str()) } diff --git a/lib/entry/libimagentryref/src/reference.rs b/lib/entry/libimagentryref/src/reference.rs index 5263c765..0af07a2a 100644 --- a/lib/entry/libimagentryref/src/reference.rs +++ b/lib/entry/libimagentryref/src/reference.rs @@ -162,12 +162,7 @@ impl Ref for Entry { /// custom hasher fn get_current_hash_with_hasher(&self, mut h: H) -> Result { self.fs_file() - .and_then(|pb| { - File::open(pb.clone()) - .map(|f| (pb, f)) - .map_err(Box::new) - .map_err(|e| REK::IOError.into_error_with_cause(e)) - }) + .and_then(|pb| File::open(pb.clone()).map(|f| (pb, f)).map_err_into(REK::IOError)) .and_then(|(path, mut file)| h.create_hash(&path, &mut file)) } @@ -210,8 +205,7 @@ impl Ref for Entry { self .get_header() .read("ref.permissions.ro") - .map_err(Box::new) - .map_err(|e| REK::HeaderFieldReadError.into_error_with_cause(e)) + .map_err_into(REK::HeaderFieldReadError) .and_then(|ro| { match ro { Some(Value::Boolean(b)) => Ok(b), @@ -220,8 +214,7 @@ impl Ref for Entry { } }) .and_then(|ro| self.get_current_permissions().map(|perm| ro == perm.readonly())) - .map_err(Box::new) - .map_err(|e| REK::RefTargetCannotReadPermissions.into_error_with_cause(e)) + .map_err_into(REK::RefTargetCannotReadPermissions) } /// Check whether the Hashsum of the referenced file is equal to the stored hashsum @@ -246,15 +239,13 @@ impl Ref for Entry { try!(self .get_header_mut() .set("ref.permissions.ro", Value::Boolean(current_perm.readonly())) - .map_err(Box::new) - .map_err(|e| REK::StoreWriteError.into_error_with_cause(e)) + .map_err_into(REK::StoreWriteError) ); try!(self .get_header_mut() .set(&format!("ref.content_hash.{}", h.hash_name())[..], Value::String(current_hash)) - .map_err(Box::new) - .map_err(|e| REK::StoreWriteError.into_error_with_cause(e)) + .map_err_into(REK::StoreWriteError) ); Ok(()) @@ -304,13 +295,11 @@ impl Ref for Entry { .into_iter() .map(|entry| { entry - .map_err(Box::new) - .map_err(|e| REK::IOError.into_error_with_cause(e)) + .map_err_into(REK::IOError) .and_then(|entry| { let pb = PathBuf::from(entry.path()); File::open(entry.path()) - .map_err(Box::new) - .map_err(|e| REK::IOError.into_error_with_cause(e)) + .map_err_into(REK::IOError) .map(|f| (pb, f)) }) .and_then(|(p, mut f)| h.create_hash(&p, &mut f).map(|h| (p, h))) @@ -321,8 +310,7 @@ impl Ref for Entry { None } }) - .map_err(Box::new) - .map_err(|e| REK::IOError.into_error_with_cause(e)) + .map_err_into(REK::IOError) }) .filter_map(|e| e.ok()) .filter_map(|e| e) @@ -337,17 +325,12 @@ impl Ref for Entry { /// Get the permissions of the file which are present fn get_current_permissions(&self) -> Result { self.fs_file() - .and_then(|pb| { - File::open(pb) - .map_err(Box::new) - .map_err(|e| REK::HeaderFieldReadError.into_error_with_cause(e)) - }) + .and_then(|pb| File::open(pb).map_err_into(REK::HeaderFieldReadError)) .and_then(|file| { file .metadata() .map(|md| md.permissions()) - .map_err(Box::new) - .map_err(|e| REK::RefTargetCannotReadPermissions.into_error_with_cause(e)) + .map_err_into(REK::RefTargetCannotReadPermissions) }) } diff --git a/lib/entry/libimagentryref/src/refstore.rs b/lib/entry/libimagentryref/src/refstore.rs index 5d4e1243..e486d67a 100644 --- a/lib/entry/libimagentryref/src/refstore.rs +++ b/lib/entry/libimagentryref/src/refstore.rs @@ -69,17 +69,13 @@ impl RefStore for Store { /// Check whether there is a reference to the file at `pb` fn exists(&self, pb: PathBuf) -> Result { pb.canonicalize() - .map_err(Box::new) - .map_err(|e| REK::PathCanonicalizationError.into_error_with_cause(e)) - .and_then(|can| { - hash_path(&can) - .map_err(Box::new) - .map_err(|e| REK::PathHashingError.into_error_with_cause(e)) - }) + .map_err_into(REK::PathCanonicalizationError) + .and_then(|c| hash_path(&c)) + .map_err_into(REK::PathHashingError) .and_then(|hash| { - self.retrieve_for_module("ref").map(|iter| (hash, iter)) - .map_err(Box::new) - .map_err(|e| REK::StoreReadError.into_error_with_cause(e)) + self.retrieve_for_module("ref") + .map(|iter| (hash, iter)) + .map_err_into(REK::StoreReadError) }) .and_then(|(hash, possible_refs)| { // This is kind of a manual Iterator::filter() call what we do here, but with the @@ -89,7 +85,8 @@ impl RefStore for Store { for r in possible_refs { let contains_hash = try!(r.to_str() .map_err_into(REK::TypeConversionError) - .map(|s| s.contains(&hash[..]))); + .map(|s| s.contains(&hash[..])) + ); if !contains_hash { continue; @@ -102,13 +99,8 @@ impl RefStore for Store { } }, - Ok(None) => { // Something weird just happened - return Err(REK::StoreReadError.into_error()); - }, - - Err(e) => { - return Err(REK::StoreReadError.into_error_with_cause(Box::new(e))); - }, + Ok(None) => return Err(REK::StoreReadError.into_error()), + Err(e) => return Err(e).map_err_into(REK::StoreReadError) } } @@ -119,7 +111,7 @@ impl RefStore for Store { /// Try to get `si` as Ref object from the store fn get<'a>(&'a self, si: StoreId) -> Result> { match self.get(si) { - Err(e) => return Err(REK::StoreReadError.into_error_with_cause(Box::new(e))), + Err(e) => return Err(e).map_err_into(REK::StoreReadError), Ok(None) => return Err(REK::RefNotInStore.into_error()), Ok(Some(fle)) => Ok(fle), } @@ -132,8 +124,7 @@ impl RefStore for Store { ModuleEntryPath::new(hash) .into_storeid() .and_then(|id| self.get(id)) - .map_err(Box::new) - .map_err(|e| REK::StoreReadError.into_error_with_cause(e)) + .map_err_into(REK::StoreReadError) } /// Delete a ref by hash @@ -143,8 +134,7 @@ impl RefStore for Store { ModuleEntryPath::new(hash) .into_storeid() .and_then(|id| self.delete(id)) - .map_err(Box::new) - .map_err(|e| REK::StoreWriteError.into_error_with_cause(e)) + .map_err_into(REK::StoreWriteError) } /// Create a Ref object which refers to `pb` @@ -164,8 +154,7 @@ impl RefStore for Store { let (mut fle, content_hash, permissions, canonical_path) = { // scope to be able to fold try!(File::open(pb.clone()) - .map_err(Box::new) - .map_err(|e| REK::RefTargetFileCannotBeOpened.into_error_with_cause(e)) + .map_err_into(REK::RefTargetFileCannotBeOpened) // If we were able to open this file, // we hash the contents of the file and return (file, hash) @@ -186,8 +175,7 @@ impl RefStore for Store { Some(try!(file .metadata() .map(|md| md.permissions()) - .map_err(Box::new) - .map_err(|e| REK::RefTargetCannotReadPermissions.into_error_with_cause(e)) + .map_err_into(REK::RefTargetCannotReadPermissions) )) } else { None @@ -203,16 +191,13 @@ impl RefStore for Store { pb.canonicalize() .map(|can| (opt_contenthash, opt_permissions, can)) // if PathBuf::canonicalize() failed, build an error from the return value - .map_err(|e| REK::PathCanonicalizationError.into_error_with_cause(Box::new(e))) + .map_err_into(REK::PathCanonicalizationError) }) // and then we hash the canonicalized path // and return (file, content hash, permissions, canonicalized path, path hash) .and_then(|(opt_contenthash, opt_permissions, can)| { - let path_hash = try!(hash_path(&can) - .map_err(Box::new) - .map_err(|e| REK::PathHashingError.into_error_with_cause(e)) - ); + let path_hash = try!(hash_path(&can).map_err_into(REK::PathHashingError)); Ok((opt_contenthash, opt_permissions, can, path_hash)) }) @@ -234,8 +219,7 @@ impl RefStore for Store { .and_then(|(opt_conhash, opt_perm, can, path_hash)| { let fle = try!(self .create(ModuleEntryPath::new(path_hash)) - .map_err(Box::new) - .map_err(|e| REK::StoreWriteError.into_error_with_cause(e)) + .map_err_into(REK::StoreWriteError) ); Ok((fle, opt_conhash, opt_perm, can)) @@ -262,15 +246,9 @@ impl RefStore for Store { match fle.get_header_mut().insert(s, v.clone()) { Ok(false) => { let e = REK::HeaderFieldAlreadyExistsError.into_error(); - let e = Box::new(e); - let e = REK::HeaderFieldWriteError.into_error_with_cause(e); - return Err(e); - }, - Err(e) => { - let e = Box::new(e); - let e = REK::HeaderFieldWriteError.into_error_with_cause(e); - return Err(e); + return Err(e).map_err_into(REK::HeaderFieldWriteError); }, + Err(e) => return Err(e).map_err_into(REK::HeaderFieldWriteError), _ => (), } } @@ -284,3 +262,4 @@ impl RefStore for Store { } } +