From b9800e19e964f02f27f2a2856c4cc62ac38da303 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 8 Feb 2018 15:19:29 +0100 Subject: [PATCH] Refactor imag-diary to use new error handling interface --- bin/domain/imag-diary/src/create.rs | 19 ++++++------------- bin/domain/imag-diary/src/delete.rs | 13 +++++++------ bin/domain/imag-diary/src/edit.rs | 6 +----- 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/bin/domain/imag-diary/src/create.rs b/bin/domain/imag-diary/src/create.rs index e4e22d52..b1f6f976 100644 --- a/bin/domain/imag-diary/src/create.rs +++ b/bin/domain/imag-diary/src/create.rs @@ -25,7 +25,6 @@ use libimagdiary::error::DiaryErrorKind as DEK; use libimagdiary::error::ResultExt; use libimagentryedit::edit::Edit; use libimagrt::runtime::Runtime; -use libimagerror::trace::trace_error_exit; use libimagerror::trace::MapErrTrace; use libimagutil::warn_exit::warn_exit; use libimagstore::store::FileLockEntry; @@ -50,11 +49,8 @@ pub fn create(rt: &Runtime) { .chain_err(|| DEK::DiaryEditError) }; - if let Err(e) = res { - trace_error_exit(&e, 1); - } else { - info!("Ok!"); - } + let _ = res.map_err_trace_exit_unwrap(1); + info!("Ok!"); } fn create_entry<'a>(diary: &'a Store, diaryname: &str, rt: &Runtime) -> FileLockEntry<'a> { @@ -88,13 +84,10 @@ fn create_entry<'a>(diary: &'a Store, diaryname: &str, rt: &Runtime) -> FileLock } }; - match entry { - Err(e) => trace_error_exit(&e, 1), - Ok(e) => { - debug!("Created: {}", e.get_location()); - e - } - } + let e = entry.map_err_trace_exit_unwrap(1); + + debug!("Created: {}", e.get_location()); + e } diff --git a/bin/domain/imag-diary/src/delete.rs b/bin/domain/imag-diary/src/delete.rs index 7f5c512d..f054cd2a 100644 --- a/bin/domain/imag-diary/src/delete.rs +++ b/bin/domain/imag-diary/src/delete.rs @@ -23,11 +23,11 @@ use chrono::naive::NaiveDateTime; use libimagdiary::diaryid::DiaryId; use libimagrt::runtime::Runtime; -use libimagerror::trace::trace_error_exit; use libimagtimeui::datetime::DateTime; use libimagtimeui::parse::Parse; use libimagutil::warn_exit::warn_exit; use libimagstore::storeid::IntoStoreId; +use libimagerror::trace::MapErrTrace; use util::get_diary_name; @@ -53,9 +53,9 @@ pub fn delete(rt: &Runtime) { DiaryId::from_datetime(diaryname.clone(), dt) .into_storeid() .map(|id| rt.store().retrieve(id)) - .unwrap_or_else(|e| trace_error_exit(&e, 1)) + .map_err_trace_exit_unwrap(1) }) - .unwrap_or_else(|e| trace_error_exit(&e, 1)) + .map_err_trace_exit_unwrap(1) .get_location() .clone(); @@ -64,9 +64,10 @@ pub fn delete(rt: &Runtime) { return; } - if let Err(e) = rt.store().delete(to_del_location) { - trace_error_exit(&e, 1) - } + let _ = rt + .store() + .delete(to_del_location) + .map_err_trace_exit_unwrap(1); info!("Ok!"); } diff --git a/bin/domain/imag-diary/src/edit.rs b/bin/domain/imag-diary/src/edit.rs index acd48e39..2c974056 100644 --- a/bin/domain/imag-diary/src/edit.rs +++ b/bin/domain/imag-diary/src/edit.rs @@ -32,7 +32,6 @@ use libimagerror::trace::MapErrTrace; use libimagtimeui::datetime::DateTime; use libimagtimeui::parse::Parse; use libimagutil::warn_exit::warn_exit; -use libimagerror::trace::trace_error_exit; use util::get_diary_name; @@ -49,10 +48,7 @@ pub fn edit(rt: &Runtime) { .or_else(|| { rt.store() .get_youngest_entry_id(&diaryname) - .map(|optid| match optid { - Ok(id) => id, - Err(e) => trace_error_exit(&e, 1), - }) + .map(|o| o.map_err_trace_exit_unwrap(1)) }) .ok_or_else(|| { error!("No entries in diary. Aborting");