imag/imag-diary/src/edit.rs

63 lines
2.1 KiB
Rust
Raw Normal View History

//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
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
}