From 21c15ca2070b35ea91b52208e8a1217404fd8458 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 29 Aug 2017 16:04:11 +0200 Subject: [PATCH] Adapt imag-diary to compile with the new libimagdiary API --- bin/domain/imag-diary/src/create.rs | 45 ++++++++++++++++------------ bin/domain/imag-diary/src/delete.rs | 27 ++++++++++++----- bin/domain/imag-diary/src/edit.rs | 35 ++++++++++++++++------ bin/domain/imag-diary/src/list.rs | 4 +-- bin/domain/imag-diary/src/view.rs | 3 +- lib/domain/libimagdiary/src/diary.rs | 2 +- 6 files changed, 74 insertions(+), 42 deletions(-) diff --git a/bin/domain/imag-diary/src/create.rs b/bin/domain/imag-diary/src/create.rs index 78f679a7..61b892b6 100644 --- a/bin/domain/imag-diary/src/create.rs +++ b/bin/domain/imag-diary/src/create.rs @@ -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> { + 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); diff --git a/bin/domain/imag-diary/src/delete.rs b/bin/domain/imag-diary/src/delete.rs index da44bc92..514700f7 100644 --- a/bin/domain/imag-diary/src/delete.rs +++ b/bin/domain/imag-diary/src/delete.rs @@ -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 = 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) } diff --git a/bin/domain/imag-diary/src/edit.rs b/bin/domain/imag-diary/src/edit.rs index 1cd5f9e5..3f9e59cf 100644 --- a/bin/domain/imag-diary/src/edit.rs +++ b/bin/domain/imag-diary/src/edit.rs @@ -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 = 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(); } diff --git a/bin/domain/imag-diary/src/list.rs b/bin/domain/imag-diary/src/list.rs index 4ed3123c..90a8dd96 100644 --- a/bin/domain/imag-diary/src/list.rs +++ b/bin/domain/imag-diary/src/list.rs @@ -42,9 +42,7 @@ pub fn list(rt: &Runtime) { .unwrap_or(String::from("<>")) } - 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); diff --git a/bin/domain/imag-diary/src/view.rs b/bin/domain/imag-diary/src/view.rs index 041a1fe0..1e60383b 100644 --- a/bin/domain/imag-diary/src/view.rs +++ b/bin/domain/imag-diary/src/view.rs @@ -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(); diff --git a/lib/domain/libimagdiary/src/diary.rs b/lib/domain/libimagdiary/src/diary.rs index cf738e33..c67467cc 100644 --- a/lib/domain/libimagdiary/src/diary.rs +++ b/lib/domain/libimagdiary/src/diary.rs @@ -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;