Merge pull request #616 from matthiasbeyer/libimagentrylink/remove-unwrap

libimagentrylink - unwrap() cleanup
This commit is contained in:
Matthias Beyer 2016-08-03 13:05:36 +02:00 committed by GitHub
commit b45c4d4d0b
3 changed files with 52 additions and 29 deletions

View File

@ -13,4 +13,5 @@ generate_error_module!(
pub use self::error::LinkError;
pub use self::error::LinkErrorKind;
pub use self::error::MapErrInto;

View File

@ -19,9 +19,11 @@ use libimagstore::store::FileLockEntry;
use libimagstore::store::Store;
use libimagstore::storeid::StoreId;
use libimagstore::storeid::IntoStoreId;
use libimagutil::debug_result::*;
use error::LinkError as LE;
use error::LinkErrorKind as LEK;
use error::MapErrInto;
use result::Result;
use internal::InternalLinker;
use module_path::ModuleEntryPath;
@ -150,12 +152,12 @@ impl ExternalLinker for Entry {
// retrieve the file from the store, which implicitely creates the entry if it does not
// exist
let file = store.retrieve(file_id.clone());
if file.is_err() {
debug!("Failed to create or retrieve an file for this link '{:?}'", link);
return Err(LE::new(LEK::StoreWriteError, Some(Box::new(file.unwrap_err()))));
}
let mut file = file.unwrap();
let mut file = try!(store
.retrieve(file_id.clone())
.map_err_into(LEK::StoreWriteError)
.map_dbg_err(|_| {
format!("Failed to create or retrieve an file for this link '{:?}'", link)
}));
debug!("Generating header content!");
{

View File

@ -49,11 +49,19 @@ impl InternalLinker for Entry {
new_links.push(link);
}
let new_links = links_into_values(new_links);
if new_links.iter().any(|o| o.is_none()) {
return Err(LEK::InternalConversionError.into());
}
let new_links = new_links.into_iter().map(|o| o.unwrap()).collect();
let new_links = try!(links_into_values(new_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)
},
}
})
}));
process_rw_result(self.get_header_mut().set("imag.links", Value::Array(new_links)))
}
@ -106,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);
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)
},
}
})
}));
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 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.
@ -125,14 +138,21 @@ fn add_foreign_link(target: &mut Entry, from: StoreId) -> Result<()> {
target.get_internal_links()
.and_then(|mut links| {
links.push(from);
let links = links_into_values(links);
if links.iter().any(|o| o.is_none()) {
Err(LEK::InternalConversionError.into())
} else {
let links = links.into_iter().map(|opt| opt.unwrap()).collect();
process_rw_result(target.get_header_mut().set("imag.links", Value::Array(links)))
.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)
},
}
})
}));
process_rw_result(target.get_header_mut().set("imag.links", Value::Array(links)))
.map(|_| ())
})
}