Merge pull request #696 from matthiasbeyer/imag-diary/refactor
imag-diary/refactor
This commit is contained in:
commit
56576c16bb
6 changed files with 28 additions and 34 deletions
|
@ -2,8 +2,8 @@ use std::process::exit;
|
||||||
|
|
||||||
use libimagdiary::diary::Diary;
|
use libimagdiary::diary::Diary;
|
||||||
use libimagdiary::diaryid::DiaryId;
|
use libimagdiary::diaryid::DiaryId;
|
||||||
use libimagdiary::error::DiaryError as DE;
|
|
||||||
use libimagdiary::error::DiaryErrorKind as DEK;
|
use libimagdiary::error::DiaryErrorKind as DEK;
|
||||||
|
use libimagdiary::error::MapErrInto;
|
||||||
use libimagentryedit::edit::Edit;
|
use libimagentryedit::edit::Edit;
|
||||||
use libimagrt::runtime::Runtime;
|
use libimagrt::runtime::Runtime;
|
||||||
use libimagerror::trace::trace_error;
|
use libimagerror::trace::trace_error;
|
||||||
|
@ -13,13 +13,11 @@ use libimagdiary::result::Result;
|
||||||
use util::get_diary_name;
|
use util::get_diary_name;
|
||||||
|
|
||||||
pub fn create(rt: &Runtime) {
|
pub fn create(rt: &Runtime) {
|
||||||
|
let diaryname = get_diary_name(rt)
|
||||||
let diaryname = get_diary_name(rt);
|
.unwrap_or_else(|| {
|
||||||
if diaryname.is_none() {
|
|
||||||
warn!("No diary selected. Use either the configuration file or the commandline option");
|
warn!("No diary selected. Use either the configuration file or the commandline option");
|
||||||
exit(1);
|
exit(1)
|
||||||
}
|
});
|
||||||
let diaryname = diaryname.unwrap();
|
|
||||||
|
|
||||||
let prevent_edit = rt.cli().subcommand_matches("create").unwrap().is_present("no-edit");
|
let prevent_edit = rt.cli().subcommand_matches("create").unwrap().is_present("no-edit");
|
||||||
|
|
||||||
|
@ -98,8 +96,7 @@ pub fn create(rt: &Runtime) {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
debug!("Editing new diary entry");
|
debug!("Editing new diary entry");
|
||||||
entry.edit_content(rt)
|
entry.edit_content(rt).map_err_into(DEK::DiaryEditError)
|
||||||
.map_err(|e| DE::new(DEK::DiaryEditError, Some(Box::new(e))))
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -13,12 +13,10 @@ use util::get_diary_name;
|
||||||
pub fn delete(rt: &Runtime) {
|
pub fn delete(rt: &Runtime) {
|
||||||
use libimaginteraction::ask::ask_bool;
|
use libimaginteraction::ask::ask_bool;
|
||||||
|
|
||||||
let diaryname = get_diary_name(rt);
|
let diaryname = get_diary_name(rt).unwrap_or_else(|| {
|
||||||
if diaryname.is_none() {
|
|
||||||
warn!("No diary selected. Use either the configuration file or the commandline option");
|
warn!("No diary selected. Use either the configuration file or the commandline option");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
});
|
||||||
let diaryname = diaryname.unwrap();
|
|
||||||
|
|
||||||
let diary = Diary::open(rt.store(), &diaryname[..]);
|
let diary = Diary::open(rt.store(), &diaryname[..]);
|
||||||
debug!("Diary opened: {:?}", diary);
|
debug!("Diary opened: {:?}", diary);
|
||||||
|
@ -52,9 +50,10 @@ pub fn delete(rt: &Runtime) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
match diary.delete_entry(to_del) {
|
if let Err(e) = diary.delete_entry(to_del) {
|
||||||
Ok(_) => info!("Ok"),
|
trace_error_exit(&e, 1)
|
||||||
Err(e) => trace_error_exit(&e, 1),
|
}
|
||||||
}
|
|
||||||
|
info!("Ok!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,23 +3,23 @@ use chrono::naive::datetime::NaiveDateTime;
|
||||||
|
|
||||||
use libimagdiary::diary::Diary;
|
use libimagdiary::diary::Diary;
|
||||||
use libimagdiary::diaryid::DiaryId;
|
use libimagdiary::diaryid::DiaryId;
|
||||||
use libimagdiary::error::DiaryError as DE;
|
|
||||||
use libimagdiary::error::DiaryErrorKind as DEK;
|
use libimagdiary::error::DiaryErrorKind as DEK;
|
||||||
|
use libimagdiary::error::MapErrInto;
|
||||||
use libimagentryedit::edit::Edit;
|
use libimagentryedit::edit::Edit;
|
||||||
use libimagrt::runtime::Runtime;
|
use libimagrt::runtime::Runtime;
|
||||||
use libimagerror::trace::trace_error;
|
use libimagerror::trace::trace_error;
|
||||||
|
use libimagerror::into::IntoError;
|
||||||
use libimagtimeui::datetime::DateTime;
|
use libimagtimeui::datetime::DateTime;
|
||||||
use libimagtimeui::parse::Parse;
|
use libimagtimeui::parse::Parse;
|
||||||
|
|
||||||
use util::get_diary_name;
|
use util::get_diary_name;
|
||||||
|
|
||||||
pub fn edit(rt: &Runtime) {
|
pub fn edit(rt: &Runtime) {
|
||||||
let diaryname = get_diary_name(rt);
|
let diaryname = get_diary_name(rt).unwrap_or_else(|| {
|
||||||
if diaryname.is_none() {
|
|
||||||
warn!("No diary name");
|
warn!("No diary name");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
});
|
||||||
let diaryname = diaryname.unwrap();
|
|
||||||
let diary = Diary::open(rt.store(), &diaryname[..]);
|
let diary = Diary::open(rt.store(), &diaryname[..]);
|
||||||
|
|
||||||
let datetime : Option<NaiveDateTime> = rt
|
let datetime : Option<NaiveDateTime> = rt
|
||||||
|
@ -36,10 +36,10 @@ pub fn edit(rt: &Runtime) {
|
||||||
};
|
};
|
||||||
|
|
||||||
match to_edit {
|
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),
|
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();
|
.map_err(|e| trace_error(&e)).ok();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
|
||||||
use libimagdiary::diary::Diary;
|
use libimagdiary::diary::Diary;
|
||||||
use libimagdiary::error::DiaryError as DE;
|
|
||||||
use libimagdiary::error::DiaryErrorKind as DEK;
|
use libimagdiary::error::DiaryErrorKind as DEK;
|
||||||
|
use libimagdiary::error::MapErrInto;
|
||||||
use libimagentrylist::listers::core::CoreLister;
|
use libimagentrylist::listers::core::CoreLister;
|
||||||
use libimagentrylist::lister::Lister;
|
use libimagentrylist::lister::Lister;
|
||||||
use libimagrt::runtime::Runtime;
|
use libimagrt::runtime::Runtime;
|
||||||
|
@ -12,12 +12,10 @@ use libimagerror::trace::trace_error;
|
||||||
use util::get_diary_name;
|
use util::get_diary_name;
|
||||||
|
|
||||||
pub fn list(rt: &Runtime) {
|
pub fn list(rt: &Runtime) {
|
||||||
let diaryname = get_diary_name(rt);
|
let diaryname = get_diary_name(rt).unwrap_or_else(|| {
|
||||||
if diaryname.is_none() {
|
|
||||||
warn!("No diary selected. Use either the configuration file or the commandline option");
|
warn!("No diary selected. Use either the configuration file or the commandline option");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
});
|
||||||
let diaryname = diaryname.unwrap();
|
|
||||||
|
|
||||||
fn entry_to_location_listing_string(e: &Entry) -> String {
|
fn entry_to_location_listing_string(e: &Entry) -> String {
|
||||||
e.get_location().clone()
|
e.get_location().clone()
|
||||||
|
@ -40,7 +38,7 @@ pub fn list(rt: &Runtime) {
|
||||||
|
|
||||||
CoreLister::new(&entry_to_location_listing_string)
|
CoreLister::new(&entry_to_location_listing_string)
|
||||||
.list(es) // TODO: Do not ignore non-ok()s
|
.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(|_| debug!("Ok"))
|
||||||
.map_err(|e| trace_error(&e))
|
.map_err(|e| trace_error(&e))
|
||||||
|
|
|
@ -9,12 +9,11 @@ use libimagerror::trace::trace_error;
|
||||||
use util::get_diary_name;
|
use util::get_diary_name;
|
||||||
|
|
||||||
pub fn view(rt: &Runtime) {
|
pub fn view(rt: &Runtime) {
|
||||||
let diaryname = get_diary_name(rt);
|
let diaryname = get_diary_name(rt).unwrap_or_else(|| {
|
||||||
if diaryname.is_none() {
|
|
||||||
warn!("No diary name");
|
warn!("No diary name");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
});
|
||||||
let diaryname = diaryname.unwrap();
|
|
||||||
let diary = Diary::open(rt.store(), &diaryname[..]);
|
let diary = Diary::open(rt.store(), &diaryname[..]);
|
||||||
let show_header = rt.cli().subcommand_matches("view").unwrap().is_present("show-header");
|
let show_header = rt.cli().subcommand_matches("view").unwrap().is_present("show-header");
|
||||||
|
|
||||||
|
|
|
@ -13,4 +13,5 @@ generate_error_module!(
|
||||||
|
|
||||||
pub use self::error::DiaryError;
|
pub use self::error::DiaryError;
|
||||||
pub use self::error::DiaryErrorKind;
|
pub use self::error::DiaryErrorKind;
|
||||||
|
pub use self::error::MapErrInto;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue