Refactoring: Use function chaining rather than matching

This commit is contained in:
Matthias Beyer 2018-01-04 23:09:30 +01:00
parent a386d50df3
commit dd66936e48

View file

@ -25,7 +25,6 @@ use error::Result;
use libimagstore::store::Entry;
use uuid::Uuid;
use toml::Value;
use toml_query::read::TomlValueReadExt;
pub trait Task {
@ -34,14 +33,13 @@ pub trait Task {
impl Task for Entry {
fn get_uuid(&self) -> Result<Uuid> {
match self.get_header().read("todo.uuid") {
Ok(Some(&Value::String(ref uuid))) => {
Uuid::parse_str(uuid).chain_err(|| TEK::UuidParserError)
},
Ok(Some(_)) => Err(TE::from_kind(TEK::HeaderTypeError)),
Ok(None) => Err(TE::from_kind(TEK::HeaderFieldMissing)),
Err(e) => Err(e).chain_err(|| TEK::StoreError),
}
self.get_header()
.read("todo.uuid")
.chain_err(|| TEK::StoreError)?
.ok_or(TE::from_kind(TEK::HeaderFieldMissing))?
.as_str()
.ok_or(TE::from_kind(TEK::HeaderTypeError))
.and_then(|u| Uuid::parse_str(u).chain_err(|| TEK::UuidParserError))
}
}