Replace uses of try!() macro with "?" operator

This commit is contained in:
Matthias Beyer 2017-10-30 20:17:22 +01:00
parent 81ceb50f4a
commit 078936191b
6 changed files with 24 additions and 27 deletions

View file

@ -49,8 +49,8 @@ impl RefFlags {
} }
Ok(RefFlags { Ok(RefFlags {
content_hashing: try!(get_field(v, "ref.flags.content_hashing")), content_hashing: get_field(v, "ref.flags.content_hashing")?,
permission_tracking: try!(get_field(v, "ref.flags.permission_tracking")), permission_tracking: get_field(v, "ref.flags.permission_tracking")?,
}) })
} }

View file

@ -55,7 +55,7 @@ impl Hasher for DefaultHasher {
fn create_hash<R: Read>(&mut self, _: &PathBuf, c: &mut R) -> Result<String> { fn create_hash<R: Read>(&mut self, _: &PathBuf, c: &mut R) -> Result<String> {
let mut s = String::new(); let mut s = String::new();
try!(c.read_to_string(&mut s)); c.read_to_string(&mut s)?;
self.hasher.input_str(&s[..]); self.hasher.input_str(&s[..]);
Ok(self.hasher.result_str()) Ok(self.hasher.result_str())
} }

View file

@ -51,12 +51,12 @@ impl Hasher for NBytesHasher {
} }
fn create_hash<R: Read>(&mut self, _: &PathBuf, contents: &mut R) -> Result<String> { fn create_hash<R: Read>(&mut self, _: &PathBuf, contents: &mut R) -> Result<String> {
let s : String = try!(contents let s : String = contents
.bytes() .bytes()
.take(self.n) .take(self.n)
.collect::<RResult<Vec<u8>, _>>() .collect::<RResult<Vec<u8>, _>>()
.map_err(From::from) .map_err(From::from)
.and_then(|v| String::from_utf8(v).map_err(RE::from))); .and_then(|v| String::from_utf8(v).map_err(RE::from))?;
self.hasher.input_str(&s[..]); self.hasher.input_str(&s[..]);
Ok(self.hasher.result_str()) Ok(self.hasher.result_str())

View file

@ -89,7 +89,7 @@ impl Lister for RefLister {
debug!("Listing Entry: {:?}", entry); debug!("Listing Entry: {:?}", entry);
{ {
let is_dead = if self.check_dead { let is_dead = if self.check_dead {
if try!(lerror::ResultExt::chain_err(entry.fs_link_exists(), || LEK::FormatError)) { if lerror::ResultExt::chain_err(entry.fs_link_exists(), || LEK::FormatError)? {
"dead" "dead"
} else { } else {
"alive" "alive"

View file

@ -219,8 +219,8 @@ impl Ref for Entry {
/// Check whether the Hashsum of the referenced file is equal to the stored hashsum /// Check whether the Hashsum of the referenced file is equal to the stored hashsum
fn fs_link_valid_hash(&self) -> Result<bool> { fn fs_link_valid_hash(&self) -> Result<bool> {
let stored_hash = try!(self.get_stored_hash()); let stored_hash = self.get_stored_hash()?;
let current_hash = try!(self.get_current_hash()); let current_hash = self.get_current_hash()?;
Ok(stored_hash == current_hash) Ok(stored_hash == current_hash)
} }
@ -233,18 +233,18 @@ impl Ref for Entry {
/// Update the Ref by re-checking the file from FS using the passed Hasher instance /// Update the Ref by re-checking the file from FS using the passed Hasher instance
/// This errors if the file is not present or cannot be read() /// This errors if the file is not present or cannot be read()
fn update_ref_with_hasher<H: Hasher>(&mut self, h: &H) -> Result<()> { fn update_ref_with_hasher<H: Hasher>(&mut self, h: &H) -> Result<()> {
let current_hash = try!(self.get_current_hash()); // uses the default hasher let current_hash = self.get_current_hash()?; // uses the default hasher
let current_perm = try!(self.get_current_permissions()); let current_perm = self.get_current_permissions()?;
try!(self self
.get_header_mut() .get_header_mut()
.set("ref.permissions.ro", Value::Boolean(current_perm.readonly())) .set("ref.permissions.ro", Value::Boolean(current_perm.readonly()))
); ?;
try!(self self
.get_header_mut() .get_header_mut()
.set(&format!("ref.content_hash.{}", h.hash_name())[..], Value::String(current_hash)) .set(&format!("ref.content_hash.{}", h.hash_name())[..], Value::String(current_hash))
); ?;
Ok(()) Ok(())
} }

View file

@ -91,10 +91,10 @@ impl RefStore for Store {
// manually here. If you can come up with a better version of this, feel free to // manually here. If you can come up with a better version of this, feel free to
// take this note as a todo. // take this note as a todo.
for r in possible_refs { for r in possible_refs {
let contains_hash = try!(r.to_str() let contains_hash = r.to_str()
.chain_err(|| REK::TypeConversionError) .chain_err(|| REK::TypeConversionError)
.map(|s| s.contains(&hash[..])) .map(|s| s.contains(&hash[..]))
); ?;
if !contains_hash { if !contains_hash {
continue; continue;
@ -182,14 +182,14 @@ impl RefStore for Store {
} }
let (mut fle, content_hash, permissions, canonical_path) = { // scope to be able to fold let (mut fle, content_hash, permissions, canonical_path) = { // scope to be able to fold
try!(File::open(pb.clone()) File::open(pb.clone())
.chain_err(|| REK::RefTargetFileCannotBeOpened) .chain_err(|| REK::RefTargetFileCannotBeOpened)
// If we were able to open this file, // If we were able to open this file,
// we hash the contents of the file and return (file, hash) // we hash the contents of the file and return (file, hash)
.and_then(|mut file| { .and_then(|mut file| {
let opt_contenthash = if flags.get_content_hashing() { let opt_contenthash = if flags.get_content_hashing() {
Some(try!(h.create_hash(&pb, &mut file))) Some(h.create_hash(&pb, &mut file)?)
} else { } else {
None None
}; };
@ -201,11 +201,9 @@ impl RefStore for Store {
// and return (file, content hash, permissions) // and return (file, content hash, permissions)
.and_then(|(file, opt_contenthash)| { .and_then(|(file, opt_contenthash)| {
let opt_permissions = if flags.get_permission_tracking() { let opt_permissions = if flags.get_permission_tracking() {
Some(try!(file Some(file.metadata()
.metadata() .map(|md| md.permissions())
.map(|md| md.permissions()) .chain_err(|| REK::RefTargetCannotReadPermissions)?)
.chain_err(|| REK::RefTargetCannotReadPermissions)
))
} else { } else {
None None
}; };
@ -226,7 +224,7 @@ impl RefStore for Store {
// and then we hash the canonicalized path // and then we hash the canonicalized path
// and return (file, content hash, permissions, canonicalized path, path hash) // and return (file, content hash, permissions, canonicalized path, path hash)
.and_then(|(opt_contenthash, opt_permissions, can)| { .and_then(|(opt_contenthash, opt_permissions, can)| {
let path_hash = try!(hash_path(&can).chain_err(|| REK::PathHashingError)); let path_hash = hash_path(&can).chain_err(|| REK::PathHashingError)?;
Ok((opt_contenthash, opt_permissions, can, path_hash)) Ok((opt_contenthash, opt_permissions, can, path_hash))
}) })
@ -246,10 +244,9 @@ impl RefStore for Store {
// and then we create the FileLockEntry in the Store // and then we create the FileLockEntry in the Store
// and return (filelockentry, content hash, permissions, canonicalized path) // and return (filelockentry, content hash, permissions, canonicalized path)
.and_then(|(opt_conhash, opt_perm, can, path_hash)| { .and_then(|(opt_conhash, opt_perm, can, path_hash)| {
let fle = try!(self.create(ModuleEntryPath::new(path_hash))); let fle = self.create(ModuleEntryPath::new(path_hash))?;
Ok((fle, opt_conhash, opt_perm, can)) Ok((fle, opt_conhash, opt_perm, can))
}) })?
)
}; };
for tpl in [ for tpl in [