From feaa32196b83d060ba19df498dd0fc13b5d96e9b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 4 Jan 2018 23:09:30 +0100 Subject: [PATCH] Refactoring: Use function chaining rather than matching Also introduce error links for this. --- .../libimagentrycategory/src/category.rs | 23 +++++++------- lib/entry/libimagentrycategory/src/error.rs | 8 +++++ .../libimagentrycategory/src/register.rs | 30 +++++++++---------- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/lib/entry/libimagentrycategory/src/category.rs b/lib/entry/libimagentrycategory/src/category.rs index f2a2b2cf..5cef0d6d 100644 --- a/lib/entry/libimagentrycategory/src/category.rs +++ b/lib/entry/libimagentrycategory/src/category.rs @@ -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> { - 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 { diff --git a/lib/entry/libimagentrycategory/src/error.rs b/lib/entry/libimagentrycategory/src/error.rs index bd3da1a0..32642043 100644 --- a/lib/entry/libimagentrycategory/src/error.rs +++ b/lib/entry/libimagentrycategory/src/error.rs @@ -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") diff --git a/lib/entry/libimagentrycategory/src/register.rs b/lib/entry/libimagentrycategory/src/register.rs index d5b40584..bc1aa929 100644 --- a/lib/entry/libimagentrycategory/src/register.rs +++ b/lib/entry/libimagentrycategory/src/register.rs @@ -225,14 +225,13 @@ fn represents_category(store: &Store, sid: StoreId, name: &str) -> Result .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)) }) } }