Merge pull request #717 from matthiasbeyer/use-result-trace-helper

Use result trace helper
This commit is contained in:
Matthias Beyer 2016-09-08 16:50:08 +02:00 committed by GitHub
commit 00acf9bb85
10 changed files with 87 additions and 153 deletions

View file

@ -1,5 +1,5 @@
use libimagrt::runtime::Runtime;
use libimagerror::trace::trace_error;
use libimagerror::trace::{MapErrTrace, trace_error};
use libimagcounter::counter::Counter;
pub fn list(rt: &Runtime) {
@ -25,11 +25,11 @@ pub fn list(rt: &Runtime) {
println!("{} - {} {}", name.unwrap(), value.unwrap(), unit.unwrap());
}
})
.map_err(|e| trace_error(&e))
.map_err_trace()
.ok();
}
})
.map_err(|e| trace_error(&e))
.map_err_trace()
});
}

View file

@ -27,8 +27,9 @@ use std::str::FromStr;
use libimagrt::setup::generate_runtime_setup;
use libimagcounter::counter::Counter;
use libimagerror::trace::{trace_error, trace_error_exit};
use libimagerror::trace::MapErrTrace;
use libimagutil::key_value_split::IntoKeyValue;
use libimagutil::info_result::*;
mod create;
mod delete;
@ -73,30 +74,15 @@ fn main() {
match action {
Action::Inc => {
Counter::load(String::from(name), rt.store())
.map(|mut counter| {
match counter.inc() {
Err(e) => trace_error_exit(&e, 1),
Ok(_) => info!("Ok"),
}
})
.map(|mut c| c.inc().map_err_trace_exit(1).map_info_str("Ok"))
},
Action::Dec => {
Counter::load(String::from(name), rt.store())
.map(|mut counter| {
match counter.dec() {
Err(e) => trace_error_exit(&e, 1),
Ok(_) => info!("Ok"),
}
})
.map(|mut c| c.dec().map_err_trace_exit(1).map_info_str("Ok"))
},
Action::Reset => {
Counter::load(String::from(name), rt.store())
.map(|mut counter| {
match counter.reset() {
Err(e) => trace_error_exit(&e, 1),
Ok(_) => info!("Ok"),
}
})
.map(|mut c| c.reset().map_err_trace_exit(1).map_info_str("Ok"))
},
Action::Set => {
let kv = String::from(name).into_kv();
@ -112,15 +98,10 @@ fn main() {
}
let value : i64 = value.unwrap();
Counter::load(String::from(key), rt.store())
.map(|mut counter| {
match counter.set(value) {
Err(e) => trace_error_exit(&e, 1),
Ok(_) => info!("Ok"),
}
})
.map(|mut c| c.set(value).map_err_trace_exit(1).map_info_str("Ok"))
},
}
.map_err(|e| trace_error(&e))
.map_err_trace()
.ok();
},
|name| {

View file

@ -6,7 +6,7 @@ use libimagdiary::error::DiaryErrorKind as DEK;
use libimagdiary::error::MapErrInto;
use libimagentryedit::edit::Edit;
use libimagrt::runtime::Runtime;
use libimagerror::trace::trace_error;
use libimagerror::trace::MapErrTrace;
use libimagerror::into::IntoError;
use libimagtimeui::datetime::DateTime;
use libimagtimeui::parse::Parse;
@ -37,7 +37,7 @@ pub fn edit(rt: &Runtime) {
Some(Err(e)) => Err(e),
None => Err(DEK::EntryNotInDiary.into_error()),
}
.map_err(|e| trace_error(&e)).ok();
.map_err_trace().ok();
}

View file

@ -5,8 +5,9 @@ use libimagentrylist::listers::core::CoreLister;
use libimagentrylist::lister::Lister;
use libimagrt::runtime::Runtime;
use libimagstore::store::Entry;
use libimagerror::trace::trace_error;
use libimagutil::warn_exit::warn_exit;
use libimagerror::trace::MapErrTrace;
use libimagutil::debug_result::*;
use util::get_diary_name;
@ -18,7 +19,7 @@ pub fn list(rt: &Runtime) {
e.get_location().clone()
.without_base()
.to_str()
.map_err(|e| trace_error(&e))
.map_err_trace()
.unwrap_or(String::from("<<Path Parsing Error>>"))
}
@ -28,17 +29,16 @@ pub fn list(rt: &Runtime) {
.and_then(|es| {
debug!("Iterator for listing: {:?}", es);
let es = es.filter_map(|a| {
debug!("Filtering: {:?}", a);
a.ok()
}).map(|e| e.into());
let es = es
.filter_map(|a| a.map_dbg(|e| format!("Filtering: {:?}", e)).ok())
.map(|e| e.into());
CoreLister::new(&entry_to_location_listing_string)
.list(es) // TODO: Do not ignore non-ok()s
.map_err_into(DEK::IOError)
})
.map(|_| debug!("Ok"))
.map_err(|e| trace_error(&e))
.map_dbg_str("Ok")
.map_err_trace()
.ok();
}

View file

@ -34,10 +34,11 @@ use libimagstore::error::StoreError;
use libimagstore::store::Entry;
use libimagstore::store::FileLockEntry;
use libimagstore::store::Store;
use libimagerror::trace::{trace_error, trace_error_exit};
use libimagerror::trace::{MapErrTrace, trace_error, trace_error_exit};
use libimagentrylink::external::ExternalLinker;
use libimagutil::warn_result::*;
use libimagutil::warn_exit::warn_exit;
use libimagutil::info_result::*;
use clap::ArgMatches;
use url::Url;
@ -89,7 +90,7 @@ fn handle_internal_linking(rt: &Runtime) {
println!("{: <3}: {}", i, link);
}
})
.map_err(|e| trace_error(&e))
.map_err_trace()
.ok();
},
@ -236,38 +237,17 @@ fn handle_external_linking(rt: &Runtime) {
}
fn add_link_to_entry(store: &Store, matches: &ArgMatches, entry: &mut FileLockEntry) {
let link = matches.value_of("add").unwrap();
let link = Url::parse(link);
if link.is_err() {
debug!("URL parsing error...");
trace_error_exit(&link.unwrap_err(), 1);
}
let link = link.unwrap();
if let Err(e) = entry.add_external_link(store, link) {
debug!("Error while adding external link...");
trace_error(&e);
} else {
debug!("Everything worked well");
info!("Ok");
}
Url::parse(matches.value_of("add").unwrap())
.map_err_trace_exit(1)
.map(|link| entry.add_external_link(store, link).map_err_trace().map_info_str("Ok"))
.ok();
}
fn remove_link_from_entry(store: &Store, matches: &ArgMatches, entry: &mut FileLockEntry) {
let link = matches.value_of("remove").unwrap();
let link = Url::parse(link);
if link.is_err() {
trace_error_exit(&link.unwrap_err(), 1);
}
let link = link.unwrap();
if let Err(e) = entry.remove_external_link(store, link) {
trace_error(&e);
} else {
info!("Ok");
}
Url::parse(matches.value_of("remove").unwrap())
.map_err_trace_exit(1)
.map(|link| entry.remove_external_link(store, link).map_err_trace().map_info_str("Ok"))
.ok();
}
fn set_links_for_entry(store: &Store, matches: &ArgMatches, entry: &mut FileLockEntry) {
@ -289,29 +269,22 @@ fn set_links_for_entry(store: &Store, matches: &ArgMatches, entry: &mut FileLock
.filter_map(|x| x)
.collect();
if let Err(e) = entry.set_external_links(store, links) {
trace_error(&e);
} else {
info!("Ok");
}
entry.set_external_links(store, links)
.map_err_trace()
.map_info_str("Ok")
.ok();
}
fn list_links_for_entry(store: &Store, entry: &mut FileLockEntry) {
let res = entry.get_external_links(store)
entry.get_external_links(store)
.and_then(|links| {
for (i, link) in links.iter().enumerate() {
println!("{: <3}: {}", i, link);
}
Ok(())
});
match res {
Err(e) => {
trace_error(&e);
},
Ok(_) => {
info!("Ok");
},
}
})
.map_err_trace()
.map_info_str("Ok")
.ok();
}

View file

@ -8,6 +8,7 @@ semver = "0.2.1"
clap = "2.*"
log = "0.3"
version = "2.0.1"
itertools = "0.4"
[dependencies.libimagrt]
path = "../libimagrt"
@ -24,3 +25,6 @@ path = "../libimagentrytag"
[dependencies.libimagerror]
path = "../libimagerror"
[dependencies.libimagutil]
path = "../libimagutil"

View file

@ -2,20 +2,26 @@ extern crate clap;
#[macro_use] extern crate log;
extern crate semver;
#[macro_use] extern crate version;
extern crate itertools;
extern crate libimagnotes;
extern crate libimagrt;
extern crate libimagentryedit;
extern crate libimagentrytag;
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::{trace_error, trace_error_exit};
use libimagerror::trace::{MapErrTrace, trace_error};
use libimagutil::info_result::*;
use libimagutil::warn_result::WarnResult;
mod ui;
use ui::build_ui;
@ -48,9 +54,7 @@ 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(|e| trace_error(&e))
.ok();
Note::new(rt.store(), name.clone(), String::new()).map_err_trace().ok();
if rt.cli().subcommand_matches("create").unwrap().is_present("edit") &&
!edit_entry(rt, name) {
@ -60,8 +64,8 @@ fn create(rt: &Runtime) {
fn delete(rt: &Runtime) {
Note::delete(rt.store(), String::from(name_from_cli(rt, "delete")))
.map_err(|e| trace_error(&e))
.map(|_| println!("Ok"))
.map_err_trace()
.map_info_str("Ok")
.ok();
}
@ -83,35 +87,17 @@ fn edit_entry(rt: &Runtime, name: String) -> bool {
},
};
if let Err(e) = note.edit_content(rt) {
trace_error(&e);
warn!("Editing failed");
return false
}
true
note.edit_content(rt).map_err_trace().map_warn_err_str("Editing failed").is_ok()
}
fn list(rt: &Runtime) {
use std::cmp::Ordering;
let iter = Note::all_notes(rt.store());
if iter.is_err() {
trace_error_exit(&iter.unwrap_err(), 1);
}
let mut iter = iter.unwrap()
.filter_map(|note| {
match note {
Err(e) => {
trace_error(&e);
None
},
Ok(e) => Some(e)
}
})
.collect::<Vec<Note>>();
iter.sort_by(|note_a, note_b| {
Note::all_notes(rt.store())
.map_err_trace_exit(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 {
@ -119,11 +105,13 @@ fn list(rt: &Runtime) {
}
});
for note in iter {
for note in notes.iter() {
note.get_name()
.map(|name| println!("{}", name))
.map_err(|e| trace_error(&e))
.map_err_trace()
.ok();
}
})
.ok();
}

View file

@ -1,13 +1,12 @@
use std::path::PathBuf;
use libimagrt::runtime::Runtime;
use libimagerror::trace::trace_error_exit;
use libimagerror::trace::MapErrTrace;
use libimagstore::storeid::StoreId;
use libimagutil::warn_exit::warn_exit;
use libimagutil::warn_result::*;
pub fn delete(rt: &Runtime) {
use std::process::exit;
rt.cli()
.subcommand_matches("delete")
.map(|sub| {
@ -15,15 +14,13 @@ pub fn delete(rt: &Runtime) {
.map(|id| {
let path = PathBuf::from(id);
let path = try!(StoreId::new(Some(rt.store().path().clone()), path)
.map_err(|e| trace_error_exit(&e, 1)));
.map_err_trace_exit(1));
debug!("Deleting file at {:?}", id);
rt.store()
.delete(path)
.map_err(|e| {
warn!("Error: {:?}", e);
exit(1);
})
.map_warn_err(|e| format!("Error: {:?}", e))
.map_err_trace_exit(1)
})
.or_else(|| warn_exit("No ID passed. Will exit now", 1))
})

View file

@ -6,7 +6,8 @@ use toml::Value;
use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreId;
use libimagrt::runtime::Runtime;
use libimagerror::trace::{trace_error, trace_error_exit};
use libimagerror::trace::MapErrTrace;
use libimagutil::debug_result::*;
pub fn retrieve(rt: &Runtime) {
rt.cli()
@ -16,18 +17,16 @@ pub fn retrieve(rt: &Runtime) {
.map(|id| {
let path = PathBuf::from(id);
let path = try!(StoreId::new(Some(rt.store().path().clone()), path)
.map_err(|e| trace_error_exit(&e, 1)));
.map_err_trace_exit(1));
debug!("path = {:?}", path);
rt.store()
// "id" must be present, enforced via clap spec
.retrieve(path)
.map(|e| print_entry(rt, scmd, e))
.map_err(|e| {
debug!("No entry.");
debug!("{}:", e);
trace_error(&e);
})
.map_dbg_str("No entry")
.map_dbg(|e| format!("{:?}", e))
.map_err_trace()
})
});
}

View file

@ -13,7 +13,6 @@ extern crate libimagstore;
extern crate libimagerror;
extern crate libimagtodo;
use std::process::exit;
use std::process::{Command, Stdio};
use std::io::stdin;
@ -22,7 +21,7 @@ use toml::Value;
use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup;
use libimagtodo::task::Task;
use libimagerror::trace::trace_error;
use libimagerror::trace::{MapErrTrace, trace_error, trace_error_exit};
mod ui;
@ -51,18 +50,13 @@ fn tw_hook(rt: &Runtime) {
match Task::import(rt.store(), stdin) {
Ok((_, line, uuid)) => println!("{}\nTask {} stored in imag", line, uuid),
Err(e) => {
trace_error(&e);
exit(1);
}
Err(e) => trace_error_exit(&e, 1),
}
} else if subcmd.is_present("delete") {
// The used hook is "on-modify". This hook gives two json-objects
// per usage und wants one (the second one) back.
let stdin = stdin();
Task::delete_by_imports(rt.store(), stdin.lock())
.map_err(|e| trace_error(&e))
.ok();
Task::delete_by_imports(rt.store(), stdin.lock()).map_err_trace().ok();
} else {
// Should not be possible, as one argument is required via
// ArgGroup
@ -120,8 +114,6 @@ fn list(rt: &Runtime) {
println!("{}", outstring);
});
if let Err(e) = res {
trace_error(&e);
}
res.map_err_trace().ok();
}