Refactoring: Use function chaining rather than matching
Also introduce error links for this.
This commit is contained in:
parent
3294a77346
commit
feaa32196b
3 changed files with 34 additions and 27 deletions
|
@ -19,7 +19,6 @@
|
||||||
|
|
||||||
use toml_query::insert::TomlValueInsertExt;
|
use toml_query::insert::TomlValueInsertExt;
|
||||||
use toml_query::read::TomlValueReadExt;
|
use toml_query::read::TomlValueReadExt;
|
||||||
use toml_query::error::ErrorKind as TQEK;
|
|
||||||
use toml::Value;
|
use toml::Value;
|
||||||
|
|
||||||
use libimagstore::store::Entry;
|
use libimagstore::store::Entry;
|
||||||
|
@ -81,17 +80,17 @@ impl EntryCategory for Entry {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_category(&self) -> Result<Option<Category>> {
|
fn get_category(&self) -> Result<Option<Category>> {
|
||||||
match self.get_header().read(&String::from("category.value")) {
|
self.get_header()
|
||||||
Err(res) => match res.kind() {
|
.read("category.value")
|
||||||
&TQEK::IdentifierNotFoundInDocument(_) => Ok(None),
|
.chain_err(|| CEK::HeaderReadError)
|
||||||
_ => Err(res),
|
.and_then(|opt| {
|
||||||
}
|
opt.map(|v| {
|
||||||
.chain_err(|| CEK::HeaderReadError),
|
v.as_str()
|
||||||
|
.map(String::from)
|
||||||
Ok(Some(&Value::String(ref s))) => Ok(Some(s.clone().into())),
|
.map(Category::from)
|
||||||
Ok(None) => Err(CE::from_kind(CEK::StoreReadError)).chain_err(|| CEK::HeaderReadError),
|
})
|
||||||
Ok(_) => Err(CE::from_kind(CEK::TypeError)).chain_err(|| CEK::HeaderReadError),
|
.ok_or(CE::from_kind(CEK::TypeError))
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn has_category(&self) -> Result<bool> {
|
fn has_category(&self) -> Result<bool> {
|
||||||
|
|
|
@ -22,6 +22,14 @@ error_chain! {
|
||||||
CategoryError, CategoryErrorKind, ResultExt, Result;
|
CategoryError, CategoryErrorKind, ResultExt, Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
links {
|
||||||
|
StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreign_links {
|
||||||
|
TomlQueryError(::toml_query::error::Error);
|
||||||
|
}
|
||||||
|
|
||||||
errors {
|
errors {
|
||||||
StoreReadError {
|
StoreReadError {
|
||||||
description("Store Read error")
|
description("Store Read error")
|
||||||
|
|
|
@ -225,14 +225,13 @@ fn represents_category(store: &Store, sid: StoreId, name: &str) -> Result<bool>
|
||||||
.chain_err(|| CEK::StoreReadError)
|
.chain_err(|| CEK::StoreReadError)
|
||||||
.and_then(|fle| {
|
.and_then(|fle| {
|
||||||
if let Some(fle) = fle {
|
if let Some(fle) = fle {
|
||||||
match fle.get_header()
|
fle.get_header()
|
||||||
.read(&String::from(CATEGORY_REGISTER_NAME_FIELD_PATH))
|
.read(&String::from(CATEGORY_REGISTER_NAME_FIELD_PATH))
|
||||||
.chain_err(|| CEK::HeaderReadError)
|
.chain_err(|| CEK::HeaderReadError)?
|
||||||
{
|
.ok_or(CE::from_kind(CEK::TypeError))?
|
||||||
Ok(Some(&Value::String(ref s))) => Ok(s == name),
|
.as_str()
|
||||||
Ok(_) => Err(CE::from_kind(CEK::TypeError)),
|
.map(|s| s == name)
|
||||||
Err(e) => Err(e).chain_err(|| CEK::HeaderReadError),
|
.ok_or(CE::from_kind(CEK::TypeError))
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
Ok(false)
|
Ok(false)
|
||||||
}
|
}
|
||||||
|
@ -276,14 +275,15 @@ impl<'a> Iterator for CategoryNameIter<'a> {
|
||||||
.next()
|
.next()
|
||||||
.map(|sid| {
|
.map(|sid| {
|
||||||
self.0
|
self.0
|
||||||
.get(sid)
|
.get(sid)?
|
||||||
.chain_err(|| CEK::StoreReadError)
|
.ok_or_else(|| CE::from_kind(CEK::StoreReadError))?
|
||||||
.and_then(|fle| fle.ok_or(CE::from_kind(CEK::StoreReadError)))
|
.get_header()
|
||||||
.and_then(|fle| match fle.get_header().read(&query) {
|
.read(&query)
|
||||||
Ok(Some(&Value::String(ref s))) => Ok(Category::from(s.clone())),
|
.chain_err(|| CEK::HeaderReadError)?
|
||||||
Ok(_) => Err(CE::from_kind(CEK::TypeError)),
|
.and_then(Value::as_str)
|
||||||
Err(e) => Err(e).chain_err(|| CEK::HeaderReadError),
|
.map(String::from)
|
||||||
})
|
.map(Category::from)
|
||||||
|
.ok_or_else(|| CE::from_kind(CEK::TypeError))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue