Adapt imag-diary to compile with the new libimagdiary API

This commit is contained in:
Matthias Beyer 2017-08-29 16:04:11 +02:00
parent 31fa07d43a
commit 21c15ca207
6 changed files with 74 additions and 42 deletions

View file

@ -26,9 +26,10 @@ use libimagdiary::error::MapErrInto;
use libimagentryedit::edit::Edit;
use libimagrt::runtime::Runtime;
use libimagerror::trace::trace_error;
use libimagdiary::entry::Entry;
use libimagdiary::result::Result;
use libimagerror::trace::trace_error_exit;
use libimagutil::warn_exit::warn_exit;
use libimagstore::store::FileLockEntry;
use libimagstore::store::Store;
use util::get_diary_name;
@ -38,18 +39,18 @@ pub fn create(rt: &Runtime) {
let prevent_edit = rt.cli().subcommand_matches("create").unwrap().is_present("no-edit");
fn create_entry<'a>(diary: &'a Diary, rt: &Runtime) -> Result<Entry<'a>> {
fn create_entry<'a>(diary: &'a Store, diaryname: &str, rt: &Runtime) -> FileLockEntry<'a> {
use std::str::FromStr;
let create = rt.cli().subcommand_matches("create").unwrap();
if !create.is_present("timed") {
let entry = if !create.is_present("timed") {
debug!("Creating non-timed entry");
diary.new_entry_today()
diary.new_entry_today(diaryname)
} else {
let id = match create.value_of("timed") {
Some("h") | Some("hourly") => {
debug!("Creating hourly-timed entry");
let time = DiaryId::now(String::from(diary.name()));
let time = DiaryId::now(String::from(diaryname));
let hr = create
.value_of("hour")
.map(|v| { debug!("Creating hourly entry with hour = {:?}", v); v })
@ -65,7 +66,7 @@ pub fn create(rt: &Runtime) {
Some("m") | Some("minutely") => {
debug!("Creating minutely-timed entry");
let time = DiaryId::now(String::from(diary.name()));
let time = DiaryId::now(String::from(diaryname));
let hr = create
.value_of("hour")
.map(|h| { debug!("hour = {:?}", h); h })
@ -98,21 +99,27 @@ pub fn create(rt: &Runtime) {
None => warn_exit("Unexpected error, cannot continue", 1)
};
diary.new_entry_by_id(id)
diary.retrieve(id).map_err_into(DEK::StoreReadError)
};
match entry {
Err(e) => trace_error_exit(&e, 1),
Ok(e) => {
debug!("Created: {}", e.get_location());
e
}
}
}
let diary = Diary::open(rt.store(), &diaryname[..]);
let res = create_entry(&diary, rt)
.and_then(|mut entry| {
if prevent_edit {
debug!("Not editing new diary entry");
Ok(())
} else {
debug!("Editing new diary entry");
entry.edit_content(rt).map_err_into(DEK::DiaryEditError)
}
});
let mut entry = create_entry(rt.store(), &diaryname, rt);
let res = if prevent_edit {
debug!("Not editing new diary entry");
Ok(())
} else {
debug!("Editing new diary entry");
entry.edit_content(rt).map_err_into(DEK::DiaryEditError)
};
if let Err(e) = res {
trace_error(&e);

View file

@ -17,15 +17,17 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
use std::process::exit;
use chrono::naive::NaiveDateTime;
use libimagdiary::diary::Diary;
use libimagdiary::diaryid::DiaryId;
use libimagrt::runtime::Runtime;
use libimagerror::trace::trace_error_exit;
use libimagtimeui::datetime::DateTime;
use libimagtimeui::parse::Parse;
use libimagutil::warn_exit::warn_exit;
use libimagstore::storeid::IntoStoreId;
use util::get_diary_name;
@ -35,9 +37,6 @@ pub fn delete(rt: &Runtime) {
let diaryname = get_diary_name(rt)
.unwrap_or_else(|| warn_exit("No diary selected. Use either the configuration file or the commandline option", 1));
let diary = Diary::open(rt.store(), &diaryname[..]);
debug!("Diary opened: {:?}", diary);
let datetime : Option<NaiveDateTime> = rt
.cli()
.subcommand_matches("delete")
@ -48,8 +47,17 @@ pub fn delete(rt: &Runtime) {
.map(|dt| dt.into());
let to_del = match datetime {
Some(dt) => Some(diary.retrieve(DiaryId::from_datetime(diaryname.clone(), dt))),
None => diary.get_youngest_entry(),
Some(dt) => {
let id = match DiaryId::from_datetime(diaryname.clone(), dt).into_storeid() {
Ok(id) => id,
Err(e) => trace_error_exit(&e, 1),
};
Some(rt.store().retrieve(id))
},
None => {
warn!("Not deleting entries, because missing date/time specification");
exit(1);
},
};
let to_del = match to_del {
@ -59,12 +67,15 @@ pub fn delete(rt: &Runtime) {
None => warn_exit("No entry", 1)
};
if !ask_bool(&format!("Deleting {:?}", to_del.get_location())[..], Some(true)) {
let location = to_del.get_location().clone();
drop(to_del);
if !ask_bool(&format!("Deleting {:?}", location), Some(true)) {
info!("Aborting delete action");
return;
}
if let Err(e) = diary.delete_entry(to_del) {
if let Err(e) = rt.store().delete(location) {
trace_error_exit(&e, 1)
}

View file

@ -17,6 +17,8 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
use std::process::exit;
use chrono::naive::NaiveDateTime;
use libimagdiary::diary::Diary;
@ -30,12 +32,13 @@ 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;
pub fn edit(rt: &Runtime) {
let diaryname = get_diary_name(rt).unwrap_or_else(|| warn_exit("No diary name", 1));
let diary = Diary::open(rt.store(), &diaryname[..]);
let datetime : Option<NaiveDateTime> = rt
.cli()
@ -46,16 +49,30 @@ pub fn edit(rt: &Runtime) {
.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(),
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)
}
}
};
match to_edit {
Some(Ok(mut e)) => e.edit_content(rt).map_err_into(DEK::IOError),
Some(Err(e)) => Err(e),
None => Err(DEK::EntryNotInDiary.into_error()),
}
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();
}

View file

@ -42,9 +42,7 @@ pub fn list(rt: &Runtime) {
.unwrap_or(String::from("<<Path Parsing Error>>"))
}
let diary = Diary::open(rt.store(), &diaryname[..]);
debug!("Diary opened: {:?}", diary);
diary.entries()
Diary::entries(rt.store(), &diaryname)
.and_then(|es| {
debug!("Iterator for listing: {:?}", es);

View file

@ -27,10 +27,9 @@ use util::get_diary_name;
pub fn view(rt: &Runtime) {
let diaryname = get_diary_name(rt).unwrap_or_else(|| warn_exit("No diary name", 1));
let diary = Diary::open(rt.store(), &diaryname[..]);
let hdr = rt.cli().subcommand_matches("view").unwrap().is_present("show-header");
diary.entries()
Diary::entries(rt.store(), &diaryname)
.and_then(|entries| DV::new(hdr).view_entries(entries.into_iter().filter_map(Result::ok)))
.map_err_trace()
.ok();

View file

@ -37,7 +37,7 @@ use result::Result;
use iter::DiaryEntryIterator;
use iter::DiaryNameIterator;
trait Diary {
pub trait Diary {
// create or get a new entry for today
fn new_entry_today(&self, diary_name: &str) -> Result<FileLockEntry>;