diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2017-09-14 19:57:48 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2017-09-15 13:53:16 +0200 |
commit | 2bbda59051133e66182e3af1952b8787afea09a8 (patch) | |
tree | 1a54fc319a8d6ac5af8c69f88f8fb804082f2520 /bin/domain/imag-notes | |
parent | 94855fb7225f555a61285b5c41f97f9da636fe66 (diff) |
Refactor and simplify imag-notes binary
Diffstat (limited to 'bin/domain/imag-notes')
-rw-r--r-- | bin/domain/imag-notes/src/main.rs | 65 |
1 files changed, 35 insertions, 30 deletions
diff --git a/bin/domain/imag-notes/src/main.rs b/bin/domain/imag-notes/src/main.rs index 83c54fab..86dfe962 100644 --- a/bin/domain/imag-notes/src/main.rs +++ b/bin/domain/imag-notes/src/main.rs @@ -28,18 +28,18 @@ extern crate libimagentryedit; extern crate libimagerror; extern crate libimagutil; -use std::process::exit; - use itertools::Itertools; use libimagentryedit::edit::Edit; use libimagrt::runtime::Runtime; use libimagrt::setup::generate_runtime_setup; use libimagnotes::note::Note; -use libimagerror::trace::{MapErrTrace, trace_error}; +use libimagnotes::notestore::*; +use libimagerror::trace::MapErrTrace; use libimagutil::info_result::*; use libimagutil::warn_result::WarnResult; + mod ui; use ui::build_ui; @@ -71,49 +71,54 @@ fn name_from_cli(rt: &Runtime, subcmd: &str) -> String { fn create(rt: &Runtime) { let name = name_from_cli(rt, "create"); - Note::new(rt.store(), name.clone(), String::new()).map_err_trace().ok(); + let mut note = rt + .store() + .new_note(name.clone(), String::new()) + .map_err_trace_exit(1) + .unwrap(); - if rt.cli().subcommand_matches("create").unwrap().is_present("edit") && - !edit_entry(rt, name) { - exit(1); + if rt.cli().subcommand_matches("create").unwrap().is_present("edit") { + let _ = note + .edit_content(rt) + .map_warn_err_str("Editing failed") + .map_err_trace_exit(1); } } fn delete(rt: &Runtime) { - Note::delete(rt.store(), String::from(name_from_cli(rt, "delete"))) - .map_err_trace() + let _ = rt.store() + .delete_note(name_from_cli(rt, "delete")) .map_info_str("Ok") - .ok(); + .map_err_trace_exit(1); } fn edit(rt: &Runtime) { - edit_entry(rt, name_from_cli(rt, "edit")); -} - -fn edit_entry(rt: &Runtime, name: String) -> bool { - let mut note = match Note::get(rt.store(), name) { - Ok(Some(note)) => note, - Ok(None) => { - warn!("Cannot edit nonexistent Note"); - return false - }, - Err(e) => { - trace_error(&e); - warn!("Cannot edit nonexistent Note"); - return false - }, - }; - - note.edit_content(rt).map_err_trace().map_warn_err_str("Editing failed").is_ok() + let name = name_from_cli(rt, "edit"); + let _ = rt + .store() + .get_note(name.clone()) + .map_err_trace_exit(1) + .unwrap() + .map(|mut note| { + let _ = note + .edit_content(rt) + .map_warn_err_str("Editing failed") + .map_err_trace_exit(1); + }) + .unwrap_or_else(|| { + error!("Cannot find note with name '{}'", name); + }); } fn list(rt: &Runtime) { use std::cmp::Ordering; - Note::all_notes(rt.store()) + rt.store() + .all_notes() .map_err_trace_exit(1) .map(|iter| { - let notes = iter.filter_map(|note| note.map_err_trace().ok()) + let notes = iter + .filter_map(|noteid| rt.store().get(noteid).map_err_trace_exit(1).unwrap()) .sorted_by(|note_a, note_b| { if let (Ok(a), Ok(b)) = (note_a.get_name(), note_b.get_name()) { return a.cmp(&b) |