diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-08-28 13:53:49 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-08-31 09:20:09 +0200 |
commit | 3e4b0e4bb8939358dfa95b392ba247cf2ebb9280 (patch) | |
tree | 5c89b2901a70508c303aab2a287e9e4288df81dd /libimagentrylink | |
parent | d594b7c6a252f127fb6db0ae005fe61f4eb7a390 (diff) |
Rewrite internal link handling, returning Result<_> rather than Option<_>
Diffstat (limited to 'libimagentrylink')
-rw-r--r-- | libimagentrylink/src/internal.rs | 46 |
1 files changed, 17 insertions, 29 deletions
diff --git a/libimagentrylink/src/internal.rs b/libimagentrylink/src/internal.rs index d77599ca..90344cf3 100644 --- a/libimagentrylink/src/internal.rs +++ b/libimagentrylink/src/internal.rs @@ -54,13 +54,11 @@ impl InternalLinker for Entry { .into_iter() .fold(Ok(vec![]), |acc, elem| { acc.and_then(move |mut v| { - match elem { - None => Err(LEK::InternalConversionError.into()), - Some(e) => { + elem.map_err_into(LEK::InternalConversionError) + .map(|e| { v.push(e); - Ok(v) - }, - } + v + }) }) })); process_rw_result(self.get_header_mut().set("imag.links", Value::Array(new_links))) @@ -99,23 +97,17 @@ impl InternalLinker for Entry { } -fn links_into_values(links: Vec<StoreId>) -> Vec<Option<Value>> { - use libimagutil::debug_result::InfoResult; - +fn links_into_values(links: Vec<StoreId>) -> Vec<Result<Value>> { links .into_iter() - .map(|s| { - s.to_str() - .map_dbg_err(|e| format!("Failed to translate StoreId to String: {:?}", e)) - .ok() - }) .unique() + .map(|s| s.to_str().map_err_into(LEK::InternalConversionError)) .map(|elem| elem.map(Value::String)) .sorted_by(|a, b| { match (a, b) { - (&Some(Value::String(ref a)), &Some(Value::String(ref b))) => Ord::cmp(a, b), - (&None, _) | (_, &None) => Ordering::Equal, - _ => unreachable!() + (&Ok(Value::String(ref a)), &Ok(Value::String(ref b))) => Ord::cmp(a, b), + (&Err(_), _) | (_, &Err(_)) => Ordering::Equal, + _ => unreachable!() } }) } @@ -125,13 +117,11 @@ fn rewrite_links(header: &mut EntryHeader, links: Vec<StoreId>) -> Result<()> { .into_iter() .fold(Ok(vec![]), |acc, elem| { acc.and_then(move |mut v| { - match elem { - None => Err(LEK::InternalConversionError.into()), - Some(e) => { + elem.map_err_into(LEK::InternalConversionError) + .map(|e| { v.push(e); - Ok(v) - }, - } + v + }) }) })); @@ -149,13 +139,11 @@ fn add_foreign_link(target: &mut Entry, from: StoreId) -> Result<()> { .into_iter() .fold(Ok(vec![]), |acc, elem| { acc.and_then(move |mut v| { - match elem { - None => Err(LEK::InternalConversionError.into()), - Some(e) => { + elem.map_err_into(LEK::InternalConversionError) + .map(|e| { v.push(e); - Ok(v) - }, - } + v + }) }) })); process_rw_result(target.get_header_mut().set("imag.links", Value::Array(links))) |