Refactor and simplify imag-notes binary

This commit is contained in:
Matthias Beyer 2017-09-14 19:57:48 +02:00
parent 94855fb722
commit 2bbda59051

View file

@ -28,18 +28,18 @@ extern crate libimagentryedit;
extern crate libimagerror; extern crate libimagerror;
extern crate libimagutil; extern crate libimagutil;
use std::process::exit;
use itertools::Itertools; use itertools::Itertools;
use libimagentryedit::edit::Edit; use libimagentryedit::edit::Edit;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup; use libimagrt::setup::generate_runtime_setup;
use libimagnotes::note::Note; use libimagnotes::note::Note;
use libimagerror::trace::{MapErrTrace, trace_error}; use libimagnotes::notestore::*;
use libimagerror::trace::MapErrTrace;
use libimagutil::info_result::*; use libimagutil::info_result::*;
use libimagutil::warn_result::WarnResult; use libimagutil::warn_result::WarnResult;
mod ui; mod ui;
use ui::build_ui; use ui::build_ui;
@ -71,49 +71,54 @@ fn name_from_cli(rt: &Runtime, subcmd: &str) -> String {
fn create(rt: &Runtime) { fn create(rt: &Runtime) {
let name = name_from_cli(rt, "create"); 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") && if rt.cli().subcommand_matches("create").unwrap().is_present("edit") {
!edit_entry(rt, name) { let _ = note
exit(1); .edit_content(rt)
.map_warn_err_str("Editing failed")
.map_err_trace_exit(1);
} }
} }
fn delete(rt: &Runtime) { fn delete(rt: &Runtime) {
Note::delete(rt.store(), String::from(name_from_cli(rt, "delete"))) let _ = rt.store()
.map_err_trace() .delete_note(name_from_cli(rt, "delete"))
.map_info_str("Ok") .map_info_str("Ok")
.ok(); .map_err_trace_exit(1);
} }
fn edit(rt: &Runtime) { fn edit(rt: &Runtime) {
edit_entry(rt, name_from_cli(rt, "edit")); let name = name_from_cli(rt, "edit");
} let _ = rt
.store()
fn edit_entry(rt: &Runtime, name: String) -> bool { .get_note(name.clone())
let mut note = match Note::get(rt.store(), name) { .map_err_trace_exit(1)
Ok(Some(note)) => note, .unwrap()
Ok(None) => { .map(|mut note| {
warn!("Cannot edit nonexistent Note"); let _ = note
return false .edit_content(rt)
}, .map_warn_err_str("Editing failed")
Err(e) => { .map_err_trace_exit(1);
trace_error(&e); })
warn!("Cannot edit nonexistent Note"); .unwrap_or_else(|| {
return false error!("Cannot find note with name '{}'", name);
}, });
};
note.edit_content(rt).map_err_trace().map_warn_err_str("Editing failed").is_ok()
} }
fn list(rt: &Runtime) { fn list(rt: &Runtime) {
use std::cmp::Ordering; use std::cmp::Ordering;
Note::all_notes(rt.store()) rt.store()
.all_notes()
.map_err_trace_exit(1) .map_err_trace_exit(1)
.map(|iter| { .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| { .sorted_by(|note_a, note_b| {
if let (Ok(a), Ok(b)) = (note_a.get_name(), note_b.get_name()) { if let (Ok(a), Ok(b)) = (note_a.get_name(), note_b.get_name()) {
return a.cmp(&b) return a.cmp(&b)