summaryrefslogtreecommitdiffstats
path: root/bin/domain/imag-diary/src/create.rs
diff options
context:
space:
mode:
Diffstat (limited to 'bin/domain/imag-diary/src/create.rs')
-rw-r--r--bin/domain/imag-diary/src/create.rs117
1 files changed, 51 insertions, 66 deletions
diff --git a/bin/domain/imag-diary/src/create.rs b/bin/domain/imag-diary/src/create.rs
index 8a3f80a3..c6c4687f 100644
--- a/bin/domain/imag-diary/src/create.rs
+++ b/bin/domain/imag-diary/src/create.rs
@@ -24,14 +24,11 @@ use chrono::Timelike;
use failure::Error;
use failure::ResultExt;
use failure::err_msg;
+use failure::Fallible as Result;
use libimagdiary::diary::Diary;
use libimagentryedit::edit::Edit;
use libimagrt::runtime::Runtime;
-use libimagerror::trace::MapErrTrace;
-use libimagerror::exit::ExitUnwrap;
-use libimagutil::warn_exit::warn_exit;
-use libimagutil::debug_result::DebugResult;
use libimagutil::debug_option::DebugOption;
use libimagstore::store::FileLockEntry;
use libimagstore::store::Store;
@@ -40,60 +37,46 @@ use crate::util::get_diary_name;
use crate::util::get_diary_timed_config;
use crate::util::Timed;
-pub fn create(rt: &Runtime) {
+pub fn create(rt: &Runtime) -> Result<()> {
let diaryname = get_diary_name(rt)
- .unwrap_or_else( || warn_exit("No diary selected. Use either the configuration file or the commandline option", 1));
+ .ok_or_else(|| err_msg("No diary selected. Use either the configuration file or the commandline option"))?;
- let mut entry = create_entry(rt.store(), &diaryname, rt);
+ let mut entry = create_entry(rt.store(), &diaryname, rt)?;
+ rt.report_touched(entry.get_location())?;
- rt.report_touched(entry.get_location()).unwrap_or_exit();
-
- let res = if rt.cli().subcommand_matches("create").unwrap().is_present("no-edit") {
+ if rt.cli().subcommand_matches("create").unwrap().is_present("no-edit") {
debug!("Not editing new diary entry");
+ info!("Ok!");
Ok(())
} else {
debug!("Editing new diary entry");
entry.edit_content(rt).context(err_msg("Diary edit error")).map_err(Error::from)
- };
-
- res.map_err_trace_exit_unwrap();
- info!("Ok!");
+ }
}
-fn create_entry<'a>(diary: &'a Store, diaryname: &str, rt: &Runtime) -> FileLockEntry<'a> {
+fn create_entry<'a>(diary: &'a Store, diaryname: &str, rt: &Runtime) -> Result<FileLockEntry<'a>> {
use crate::util::parse_timed_string;
let create = rt.cli().subcommand_matches("create").unwrap();
- create.value_of("timed")
- .map(|t| parse_timed_string(t, diaryname).map_err_trace_exit_unwrap())
- .map(Some)
- .unwrap_or_else(|| {
- match get_diary_timed_config(rt, diaryname).map_err_trace_exit_unwrap() {
- Some(t) => Some(t),
- None => {
- warn!("Missing config: 'diary.diaries.{}.timed'", diaryname);
- warn!("Assuming 'false'");
- None
- }
- }
- })
- .map(|timed| {
- let time = create_id_from_clispec(&create, timed);
- diary.new_entry_at(&diaryname, &time)
- .context(err_msg("Store write error"))
- .map_err(Error::from)
- })
- .unwrap_or_else(|| {
- debug!("Creating non-timed entry");
- diary.new_entry_today(diaryname)
- })
- .map_dbg(|e| format!("Created: {}", e.get_location()))
- .map_err_trace_exit_unwrap()
+ let timed = match create.value_of("timed").map(|t| parse_timed_string(t, diaryname)) {
+ Some(t) => t.map(Some),
+ None => get_diary_timed_config(rt, diaryname)
+ }?;
+
+ if let Some(timed) = timed {
+ let time = create_id_from_clispec(&create, timed)?;
+ diary.new_entry_at(&diaryname, &time)
+ .context(err_msg("Store write error"))
+ .map_err(Error::from)
+ } else {
+ debug!("Creating non-timed entry");
+ diary.new_entry_today(diaryname)
+ }
}
-fn create_id_from_clispec(create: &ArgMatches, timed_type: Timed) -> NaiveDateTime {
+fn create_id_from_clispec(create: &ArgMatches, timed_type: Timed) -> Result<NaiveDateTime> {
use std::str::FromStr;
let dt = Local::now();
@@ -102,71 +85,73 @@ fn create_id_from_clispec(create: &ArgMatches, timed_type: Timed) -> NaiveDateTi
match timed_type {
Timed::Daily => {
debug!("Creating daily-timed entry");
- ndt.with_hour(0)
+ Ok(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
+ .unwrap()) // safe because second = 0 is safe
},
Timed::Hourly => {
debug!("Creating hourly-timed entry");
- ndt.with_minute(0)
+ Ok(ndt.with_minute(0)
.unwrap() // safe because minute = 0 is safe
.with_second(0)
- .unwrap() // safe because second = 0 is safe
+ .unwrap()) // safe because second = 0 is safe
},
Timed::Minutely => {
let min = create
.value_of("minute")
.map_dbg(|m| format!("minute = {:?}", m))
- .and_then(|s| {
+ .map(|s| {
FromStr::from_str(s)
- .map_err(|_| warn!("Could not parse minute: '{}'", s))
- .ok()
+ .map_err(Error::from)
+ .context(format_err!("Could not parse minute: '{}'", s))
+ .map_err(Error::from)
})
+ .transpose()?
.unwrap_or_else(|| ndt.minute());
ndt.with_minute(min)
- .unwrap_or_else(|| {
- error!("Cannot set {} as minute, would yield invalid time!", min);
- ::std::process::exit(1)
+ .ok_or_else(|| {
+ format_err!("Cannot set {} as minute, would yield invalid time!", min)
})
- .with_second(0)
- .unwrap() // safe because second = 0 is safe
+ .map(|ndt| ndt.with_second(0).unwrap()) // safe because second = 0 is safe
},
Timed::Secondly => {
let min = create
.value_of("minute")
.map_dbg(|m| format!("minute = {:?}", m))
- .and_then(|s| {
+ .map(|s| {
FromStr::from_str(s)
- .map_err(|_| warn!("Could not parse minute: '{}'", s))
- .ok()
+ .map_err(Error::from)
+ .context(format_err!("Could not parse minute: '{}'", s))
+ .map_err(Error::from)
})
+ .transpose()?
.unwrap_or_else(|| ndt.minute());
let sec = create
.value_of("second")
.map_dbg(|s| format!("second = {:?}", s))
- .and_then(|s| {
+ .map(|s| {
FromStr::from_str(s)
- .map_err(|_| warn!("Could not parse second: '{}'", s))
- .ok()
+ .map_err(Error::from)
+ .context(format_err!("Could not parse second: '{}'", s))
+ .map_err(Error::from)
})
+ .transpose()?
.unwrap_or_else(|| ndt.second());
ndt.with_minute(min)
- .unwrap_or_else(|| {
- error!("Cannot set {} as minute, would yield invalid time!", min);
- ::std::process::exit(1)
- })
+ .ok_or_else(|| {
+ format_err!("Cannot set {} as minute, would yield invalid time!", min)
+ })?
.with_second(sec)
- .unwrap_or_else(|| {
- error!("Cannot set {} as second, would yield invalid time!", sec);
- ::std::process::exit(1)
+ .ok_or_else(|| {
+ format_err!("Cannot set {} as second, would yield invalid time!", sec)
})
},
}