imag-notes: Use Err/Ok utils to map over results and refactor code
This commit is contained in:
parent
b9a9fd52c4
commit
473e6d5b6a
2 changed files with 32 additions and 40 deletions
|
@ -8,6 +8,7 @@ semver = "0.2.1"
|
||||||
clap = "2.*"
|
clap = "2.*"
|
||||||
log = "0.3"
|
log = "0.3"
|
||||||
version = "2.0.1"
|
version = "2.0.1"
|
||||||
|
itertools = "0.4"
|
||||||
|
|
||||||
[dependencies.libimagrt]
|
[dependencies.libimagrt]
|
||||||
path = "../libimagrt"
|
path = "../libimagrt"
|
||||||
|
@ -24,3 +25,6 @@ path = "../libimagentrytag"
|
||||||
[dependencies.libimagerror]
|
[dependencies.libimagerror]
|
||||||
path = "../libimagerror"
|
path = "../libimagerror"
|
||||||
|
|
||||||
|
[dependencies.libimagutil]
|
||||||
|
path = "../libimagutil"
|
||||||
|
|
||||||
|
|
|
@ -2,20 +2,26 @@ extern crate clap;
|
||||||
#[macro_use] extern crate log;
|
#[macro_use] extern crate log;
|
||||||
extern crate semver;
|
extern crate semver;
|
||||||
#[macro_use] extern crate version;
|
#[macro_use] extern crate version;
|
||||||
|
extern crate itertools;
|
||||||
|
|
||||||
extern crate libimagnotes;
|
extern crate libimagnotes;
|
||||||
extern crate libimagrt;
|
extern crate libimagrt;
|
||||||
extern crate libimagentryedit;
|
extern crate libimagentryedit;
|
||||||
extern crate libimagentrytag;
|
extern crate libimagentrytag;
|
||||||
extern crate libimagerror;
|
extern crate libimagerror;
|
||||||
|
extern crate libimagutil;
|
||||||
|
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
|
||||||
|
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::{trace_error, trace_error_exit};
|
use libimagerror::trace::{MapErrTrace, trace_error};
|
||||||
|
use libimagutil::info_result::*;
|
||||||
|
use libimagutil::warn_result::WarnResult;
|
||||||
|
|
||||||
mod ui;
|
mod ui;
|
||||||
use ui::build_ui;
|
use ui::build_ui;
|
||||||
|
@ -48,9 +54,7 @@ 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())
|
Note::new(rt.store(), name.clone(), String::new()).map_err_trace().ok();
|
||||||
.map_err(|e| trace_error(&e))
|
|
||||||
.ok();
|
|
||||||
|
|
||||||
if rt.cli().subcommand_matches("create").unwrap().is_present("edit") &&
|
if rt.cli().subcommand_matches("create").unwrap().is_present("edit") &&
|
||||||
!edit_entry(rt, name) {
|
!edit_entry(rt, name) {
|
||||||
|
@ -60,8 +64,8 @@ fn create(rt: &Runtime) {
|
||||||
|
|
||||||
fn delete(rt: &Runtime) {
|
fn delete(rt: &Runtime) {
|
||||||
Note::delete(rt.store(), String::from(name_from_cli(rt, "delete")))
|
Note::delete(rt.store(), String::from(name_from_cli(rt, "delete")))
|
||||||
.map_err(|e| trace_error(&e))
|
.map_err_trace()
|
||||||
.map(|_| println!("Ok"))
|
.map_info_str("Ok")
|
||||||
.ok();
|
.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,47 +87,31 @@ fn edit_entry(rt: &Runtime, name: String) -> bool {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Err(e) = note.edit_content(rt) {
|
note.edit_content(rt).map_err_trace().map_warn_err_str("Editing failed").is_ok()
|
||||||
trace_error(&e);
|
|
||||||
warn!("Editing failed");
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn list(rt: &Runtime) {
|
fn list(rt: &Runtime) {
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
let iter = Note::all_notes(rt.store());
|
Note::all_notes(rt.store())
|
||||||
if iter.is_err() {
|
.map_err_trace_exit(1)
|
||||||
trace_error_exit(&iter.unwrap_err(), 1);
|
.map(|iter| {
|
||||||
}
|
let notes = iter.filter_map(|note| note.map_err_trace().ok())
|
||||||
|
.sort_by(|note_a, note_b| {
|
||||||
|
if let (Ok(a), Ok(b)) = (note_a.get_name(), note_b.get_name()) {
|
||||||
|
return a.cmp(&b)
|
||||||
|
} else {
|
||||||
|
return Ordering::Greater;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
let mut iter = iter.unwrap()
|
for note in notes.iter() {
|
||||||
.filter_map(|note| {
|
note.get_name()
|
||||||
match note {
|
.map(|name| println!("{}", name))
|
||||||
Err(e) => {
|
.map_err_trace()
|
||||||
trace_error(&e);
|
.ok();
|
||||||
None
|
|
||||||
},
|
|
||||||
Ok(e) => Some(e)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect::<Vec<Note>>();
|
.ok();
|
||||||
|
|
||||||
iter.sort_by(|note_a, note_b| {
|
|
||||||
if let (Ok(a), Ok(b)) = (note_a.get_name(), note_b.get_name()) {
|
|
||||||
return a.cmp(&b)
|
|
||||||
} else {
|
|
||||||
return Ordering::Greater;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
for note in iter {
|
|
||||||
note.get_name()
|
|
||||||
.map(|name| println!("{}", name))
|
|
||||||
.map_err(|e| trace_error(&e))
|
|
||||||
.ok();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue