From 22d63f0946026aaad93daff370e1910b50f0e8c0 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 8 Jun 2018 02:19:55 +0200 Subject: [PATCH 1/2] Add functionality to create entry at a certain time --- bin/domain/imag-diary/src/create.rs | 9 +++++---- lib/domain/libimagdiary/src/diary.rs | 6 ++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/bin/domain/imag-diary/src/create.rs b/bin/domain/imag-diary/src/create.rs index 148c4b5a..bc39765b 100644 --- a/bin/domain/imag-diary/src/create.rs +++ b/bin/domain/imag-diary/src/create.rs @@ -18,9 +18,10 @@ // use clap::ArgMatches; +use chrono::NaiveDateTime; +use chrono::Local; use libimagdiary::diary::Diary; -use libimagdiary::diaryid::DiaryId; use libimagdiary::error::DiaryErrorKind as DEK; use libimagdiary::error::ResultExt; use libimagentryedit::edit::Edit; @@ -130,7 +131,7 @@ fn create_id_from_clispec(create: &ArgMatches, diaryname: &str, timed_type: Time .map_err(|_| warn!("Could not parse minute: '{}'", s)) .ok() }) - .unwrap_or(time.minute()); + .unwrap_or(ndt.minute()); time.with_minute(min) .with_second(0) @@ -146,7 +147,7 @@ fn create_id_from_clispec(create: &ArgMatches, diaryname: &str, timed_type: Time .map_err(|_| warn!("Could not parse minute: '{}'", s)) .ok() }) - .unwrap_or(time.minute()); + .unwrap_or(ndt.minute()); let sec = create .value_of("second") @@ -156,7 +157,7 @@ fn create_id_from_clispec(create: &ArgMatches, diaryname: &str, timed_type: Time .map_err(|_| warn!("Could not parse second: '{}'", s)) .ok() }) - .unwrap_or(time.second()); + .unwrap_or(ndt.second()); time.with_minute(min).with_second(sec) }, diff --git a/lib/domain/libimagdiary/src/diary.rs b/lib/domain/libimagdiary/src/diary.rs index ea4b2491..779c7d53 100644 --- a/lib/domain/libimagdiary/src/diary.rs +++ b/lib/domain/libimagdiary/src/diary.rs @@ -47,6 +47,8 @@ pub trait Diary { // create or get a new entry for now fn new_entry_now(&self, diary_name: &str) -> Result; + fn new_entry_at(&self, diary_name: &str, ndt: &NaiveDateTime) -> Result; + // Get an iterator for iterating over all entries of a Diary fn entries(&self, diary_name: &str) -> Result; @@ -73,6 +75,10 @@ impl Diary for Store { fn new_entry_now(&self, diary_name: &str) -> Result { let dt = Local::now(); let ndt = dt.naive_local(); + self.new_entry_at(diary_name, &ndt) + } + + fn new_entry_at(&self, diary_name: &str, ndt: &NaiveDateTime) -> Result { let id = DiaryId::new(String::from(diary_name), ndt.year(), ndt.month(), From b896bc2657a7f39283f4b82b8a4a565e108f2fdd Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 8 Jun 2018 02:20:00 +0200 Subject: [PATCH 2/2] Fix: Do not create entries with Store::retrieve() This patch fixes a bug where entries where created with `Store::retrieve()` rather than with the API from libimagdiary. This caused headers to be missing. Now, the CLI is parsed for the values passed and a NaiveDateTime object is crafted from that, which is then passed to libimagdiary. --- bin/domain/imag-diary/src/create.rs | 54 ++++++++++++++++------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/bin/domain/imag-diary/src/create.rs b/bin/domain/imag-diary/src/create.rs index bc39765b..4f861cec 100644 --- a/bin/domain/imag-diary/src/create.rs +++ b/bin/domain/imag-diary/src/create.rs @@ -20,6 +20,7 @@ use clap::ArgMatches; use chrono::NaiveDateTime; use chrono::Local; +use chrono::Timelike; use libimagdiary::diary::Diary; use libimagdiary::error::DiaryErrorKind as DEK; @@ -73,8 +74,8 @@ fn create_entry<'a>(diary: &'a Store, diaryname: &str, rt: &Runtime) -> FileLock } }) .map(|timed| { - let id = create_id_from_clispec(&create, &diaryname, timed); - diary.retrieve(id).chain_err(|| DEK::StoreReadError) + let time = create_id_from_clispec(&create, &diaryname, timed); + diary.new_entry_at(&diaryname, &time).chain_err(|| DEK::StoreWriteError) }) .unwrap_or_else(|| { debug!("Creating non-timed entry"); @@ -88,41 +89,31 @@ fn create_entry<'a>(diary: &'a Store, diaryname: &str, rt: &Runtime) -> FileLock } -fn create_id_from_clispec(create: &ArgMatches, diaryname: &str, timed_type: Timed) -> DiaryId { +fn create_id_from_clispec(create: &ArgMatches, diaryname: &str, timed_type: Timed) -> NaiveDateTime { use std::str::FromStr; - let get_hourly_id = |create: &ArgMatches| -> DiaryId { - let time = DiaryId::now(String::from(diaryname)); - let hr = create - .value_of("hour") - .map(|v| { debug!("Creating hourly entry with hour = {:?}", v); v }) - .and_then(|s| { - FromStr::from_str(s) - .map_err(|_| warn!("Could not parse hour: '{}'", s)) - .ok() - }) - .unwrap_or(time.hour()); - - time.with_hour(hr) - }; + let dt = Local::now(); + let ndt = dt.naive_local(); match timed_type { Timed::Daily => { debug!("Creating daily-timed entry"); - get_hourly_id(create) - .with_hour(0) + ndt.with_hour(0) + .unwrap() // safe because hour = 0 is safe .with_minute(0) + .unwrap() // safe because minute = 0 is safe .with_second(0) + .unwrap() // safe because second = 0 is safe }, Timed::Hourly => { debug!("Creating hourly-timed entry"); - get_hourly_id(create) - .with_minute(0) + ndt.with_minute(0) + .unwrap() // safe because minute = 0 is safe .with_second(0) + .unwrap() // safe because second = 0 is safe }, Timed::Minutely => { - let time = get_hourly_id(create); let min = create .value_of("minute") .map(|m| { debug!("minute = {:?}", m); m }) @@ -133,12 +124,16 @@ fn create_id_from_clispec(create: &ArgMatches, diaryname: &str, timed_type: Time }) .unwrap_or(ndt.minute()); - time.with_minute(min) + ndt.with_minute(min) + .unwrap_or_else(|| { + error!("Cannot set {} as minute, would yield invalid time!", min); + ::std::process::exit(1) + }) .with_second(0) + .unwrap() // safe because second = 0 is safe }, Timed::Secondly => { - let time = get_hourly_id(create); let min = create .value_of("minute") .map(|m| { debug!("minute = {:?}", m); m }) @@ -159,7 +154,16 @@ fn create_id_from_clispec(create: &ArgMatches, diaryname: &str, timed_type: Time }) .unwrap_or(ndt.second()); - time.with_minute(min).with_second(sec) + ndt.with_minute(min) + .unwrap_or_else(|| { + error!("Cannot set {} as minute, would yield invalid time!", min); + ::std::process::exit(1) + }) + .with_second(sec) + .unwrap_or_else(|| { + error!("Cannot set {} as second, would yield invalid time!", sec); + ::std::process::exit(1) + }) }, } }