From a386d50df3415f0d07e1c7ba7f4d51fb8eb518ad 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 --- lib/domain/libimagtimetrack/src/iter/get.rs | 9 ++++----- lib/domain/libimagtimetrack/src/timetracking.rs | 12 +++++++----- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/domain/libimagtimetrack/src/iter/get.rs b/lib/domain/libimagtimetrack/src/iter/get.rs index 526c1c97..739e07a2 100644 --- a/lib/domain/libimagtimetrack/src/iter/get.rs +++ b/lib/domain/libimagtimetrack/src/iter/get.rs @@ -45,11 +45,10 @@ impl<'a> Iterator for GetTimeTrackIter<'a> { fn next(&mut self) -> Option { 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)) }) } } diff --git a/lib/domain/libimagtimetrack/src/timetracking.rs b/lib/domain/libimagtimetrack/src/timetracking.rs index 7c3bf11d..bbf8b06f 100644 --- a/lib/domain/libimagtimetrack/src/timetracking.rs +++ b/lib/domain/libimagtimetrack/src/timetracking.rs @@ -65,11 +65,13 @@ impl TimeTracking for Entry { fn get_timetrack_tag(&self) -> Result { 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)) }) }