imag-contact: Move from error-chain to failure
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
parent
4486abed84
commit
3ac30c0944
3 changed files with 25 additions and 29 deletions
|
@ -24,12 +24,13 @@ maintenance = { status = "actively-developed" }
|
|||
[dependencies]
|
||||
log = "0.4.0"
|
||||
toml = "0.4"
|
||||
toml-query = "0.7"
|
||||
vobject = { git = "https://github.com/matthiasbeyer/rust-vobject", branch = "update-errorchain" }
|
||||
toml-query = { git = "https://github.com/matthiasbeyer/toml-query", branch = "failure" }
|
||||
vobject = { git = "https://github.com/matthiasbeyer/rust-vobject", branch = "master" }
|
||||
handlebars = "1.0"
|
||||
walkdir = "2"
|
||||
uuid = { version = "0.7", features = ["v4"] }
|
||||
serde_json = "1"
|
||||
failure = "0.1"
|
||||
|
||||
libimagrt = { version = "0.9.0", path = "../../../lib/core/libimagrt" }
|
||||
libimagstore = { version = "0.9.0", path = "../../../lib/core/libimagstore" }
|
||||
|
|
|
@ -44,11 +44,10 @@ use vobject::write_component;
|
|||
use toml_query::read::TomlValueReadExt;
|
||||
use toml::Value;
|
||||
use uuid::Uuid;
|
||||
use failure::Error;
|
||||
|
||||
use libimagcontact::store::ContactStore;
|
||||
use libimagcontact::error::ContactError as CE;
|
||||
use libimagrt::runtime::Runtime;
|
||||
use libimagerror::str::ErrFromStr;
|
||||
use libimagerror::trace::MapErrTrace;
|
||||
use libimagerror::trace::trace_error;
|
||||
use libimagutil::warn_result::WarnResult;
|
||||
|
@ -126,8 +125,7 @@ pub fn create(rt: &Runtime) {
|
|||
.create_new(true)
|
||||
.open(fl.clone())
|
||||
.map_warn_err_str("Cannot create/open destination File. Stopping.")
|
||||
.err_from_str()
|
||||
.map_err(CE::from)
|
||||
.map_err(Error::from)
|
||||
.map_err_trace_exit_unwrap(1);
|
||||
|
||||
let uuid_string = uuid
|
||||
|
@ -161,8 +159,7 @@ pub fn create(rt: &Runtime) {
|
|||
|
||||
match ::toml::de::from_str(&template)
|
||||
.map(|toml| parse_toml_into_vcard(toml, uuid.clone()))
|
||||
.err_from_str()
|
||||
.map_err(CE::from)
|
||||
.map_err(Error::from)
|
||||
{
|
||||
Err(e) => {
|
||||
error!("Error parsing template");
|
||||
|
@ -183,7 +180,7 @@ pub fn create(rt: &Runtime) {
|
|||
let vcard_string = write_component(&vcard);
|
||||
let _ = dest
|
||||
.write_all(&vcard_string.as_bytes())
|
||||
.map_err(CE::from)
|
||||
.map_err(Error::from)
|
||||
.map_err_trace_exit_unwrap(1);
|
||||
|
||||
break;
|
||||
|
@ -244,7 +241,7 @@ fn parse_toml_into_vcard(toml: Value, uuid: String) -> Option<Vcard> {
|
|||
|
||||
{ // parse nicknames
|
||||
debug!("Parsing nicknames");
|
||||
match toml.read("nickname").map_err_trace_exit_unwrap(1) {
|
||||
match toml.read("nickname").map_err(Error::from).map_err_trace_exit_unwrap(1) {
|
||||
Some(&Value::Array(ref ary)) => {
|
||||
for (i, element) in ary.iter().enumerate() {
|
||||
let nicktype = match read_str_from_toml(element, "type", false) {
|
||||
|
@ -306,7 +303,7 @@ fn parse_toml_into_vcard(toml: Value, uuid: String) -> Option<Vcard> {
|
|||
|
||||
{ // parse phone
|
||||
debug!("Parse phone");
|
||||
match toml.read("person.phone").map_err_trace_exit_unwrap(1) {
|
||||
match toml.read("person.phone").map_err(Error::from).map_err_trace_exit_unwrap(1) {
|
||||
Some(&Value::Array(ref ary)) => {
|
||||
for (i, element) in ary.iter().enumerate() {
|
||||
let phonetype = match read_str_from_toml(element, "type", false) {
|
||||
|
@ -344,7 +341,7 @@ fn parse_toml_into_vcard(toml: Value, uuid: String) -> Option<Vcard> {
|
|||
|
||||
{ // parse address
|
||||
debug!("Parsing address");
|
||||
match toml.read("addresses").map_err_trace_exit_unwrap(1) {
|
||||
match toml.read("addresses").map_err(Error::from).map_err_trace_exit_unwrap(1) {
|
||||
Some(&Value::Array(ref ary)) => {
|
||||
for (i, element) in ary.iter().enumerate() {
|
||||
let adrtype = match read_str_from_toml(element, "type", false) {
|
||||
|
@ -391,7 +388,7 @@ fn parse_toml_into_vcard(toml: Value, uuid: String) -> Option<Vcard> {
|
|||
|
||||
{ // parse email
|
||||
debug!("Parsing email");
|
||||
match toml.read("person.email").map_err_trace_exit_unwrap(1) {
|
||||
match toml.read("person.email").map_err(Error::from).map_err_trace_exit_unwrap(1) {
|
||||
Some(&Value::Array(ref ary)) => {
|
||||
for (i, element) in ary.iter().enumerate() {
|
||||
let mailtype = match read_str_from_toml(element, "type", false) {
|
||||
|
@ -456,7 +453,7 @@ fn parse_toml_into_vcard(toml: Value, uuid: String) -> Option<Vcard> {
|
|||
}
|
||||
|
||||
fn read_strary_from_toml(toml: &Value, path: &'static str) -> Option<Vec<String>> {
|
||||
match toml.read(path).map_warn_err_str(&format!("Failed to read value at '{}'", path)) {
|
||||
match toml.read(path).map_err(Error::from).map_warn_err_str(&format!("Failed to read value at '{}'", path)) {
|
||||
Ok(Some(&Value::Array(ref vec))) => {
|
||||
let mut v = Vec::new();
|
||||
for elem in vec {
|
||||
|
@ -486,6 +483,7 @@ fn read_strary_from_toml(toml: &Value, path: &'static str) -> Option<Vec<String>
|
|||
|
||||
fn read_str_from_toml(toml: &Value, path: &'static str, must_be_there: bool) -> Option<String> {
|
||||
let v = toml.read(path)
|
||||
.map_err(Error::from)
|
||||
.map_warn_err_str(&format!("Failed to read value at '{}'", path));
|
||||
|
||||
match v {
|
||||
|
|
|
@ -41,6 +41,7 @@ extern crate handlebars;
|
|||
extern crate walkdir;
|
||||
extern crate uuid;
|
||||
extern crate serde_json;
|
||||
extern crate failure;
|
||||
|
||||
extern crate libimagcontact;
|
||||
extern crate libimagstore;
|
||||
|
@ -58,16 +59,16 @@ use handlebars::Handlebars;
|
|||
use clap::ArgMatches;
|
||||
use toml_query::read::TomlValueReadTypeExt;
|
||||
use walkdir::WalkDir;
|
||||
use failure::Error;
|
||||
use failure::err_msg;
|
||||
|
||||
use libimagrt::runtime::Runtime;
|
||||
use libimagrt::setup::generate_runtime_setup;
|
||||
use libimagerror::str::ErrFromStr;
|
||||
use libimagerror::trace::MapErrTrace;
|
||||
use libimagerror::io::ToExitCode;
|
||||
use libimagerror::exit::ExitUnwrap;
|
||||
use libimagerror::iter::TraceIterator;
|
||||
use libimagcontact::store::ContactStore;
|
||||
use libimagcontact::error::ContactError as CE;
|
||||
use libimagcontact::contact::Contact;
|
||||
use libimagcontact::deser::DeserVcard;
|
||||
use libimagstore::iter::get::StoreIdGetIteratorExtension;
|
||||
|
@ -121,7 +122,7 @@ fn list(rt: &Runtime) {
|
|||
.map(|fle| {
|
||||
let fle = fle
|
||||
.map_err_trace_exit_unwrap(1)
|
||||
.ok_or_else(|| CE::from("StoreId not found".to_owned()))
|
||||
.ok_or_else(|| Error::from(err_msg("StoreId not found".to_owned())))
|
||||
.map_err_trace_exit_unwrap(1);
|
||||
|
||||
fle.deser().map_err_trace_exit_unwrap(1)
|
||||
|
@ -144,8 +145,7 @@ fn list(rt: &Runtime) {
|
|||
let data = build_data_object_for_handlebars(i, &deservcard);
|
||||
|
||||
list_format.render("format", &data)
|
||||
.err_from_str()
|
||||
.map_err(CE::from)
|
||||
.map_err(Error::from)
|
||||
.map_err_trace_exit_unwrap(1)
|
||||
})
|
||||
|
||||
|
@ -175,8 +175,7 @@ fn import(rt: &Runtime) {
|
|||
} else if path.is_dir() {
|
||||
for entry in WalkDir::new(path).min_depth(1).into_iter() {
|
||||
let entry = entry
|
||||
.err_from_str()
|
||||
.map_err(CE::from)
|
||||
.map_err(Error::from)
|
||||
.map_err_trace_exit_unwrap(1);
|
||||
if entry.file_type().is_file() {
|
||||
let pb = PathBuf::from(entry.path());
|
||||
|
@ -233,8 +232,7 @@ fn show(rt: &Runtime) {
|
|||
|
||||
let s = show_format
|
||||
.render("format", &data)
|
||||
.err_from_str()
|
||||
.map_err(CE::from)
|
||||
.map_err(Error::from)
|
||||
.map_err_trace_exit_unwrap(1);
|
||||
let _ = writeln!(outlock, "{}", s).to_exit_code().unwrap_or_exit();
|
||||
});
|
||||
|
@ -324,8 +322,7 @@ fn find(rt: &Runtime) {
|
|||
let data = build_data_object_for_handlebars(i, &card);
|
||||
let s = fmt
|
||||
.render("format", &data)
|
||||
.err_from_str()
|
||||
.map_err(CE::from)
|
||||
.map_err(Error::from)
|
||||
.map_err_trace_exit_unwrap(1);
|
||||
|
||||
let _ = writeln!(rt.stdout(), "{}", s)
|
||||
|
@ -341,19 +338,19 @@ fn get_contact_print_format(config_value_path: &'static str, rt: &Runtime, scmd:
|
|||
.map(String::from)
|
||||
.unwrap_or_else(|| {
|
||||
rt.config()
|
||||
.ok_or_else(|| CE::from("No configuration file".to_owned()))
|
||||
.ok_or_else(|| Error::from(err_msg("No configuration file")))
|
||||
.map_err_trace_exit_unwrap(1)
|
||||
.read_string(config_value_path)
|
||||
.map_err(Error::from)
|
||||
.map_err_trace_exit_unwrap(1)
|
||||
.ok_or_else(|| CE::from("Configuration 'contact.list_format' does not exist".to_owned()))
|
||||
.ok_or_else(|| Error::from(err_msg("Configuration 'contact.list_format' does not exist")))
|
||||
.map_err_trace_exit_unwrap(1)
|
||||
});
|
||||
|
||||
let mut hb = Handlebars::new();
|
||||
let _ = hb
|
||||
.register_template_string("format", fmt)
|
||||
.err_from_str()
|
||||
.map_err(CE::from)
|
||||
.map_err(Error::from)
|
||||
.map_err_trace_exit_unwrap(1);
|
||||
|
||||
hb.register_escape_fn(::handlebars::no_escape);
|
||||
|
|
Loading…
Reference in a new issue