Refactoring: Use function chaining rather than matching

This commit is contained in:
Matthias Beyer 2018-01-04 23:09:30 +01:00
parent e7d5e9ebc2
commit a386d50df3
2 changed files with 11 additions and 10 deletions

View file

@ -45,11 +45,10 @@ impl<'a> Iterator for GetTimeTrackIter<'a> {
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|sid| {
match self.store.get(sid).chain_err(|| TTEK::StoreReadError) {
Ok(None) => Err(TTE::from_kind(TTEK::StoreReadError)),
Ok(Some(s)) => Ok(s),
Err(e) => Err(e)
}
self.store
.get(sid)
.chain_err(|| TTEK::StoreReadError)?
.ok_or(TTE::from_kind(TTEK::StoreReadError))
})
}
}

View file

@ -65,11 +65,13 @@ impl TimeTracking for Entry {
fn get_timetrack_tag(&self) -> Result<TTT> {
self.get_header()
.read(DATE_TIME_TAG_HEADER_PATH)
.chain_err(|| TTEK::HeaderReadError)
.and_then(|value| match value {
Some(&Value::String(ref s)) => Ok(s.clone().into()),
Some(_) => Err(TTE::from_kind(TTEK::HeaderFieldTypeError)),
_ => Err(TTE::from_kind(TTEK::HeaderReadError))
.chain_err(|| TTEK::HeaderReadError)?
.ok_or(TTE::from_kind(TTEK::HeaderReadError))
.and_then(|v| {
v.as_str()
.map(String::from)
.map(Into::into)
.ok_or(TTE::from_kind(TTEK::HeaderFieldTypeError))
})
}