From 8071c4c72159e731c805770c64a8ef72f7651d94 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 29 Aug 2017 16:50:29 +0200 Subject: [PATCH] Refactor, minify edit() impl --- bin/domain/imag-diary/src/edit.rs | 53 +++++++++++++------------------ 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/bin/domain/imag-diary/src/edit.rs b/bin/domain/imag-diary/src/edit.rs index 3f9e59cf..c623e1fa 100644 --- a/bin/domain/imag-diary/src/edit.rs +++ b/bin/domain/imag-diary/src/edit.rs @@ -32,7 +32,6 @@ use libimagerror::into::IntoError; use libimagtimeui::datetime::DateTime; use libimagtimeui::parse::Parse; use libimagutil::warn_exit::warn_exit; -use libimagstore::storeid::IntoStoreId; use libimagerror::trace::trace_error_exit; use util::get_diary_name; @@ -40,40 +39,32 @@ use util::get_diary_name; pub fn edit(rt: &Runtime) { let diaryname = get_diary_name(rt).unwrap_or_else(|| warn_exit("No diary name", 1)); - let datetime : Option = rt - .cli() + rt.cli() .subcommand_matches("edit") .unwrap() .value_of("datetime") .and_then(DateTime::parse) - .map(|dt| dt.into()); - - let to_edit = match datetime { - Some(dt) => { - let id = match DiaryId::from_datetime(diaryname.clone(), dt).into_storeid() { - Ok(id) => id, - Err(e) => trace_error_exit(&e, 1), - }; - rt.store().get(id) - }, - None => match rt.store().get_youngest_entry_id(&diaryname) { - Some(Ok(id)) => match id.into_storeid() { - Ok(sid) => rt.store().get(sid), - Err(e) => trace_error_exit(&e, 1), - }, - Some(Err(e)) => trace_error_exit(&e, 1), - None => { - error!("No entries in diary. Aborting"); - exit(1) - } - } - }; - - to_edit.map(|opte| match opte { - Some(mut e) => e.edit_content(rt).map_err_into(DEK::IOError), - None => Err(DEK::EntryNotInDiary.into_error()), - }) - .map_err_trace().ok(); + .map(|dt| dt.into()) + .map(|dt: NaiveDateTime| DiaryId::from_datetime(diaryname.clone(), dt)) + .or_else(|| { + rt.store() + .get_youngest_entry_id(&diaryname) + .map(|optid| match optid { + Ok(id) => id, + Err(e) => trace_error_exit(&e, 1), + }) + }) + .ok_or_else(|| { + error!("No entries in diary. Aborting"); + exit(1) + }) + .and_then(|id| rt.store().get(id)) + .map(|opte| match opte { + Some(mut e) => e.edit_content(rt).map_err_into(DEK::IOError), + None => Err(DEK::EntryNotInDiary.into_error()), + }) + .map_err_trace() + .ok(); }