summaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2018-07-27 20:10:59 +0200
committerGitHub <noreply@github.com>2018-07-27 20:10:59 +0200
commitacdc1e84bc3082579d844947a978485152b4f6e6 (patch)
treec41b1f1722e9e83207f6bcf53131f158260b6824 /bin
parent2a62b6dffb9f82a464cc7d9e21a488aee04197ca (diff)
parentb896bc2657a7f39283f4b82b8a4a565e108f2fdd (diff)
Merge pull request #1495 from matthiasbeyer/imag-diary/fix-missing-header
Imag diary/fix missing header
Diffstat (limited to 'bin')
-rw-r--r--bin/domain/imag-diary/src/create.rs63
1 files changed, 34 insertions, 29 deletions
diff --git a/bin/domain/imag-diary/src/create.rs b/bin/domain/imag-diary/src/create.rs
index 148c4b5a..4f861cec 100644
--- a/bin/domain/imag-diary/src/create.rs
+++ b/bin/domain/imag-diary/src/create.rs
@@ -18,9 +18,11 @@
//
use clap::ArgMatches;
+use chrono::NaiveDateTime;
+use chrono::Local;
+use chrono::Timelike;
use libimagdiary::diary::Diary;
-use libimagdiary::diaryid::DiaryId;
use libimagdiary::error::DiaryErrorKind as DEK;
use libimagdiary::error::ResultExt;
use libimagentryedit::edit::Edit;
@@ -72,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");
@@ -87,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 })
@@ -130,14 +122,18 @@ 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)
+ 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 })
@@ -146,7 +142,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,9 +152,18 @@ 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)
+ 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)
+ })
},
}
}