Rewrite internal link handling, returning Result<_> rather than Option<_>

This commit is contained in:
Matthias Beyer 2016-08-28 13:53:49 +02:00
parent d594b7c6a2
commit 3e4b0e4bb8

View file

@ -54,13 +54,11 @@ impl InternalLinker for Entry {
.into_iter() .into_iter()
.fold(Ok(vec![]), |acc, elem| { .fold(Ok(vec![]), |acc, elem| {
acc.and_then(move |mut v| { acc.and_then(move |mut v| {
match elem { elem.map_err_into(LEK::InternalConversionError)
None => Err(LEK::InternalConversionError.into()), .map(|e| {
Some(e) => {
v.push(e); v.push(e);
Ok(v) v
}, })
}
}) })
})); }));
process_rw_result(self.get_header_mut().set("imag.links", Value::Array(new_links))) 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>> { fn links_into_values(links: Vec<StoreId>) -> Vec<Result<Value>> {
use libimagutil::debug_result::InfoResult;
links links
.into_iter() .into_iter()
.map(|s| {
s.to_str()
.map_dbg_err(|e| format!("Failed to translate StoreId to String: {:?}", e))
.ok()
})
.unique() .unique()
.map(|s| s.to_str().map_err_into(LEK::InternalConversionError))
.map(|elem| elem.map(Value::String)) .map(|elem| elem.map(Value::String))
.sorted_by(|a, b| { .sorted_by(|a, b| {
match (a, b) { match (a, b) {
(&Some(Value::String(ref a)), &Some(Value::String(ref b))) => Ord::cmp(a, b), (&Ok(Value::String(ref a)), &Ok(Value::String(ref b))) => Ord::cmp(a, b),
(&None, _) | (_, &None) => Ordering::Equal, (&Err(_), _) | (_, &Err(_)) => Ordering::Equal,
_ => unreachable!() _ => unreachable!()
} }
}) })
} }
@ -125,13 +117,11 @@ fn rewrite_links(header: &mut EntryHeader, links: Vec<StoreId>) -> Result<()> {
.into_iter() .into_iter()
.fold(Ok(vec![]), |acc, elem| { .fold(Ok(vec![]), |acc, elem| {
acc.and_then(move |mut v| { acc.and_then(move |mut v| {
match elem { elem.map_err_into(LEK::InternalConversionError)
None => Err(LEK::InternalConversionError.into()), .map(|e| {
Some(e) => {
v.push(e); v.push(e);
Ok(v) v
}, })
}
}) })
})); }));
@ -149,13 +139,11 @@ fn add_foreign_link(target: &mut Entry, from: StoreId) -> Result<()> {
.into_iter() .into_iter()
.fold(Ok(vec![]), |acc, elem| { .fold(Ok(vec![]), |acc, elem| {
acc.and_then(move |mut v| { acc.and_then(move |mut v| {
match elem { elem.map_err_into(LEK::InternalConversionError)
None => Err(LEK::InternalConversionError.into()), .map(|e| {
Some(e) => {
v.push(e); v.push(e);
Ok(v) v
}, })
}
}) })
})); }));
process_rw_result(target.get_header_mut().set("imag.links", Value::Array(links))) process_rw_result(target.get_header_mut().set("imag.links", Value::Array(links)))