Refactor, minify create() impl
This commit is contained in:
parent
7e7cf8ecf8
commit
356c86fd51
1 changed files with 75 additions and 78 deletions
|
@ -19,13 +19,14 @@
|
||||||
|
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
|
||||||
|
use clap::ArgMatches;
|
||||||
|
|
||||||
use libimagdiary::diary::Diary;
|
use libimagdiary::diary::Diary;
|
||||||
use libimagdiary::diaryid::DiaryId;
|
use libimagdiary::diaryid::DiaryId;
|
||||||
use libimagdiary::error::DiaryErrorKind as DEK;
|
use libimagdiary::error::DiaryErrorKind as DEK;
|
||||||
use libimagdiary::error::MapErrInto;
|
use libimagdiary::error::MapErrInto;
|
||||||
use libimagentryedit::edit::Edit;
|
use libimagentryedit::edit::Edit;
|
||||||
use libimagrt::runtime::Runtime;
|
use libimagrt::runtime::Runtime;
|
||||||
use libimagerror::trace::trace_error;
|
|
||||||
use libimagerror::trace::trace_error_exit;
|
use libimagerror::trace::trace_error_exit;
|
||||||
use libimagutil::warn_exit::warn_exit;
|
use libimagutil::warn_exit::warn_exit;
|
||||||
use libimagstore::store::FileLockEntry;
|
use libimagstore::store::FileLockEntry;
|
||||||
|
@ -37,94 +38,90 @@ pub fn create(rt: &Runtime) {
|
||||||
let diaryname = get_diary_name(rt)
|
let diaryname = get_diary_name(rt)
|
||||||
.unwrap_or_else( || warn_exit("No diary selected. Use either the configuration file or the commandline option", 1));
|
.unwrap_or_else( || warn_exit("No diary selected. Use either the configuration file or the commandline option", 1));
|
||||||
|
|
||||||
let prevent_edit = rt.cli().subcommand_matches("create").unwrap().is_present("no-edit");
|
|
||||||
|
|
||||||
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();
|
|
||||||
let entry = if !create.is_present("timed") {
|
|
||||||
debug!("Creating non-timed entry");
|
|
||||||
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(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).with_minute(0)
|
|
||||||
},
|
|
||||||
|
|
||||||
Some("m") | Some("minutely") => {
|
|
||||||
debug!("Creating minutely-timed entry");
|
|
||||||
let time = DiaryId::now(String::from(diaryname));
|
|
||||||
let hr = create
|
|
||||||
.value_of("hour")
|
|
||||||
.map(|h| { debug!("hour = {:?}", h); h })
|
|
||||||
.and_then(|s| {
|
|
||||||
FromStr::from_str(s)
|
|
||||||
.map_err(|_| warn!("Could not parse hour: '{}'", s))
|
|
||||||
.ok()
|
|
||||||
})
|
|
||||||
.unwrap_or(time.hour());
|
|
||||||
|
|
||||||
let min = create
|
|
||||||
.value_of("minute")
|
|
||||||
.map(|m| { debug!("minute = {:?}", m); m })
|
|
||||||
.and_then(|s| {
|
|
||||||
FromStr::from_str(s)
|
|
||||||
.map_err(|_| warn!("Could not parse minute: '{}'", s))
|
|
||||||
.ok()
|
|
||||||
})
|
|
||||||
.unwrap_or(time.minute());
|
|
||||||
|
|
||||||
time.with_hour(hr).with_minute(min)
|
|
||||||
},
|
|
||||||
|
|
||||||
Some(_) => {
|
|
||||||
warn!("Timed creation failed: Unknown spec '{}'",
|
|
||||||
create.value_of("timed").unwrap());
|
|
||||||
exit(1);
|
|
||||||
},
|
|
||||||
|
|
||||||
None => warn_exit("Unexpected error, cannot continue", 1)
|
|
||||||
};
|
|
||||||
|
|
||||||
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 mut entry = create_entry(rt.store(), &diaryname, rt);
|
let mut entry = create_entry(rt.store(), &diaryname, rt);
|
||||||
|
|
||||||
let res = if prevent_edit {
|
let res = if rt.cli().subcommand_matches("create").unwrap().is_present("no-edit") {
|
||||||
debug!("Not editing new diary entry");
|
debug!("Not editing new diary entry");
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
debug!("Editing new diary entry");
|
debug!("Editing new diary entry");
|
||||||
entry.edit_content(rt).map_err_into(DEK::DiaryEditError)
|
entry.edit_content(rt)
|
||||||
|
.map_err_into(DEK::DiaryEditError)
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Err(e) = res {
|
if let Err(e) = res {
|
||||||
trace_error(&e);
|
trace_error_exit(&e, 1);
|
||||||
} else {
|
} else {
|
||||||
info!("Ok!");
|
info!("Ok!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn create_entry<'a>(diary: &'a Store, diaryname: &str, rt: &Runtime) -> FileLockEntry<'a> {
|
||||||
|
let create = rt.cli().subcommand_matches("create").unwrap();
|
||||||
|
let entry = if !create.is_present("timed") {
|
||||||
|
debug!("Creating non-timed entry");
|
||||||
|
diary.new_entry_today(diaryname)
|
||||||
|
} else {
|
||||||
|
let id = create_id_from_clispec(&create, &diaryname);
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn create_id_from_clispec(create: &ArgMatches, diaryname: &str) -> DiaryId {
|
||||||
|
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)
|
||||||
|
};
|
||||||
|
|
||||||
|
match create.value_of("timed") {
|
||||||
|
Some("h") | Some("hourly") => {
|
||||||
|
debug!("Creating hourly-timed entry");
|
||||||
|
get_hourly_id(create)
|
||||||
|
},
|
||||||
|
|
||||||
|
Some("m") | Some("minutely") => {
|
||||||
|
let time = get_hourly_id(create);
|
||||||
|
let min = create
|
||||||
|
.value_of("minute")
|
||||||
|
.map(|m| { debug!("minute = {:?}", m); m })
|
||||||
|
.and_then(|s| {
|
||||||
|
FromStr::from_str(s)
|
||||||
|
.map_err(|_| warn!("Could not parse minute: '{}'", s))
|
||||||
|
.ok()
|
||||||
|
})
|
||||||
|
.unwrap_or(time.minute());
|
||||||
|
|
||||||
|
time.with_minute(min)
|
||||||
|
},
|
||||||
|
|
||||||
|
Some(_) => {
|
||||||
|
warn!("Timed creation failed: Unknown spec '{}'",
|
||||||
|
create.value_of("timed").unwrap());
|
||||||
|
exit(1);
|
||||||
|
},
|
||||||
|
|
||||||
|
None => warn_exit("Unexpected error, cannot continue", 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue