From 17913ae3fd5422f2be3a77064ac107ceaf0cb10a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 4 Nov 2018 22:36:35 +0100 Subject: Optimize implementation: Less matches This patch simplifies the code to be not three nested matches but rather one match and then some function chaining. Signed-off-by: Matthias Beyer --- bin/domain/imag-habit/src/main.rs | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'bin/domain') diff --git a/bin/domain/imag-habit/src/main.rs b/bin/domain/imag-habit/src/main.rs index 629afa10..7aa49664 100644 --- a/bin/domain/imag-habit/src/main.rs +++ b/bin/domain/imag-habit/src/main.rs @@ -69,6 +69,7 @@ use libimagstore::store::FileLockEntry; use libimagstore::store::Store; use libimagstore::storeid::StoreId; use libimaginteraction::ask::ask_bool; +use libimagutil::debug_result::DebugResult; mod ui; @@ -115,20 +116,16 @@ fn create(rt: &Runtime) { let date = scmd.value_of("create-date").unwrap(); // safe by clap let parsedate = |d, pname| match kairos_parse(d).map_err_trace_exit_unwrap(1) { - Parsed::TimeType(tt) => match tt.calculate() { - Ok(tt) => match tt.get_moment() { - Some(mom) => mom.date(), - None => { - debug!("TimeType yielded: '{:?}'", tt); - error!("Error: '{}' parameter does not yield a point in time", pname); - exit(1); - }, - }, - Err(e) => { - error!("Error: '{:?}'", e); - exit(1); - } - }, + Parsed::TimeType(tt) => tt.calculate() + .map_dbg(|y| format!("TimeType yielded: '{:?}'", y)) + .map_err_trace_exit_unwrap(1) + .get_moment() + .ok_or_else(|| { + error!("Error: '{}' parameter does not yield a point in time", pname); + exit(1) + }) + .unwrap() // safe by above + .date(), _ => { error!("Error: '{}' parameter does not yield a point in time", pname); exit(1); -- cgit v1.2.3