imag/imag-diary/src/edit.rs

44 lines
1.2 KiB
Rust
Raw Normal View History

2016-06-08 17:40:13 +00:00
use chrono::naive::datetime::NaiveDateTime;
use libimagdiary::diary::Diary;
use libimagdiary::diaryid::DiaryId;
use libimagdiary::error::DiaryErrorKind as DEK;
2016-09-04 15:54:04 +00:00
use libimagdiary::error::MapErrInto;
use libimagentryedit::edit::Edit;
2016-06-08 17:40:13 +00:00
use libimagrt::runtime::Runtime;
use libimagerror::trace::MapErrTrace;
2016-09-04 15:54:04 +00:00
use libimagerror::into::IntoError;
2016-06-08 17:40:13 +00:00
use libimagtimeui::datetime::DateTime;
use libimagtimeui::parse::Parse;
2016-09-05 17:30:21 +00:00
use libimagutil::warn_exit::warn_exit;
2016-06-08 17:40:13 +00:00
use util::get_diary_name;
pub fn edit(rt: &Runtime) {
2016-09-05 17:30:21 +00:00
let diaryname = get_diary_name(rt).unwrap_or_else(|| warn_exit("No diary name", 1));
2016-06-08 17:40:13 +00:00
let diary = Diary::open(rt.store(), &diaryname[..]);
let datetime : Option<NaiveDateTime> = rt
.cli()
.subcommand_matches("edit")
.unwrap()
.value_of("datetime")
.and_then(DateTime::parse)
.map(|dt| dt.into());
let to_edit = match datetime {
Some(dt) => Some(diary.retrieve(DiaryId::from_datetime(diaryname.clone(), dt))),
None => diary.get_youngest_entry(),
};
match to_edit {
2016-09-04 15:54:04 +00:00
Some(Ok(mut e)) => e.edit_content(rt).map_err_into(DEK::IOError),
2016-06-08 17:40:13 +00:00
Some(Err(e)) => Err(e),
2016-09-04 15:54:04 +00:00
None => Err(DEK::EntryNotInDiary.into_error()),
2016-06-08 17:40:13 +00:00
}
.map_err_trace().ok();
2016-06-08 17:40:13 +00:00
}