diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-08-03 11:29:38 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-08-03 11:29:38 +0200 |
commit | 41bf063a71b431abfa4e35c541386cda4f7bb803 (patch) | |
tree | a9313cce385c2c5c7c907da1835e74813bc42645 | |
parent | 80491ff0b7ab183d23cf4a178c8c5d8fb8afef35 (diff) |
Replace map(unwrap()) with fold()
-rw-r--r-- | libimagentrylink/src/internal.rs | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/libimagentrylink/src/internal.rs b/libimagentrylink/src/internal.rs index 3e8c1161..0f5a12ce 100644 --- a/libimagentrylink/src/internal.rs +++ b/libimagentrylink/src/internal.rs @@ -114,17 +114,22 @@ fn links_into_values(links: Vec<StoreId>) -> Vec<Option<Value>> { } fn rewrite_links(header: &mut EntryHeader, links: Vec<StoreId>) -> Result<()> { - let links = links_into_values(links); - - if links.iter().any(|o| o.is_none()) { - // if any type convert failed we fail as well - Err(LEK::InternalConversionError.into()) - } else { - // I know it is ugly - let links = links.into_iter().map(|opt| opt.unwrap()).collect(); - let process = header.set("imag.links", Value::Array(links)); - process_rw_result(process).map(|_| ()) - } + let links = try!(links_into_values(links) + .into_iter() + .fold(Ok(vec![]), |acc, elem| { + acc.and_then(move |mut v| { + match elem { + None => Err(LEK::InternalConversionError.into()), + Some(e) => { + v.push(e); + Ok(v) + }, + } + }) + })); + + let process = header.set("imag.links", Value::Array(links)); + process_rw_result(process).map(|_| ()) } /// When Linking A -> B, the specification wants us to link back B -> A. |