Merge pull request #696 from matthiasbeyer/imag-diary/refactor

imag-diary/refactor
This commit is contained in:
Matthias Beyer 2016-09-05 20:30:14 +02:00 committed by GitHub
commit 56576c16bb
6 changed files with 28 additions and 34 deletions

View file

@ -2,8 +2,8 @@ use std::process::exit;
use libimagdiary::diary::Diary;
use libimagdiary::diaryid::DiaryId;
use libimagdiary::error::DiaryError as DE;
use libimagdiary::error::DiaryErrorKind as DEK;
use libimagdiary::error::MapErrInto;
use libimagentryedit::edit::Edit;
use libimagrt::runtime::Runtime;
use libimagerror::trace::trace_error;
@ -13,13 +13,11 @@ use libimagdiary::result::Result;
use util::get_diary_name;
pub fn create(rt: &Runtime) {
let diaryname = get_diary_name(rt);
if diaryname.is_none() {
warn!("No diary selected. Use either the configuration file or the commandline option");
exit(1);
}
let diaryname = diaryname.unwrap();
let diaryname = get_diary_name(rt)
.unwrap_or_else(|| {
warn!("No diary selected. Use either the configuration file or the commandline option");
exit(1)
});
let prevent_edit = rt.cli().subcommand_matches("create").unwrap().is_present("no-edit");
@ -98,8 +96,7 @@ pub fn create(rt: &Runtime) {
Ok(())
} else {
debug!("Editing new diary entry");
entry.edit_content(rt)
.map_err(|e| DE::new(DEK::DiaryEditError, Some(Box::new(e))))
entry.edit_content(rt).map_err_into(DEK::DiaryEditError)
}
});

View file

@ -13,12 +13,10 @@ use util::get_diary_name;
pub fn delete(rt: &Runtime) {
use libimaginteraction::ask::ask_bool;
let diaryname = get_diary_name(rt);
if diaryname.is_none() {
let diaryname = get_diary_name(rt).unwrap_or_else(|| {
warn!("No diary selected. Use either the configuration file or the commandline option");
exit(1);
}
let diaryname = diaryname.unwrap();
});
let diary = Diary::open(rt.store(), &diaryname[..]);
debug!("Diary opened: {:?}", diary);
@ -52,9 +50,10 @@ pub fn delete(rt: &Runtime) {
return;
}
match diary.delete_entry(to_del) {
Ok(_) => info!("Ok"),
Err(e) => trace_error_exit(&e, 1),
if let Err(e) = diary.delete_entry(to_del) {
trace_error_exit(&e, 1)
}
info!("Ok!");
}

View file

@ -3,23 +3,23 @@ use chrono::naive::datetime::NaiveDateTime;
use libimagdiary::diary::Diary;
use libimagdiary::diaryid::DiaryId;
use libimagdiary::error::DiaryError as DE;
use libimagdiary::error::DiaryErrorKind as DEK;
use libimagdiary::error::MapErrInto;
use libimagentryedit::edit::Edit;
use libimagrt::runtime::Runtime;
use libimagerror::trace::trace_error;
use libimagerror::into::IntoError;
use libimagtimeui::datetime::DateTime;
use libimagtimeui::parse::Parse;
use util::get_diary_name;
pub fn edit(rt: &Runtime) {
let diaryname = get_diary_name(rt);
if diaryname.is_none() {
let diaryname = get_diary_name(rt).unwrap_or_else(|| {
warn!("No diary name");
exit(1);
}
let diaryname = diaryname.unwrap();
});
let diary = Diary::open(rt.store(), &diaryname[..]);
let datetime : Option<NaiveDateTime> = rt
@ -36,10 +36,10 @@ pub fn edit(rt: &Runtime) {
};
match to_edit {
Some(Ok(mut e)) => e.edit_content(rt).map_err(|e| DE::new(DEK::IOError, Some(Box::new(e)))),
Some(Ok(mut e)) => e.edit_content(rt).map_err_into(DEK::IOError),
Some(Err(e)) => Err(e),
None => Err(DE::new(DEK::EntryNotInDiary, None)),
None => Err(DEK::EntryNotInDiary.into_error()),
}
.map_err(|e| trace_error(&e)).ok();
}

View file

@ -1,8 +1,8 @@
use std::process::exit;
use libimagdiary::diary::Diary;
use libimagdiary::error::DiaryError as DE;
use libimagdiary::error::DiaryErrorKind as DEK;
use libimagdiary::error::MapErrInto;
use libimagentrylist::listers::core::CoreLister;
use libimagentrylist::lister::Lister;
use libimagrt::runtime::Runtime;
@ -12,12 +12,10 @@ use libimagerror::trace::trace_error;
use util::get_diary_name;
pub fn list(rt: &Runtime) {
let diaryname = get_diary_name(rt);
if diaryname.is_none() {
let diaryname = get_diary_name(rt).unwrap_or_else(|| {
warn!("No diary selected. Use either the configuration file or the commandline option");
exit(1);
}
let diaryname = diaryname.unwrap();
});
fn entry_to_location_listing_string(e: &Entry) -> String {
e.get_location().clone()
@ -40,7 +38,7 @@ pub fn list(rt: &Runtime) {
CoreLister::new(&entry_to_location_listing_string)
.list(es) // TODO: Do not ignore non-ok()s
.map_err(|e| DE::new(DEK::IOError, Some(Box::new(e))))
.map_err_into(DEK::IOError)
})
.map(|_| debug!("Ok"))
.map_err(|e| trace_error(&e))

View file

@ -9,12 +9,11 @@ use libimagerror::trace::trace_error;
use util::get_diary_name;
pub fn view(rt: &Runtime) {
let diaryname = get_diary_name(rt);
if diaryname.is_none() {
let diaryname = get_diary_name(rt).unwrap_or_else(|| {
warn!("No diary name");
exit(1);
}
let diaryname = diaryname.unwrap();
});
let diary = Diary::open(rt.store(), &diaryname[..]);
let show_header = rt.cli().subcommand_matches("view").unwrap().is_present("show-header");

View file

@ -13,4 +13,5 @@ generate_error_module!(
pub use self::error::DiaryError;
pub use self::error::DiaryErrorKind;
pub use self::error::MapErrInto;