Refactor, minify edit() impl

This commit is contained in:
Matthias Beyer 2017-08-29 16:50:29 +02:00
parent 356c86fd51
commit 8071c4c721

View file

@ -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<NaiveDateTime> = 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();
}