Refactoring: Use function chaining rather than matching

Also introduce error links for this.
This commit is contained in:
Matthias Beyer 2018-01-04 23:09:30 +01:00
parent 3294a77346
commit feaa32196b
3 changed files with 34 additions and 27 deletions

View file

@ -19,7 +19,6 @@
use toml_query::insert::TomlValueInsertExt;
use toml_query::read::TomlValueReadExt;
use toml_query::error::ErrorKind as TQEK;
use toml::Value;
use libimagstore::store::Entry;
@ -81,17 +80,17 @@ impl EntryCategory for Entry {
}
fn get_category(&self) -> Result<Option<Category>> {
match self.get_header().read(&String::from("category.value")) {
Err(res) => match res.kind() {
&TQEK::IdentifierNotFoundInDocument(_) => Ok(None),
_ => Err(res),
}
.chain_err(|| CEK::HeaderReadError),
Ok(Some(&Value::String(ref s))) => Ok(Some(s.clone().into())),
Ok(None) => Err(CE::from_kind(CEK::StoreReadError)).chain_err(|| CEK::HeaderReadError),
Ok(_) => Err(CE::from_kind(CEK::TypeError)).chain_err(|| CEK::HeaderReadError),
}
self.get_header()
.read("category.value")
.chain_err(|| CEK::HeaderReadError)
.and_then(|opt| {
opt.map(|v| {
v.as_str()
.map(String::from)
.map(Category::from)
})
.ok_or(CE::from_kind(CEK::TypeError))
})
}
fn has_category(&self) -> Result<bool> {

View file

@ -22,6 +22,14 @@ error_chain! {
CategoryError, CategoryErrorKind, ResultExt, Result;
}
links {
StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind);
}
foreign_links {
TomlQueryError(::toml_query::error::Error);
}
errors {
StoreReadError {
description("Store Read error")

View file

@ -225,14 +225,13 @@ fn represents_category(store: &Store, sid: StoreId, name: &str) -> Result<bool>
.chain_err(|| CEK::StoreReadError)
.and_then(|fle| {
if let Some(fle) = fle {
match fle.get_header()
fle.get_header()
.read(&String::from(CATEGORY_REGISTER_NAME_FIELD_PATH))
.chain_err(|| CEK::HeaderReadError)
{
Ok(Some(&Value::String(ref s))) => Ok(s == name),
Ok(_) => Err(CE::from_kind(CEK::TypeError)),
Err(e) => Err(e).chain_err(|| CEK::HeaderReadError),
}
.chain_err(|| CEK::HeaderReadError)?
.ok_or(CE::from_kind(CEK::TypeError))?
.as_str()
.map(|s| s == name)
.ok_or(CE::from_kind(CEK::TypeError))
} else {
Ok(false)
}
@ -276,14 +275,15 @@ impl<'a> Iterator for CategoryNameIter<'a> {
.next()
.map(|sid| {
self.0
.get(sid)
.chain_err(|| CEK::StoreReadError)
.and_then(|fle| fle.ok_or(CE::from_kind(CEK::StoreReadError)))
.and_then(|fle| match fle.get_header().read(&query) {
Ok(Some(&Value::String(ref s))) => Ok(Category::from(s.clone())),
Ok(_) => Err(CE::from_kind(CEK::TypeError)),
Err(e) => Err(e).chain_err(|| CEK::HeaderReadError),
})
.get(sid)?
.ok_or_else(|| CE::from_kind(CEK::StoreReadError))?
.get_header()
.read(&query)
.chain_err(|| CEK::HeaderReadError)?
.and_then(Value::as_str)
.map(String::from)
.map(Category::from)
.ok_or_else(|| CE::from_kind(CEK::TypeError))
})
}
}