diff options
Diffstat (limited to 'bin/domain')
49 files changed, 318 insertions, 321 deletions
diff --git a/bin/domain/imag-bookmark/Cargo.toml b/bin/domain/imag-bookmark/Cargo.toml index 4462144b..ededc996 100644 --- a/bin/domain/imag-bookmark/Cargo.toml +++ b/bin/domain/imag-bookmark/Cargo.toml @@ -23,7 +23,7 @@ maintenance = { status = "actively-developed" } log = "0.4.6" toml = "0.5.1" toml-query = "0.9.2" -failure = "0.1.5" +anyhow = "1" resiter = "0.4.0" url = "2" handlebars = "2" diff --git a/bin/domain/imag-bookmark/src/lib.rs b/bin/domain/imag-bookmark/src/lib.rs index 51259481..ad36c80f 100644 --- a/bin/domain/imag-bookmark/src/lib.rs +++ b/bin/domain/imag-bookmark/src/lib.rs @@ -40,7 +40,7 @@ extern crate toml; extern crate url; extern crate uuid; extern crate toml_query; -#[macro_use] extern crate failure; +#[macro_use] extern crate anyhow; extern crate resiter; extern crate handlebars; extern crate rayon; @@ -56,9 +56,9 @@ use std::io::Write; use std::collections::BTreeMap; use std::process::Command; -use failure::Error; -use failure::err_msg; -use failure::Fallible as Result; +use anyhow::Error; + +use anyhow::Result; use resiter::AndThen; use resiter::IterInnerOkOrElse; use clap::App; @@ -86,7 +86,7 @@ mod ui; pub enum ImagBookmark {} impl ImagApplication for ImagBookmark { fn run(rt: Runtime) -> Result<()> { - match rt.cli().subcommand_name().ok_or_else(|| err_msg("No subcommand called"))? { + match rt.cli().subcommand_name().ok_or_else(|| anyhow!("No subcommand called"))? { "add" => add(&rt), "open" => open(&rt), "list" => list(&rt), @@ -97,7 +97,7 @@ impl ImagApplication for ImagBookmark { if rt.handle_unknown_subcommand("imag-bookmark", other, rt.cli())?.success() { Ok(()) } else { - Err(err_msg("Failed to handle unknown subcommand")) + Err(anyhow!("Failed to handle unknown subcommand")) } }, } @@ -139,12 +139,12 @@ fn open(rt: &Runtime) -> Result<()> { let open_command = rt.config() .map(|value| { value.read("bookmark.open")? - .ok_or_else(|| err_msg("Configuration missing: 'bookmark.open'"))? + .ok_or_else(|| anyhow!("Configuration missing: 'bookmark.open'"))? .as_str() - .ok_or_else(|| err_msg("Open command should be a string")) + .ok_or_else(|| anyhow!("Open command should be a string")) }) .or_else(|| Ok(scmd.value_of("opencmd")).transpose()) - .unwrap_or_else(|| Err(err_msg("No open command available in config or on commandline")))?; + .unwrap_or_else(|| Err(anyhow!("No open command available in config or on commandline")))?; let hb = { let mut hb = Handlebars::new(); @@ -153,11 +153,11 @@ fn open(rt: &Runtime) -> Result<()> { }; let iter = rt.ids::<crate::ui::PathProvider>()? - .ok_or_else(|| err_msg("No ids supplied"))? + .ok_or_else(|| anyhow!("No ids supplied"))? .into_iter() .map(Ok) .into_get_iter(rt.store()) - .map_inner_ok_or_else(|| err_msg("Did not find one entry")); + .map_inner_ok_or_else(|| anyhow!("Did not find one entry")); if scmd.is_present("openparallel") { let links = iter @@ -195,11 +195,11 @@ fn list(rt: &Runtime) -> Result<()> { rt.store() .all_bookmarks()? .into_get_iter() - .map_inner_ok_or_else(|| err_msg("Did not find one entry")) + .map_inner_ok_or_else(|| anyhow!("Did not find one entry")) .and_then_ok(|entry| { if entry.is_bookmark()? { let url = entry.get_url()? - .ok_or_else(|| format_err!("Failed to retrieve URL for {}", entry.get_location()))?; + .ok_or_else(|| anyhow!("Failed to retrieve URL for {}", entry.get_location()))?; if !rt.output_is_pipe() { writeln!(rt.stdout(), "{}", url)?; } @@ -214,11 +214,11 @@ fn list(rt: &Runtime) -> Result<()> { fn remove(rt: &Runtime) -> Result<()> { rt.ids::<crate::ui::PathProvider>()? - .ok_or_else(|| err_msg("No ids supplied"))? + .ok_or_else(|| anyhow!("No ids supplied"))? .into_iter() .map(Ok) .into_get_iter(rt.store()) - .map_inner_ok_or_else(|| err_msg("Did not find one entry")) + .map_inner_ok_or_else(|| anyhow!("Did not find one entry")) .and_then_ok(|fle| { rt.report_touched(fle.get_location()) .map_err(Error::from) @@ -239,12 +239,12 @@ fn find(rt: &Runtime) -> Result<()> { .all_bookmarks()? .into_get_iter() } - .map_inner_ok_or_else(|| err_msg("Did not find one entry")) + .map_inner_ok_or_else(|| anyhow!("Did not find one entry")) .and_then_ok(|fle| { if fle.is_bookmark()? { let url = fle .get_url()? - .ok_or_else(|| format_err!("Failed to retrieve URL for {}", fle.get_location()))?; + .ok_or_else(|| anyhow!("Failed to retrieve URL for {}", fle.get_location()))?; if url.as_str().contains(substr) { if !rt.output_is_pipe() { writeln!(rt.stdout(), "{}", url)?; @@ -262,7 +262,7 @@ fn find(rt: &Runtime) -> Result<()> { fn calculate_command_data<'a>(hb: &Handlebars, entry: &FileLockEntry<'a>, open_command: &str) -> Result<Vec<String>> { let url = entry.get_url()? - .ok_or_else(|| format_err!("Failed to retrieve URL for {}", entry.get_location()))? + .ok_or_else(|| anyhow!("Failed to retrieve URL for {}", entry.get_location()))? .into_string(); let data = { @@ -277,7 +277,7 @@ fn calculate_command_data<'a>(hb: &Handlebars, entry: &FileLockEntry<'a>, open_c .collect::<Vec<String>>(); if command_rendered.len() > 2 { - return Err(format_err!("Command seems not to include URL: '{}'", open_command)); + return Err(anyhow!("Command seems not to include URL: '{}'", open_command)); } Ok(command_rendered.into_iter().map(String::from).collect()) diff --git a/bin/domain/imag-bookmark/src/ui.rs b/bin/domain/imag-bookmark/src/ui.rs index c7c1a0e4..e4645486 100644 --- a/bin/domain/imag-bookmark/src/ui.rs +++ b/bin/domain/imag-bookmark/src/ui.rs @@ -19,7 +19,7 @@ use std::path::PathBuf; -use failure::Fallible as Result; +use anyhow::Result; use clap::{Arg, ArgMatches, App, SubCommand}; use libimagutil::cli_validators::*; @@ -118,7 +118,7 @@ pub struct PathProvider; impl IdPathProvider for PathProvider { fn get_ids(matches: &ArgMatches) -> Result<Option<Vec<StoreId>>> { fn no_ids_error() -> Result<Option<Vec<StoreId>>> { - Err(format_err!("Command does not get IDs as input")) + Err(anyhow!("Command does not get IDs as input")) } fn get_id_paths(field: &str, subm: &ArgMatches) -> Result<Option<Vec<StoreId>>> { @@ -137,7 +137,7 @@ impl IdPathProvider for PathProvider { ("remove", Some(subm)) => get_id_paths("ids", subm), ("list", Some(subm)) => get_id_paths("ids", subm), ("find", Some(subm)) => get_id_paths("ids", subm), - (other, _) => Err(format_err!("Not a known command: {}", other)), + (other, _) => Err(anyhow!("Not a known command: {}", other)), } } } diff --git a/bin/domain/imag-calendar/Cargo.toml b/bin/domain/imag-calendar/Cargo.toml index be384832..9d7cd5a5 100644 --- a/bin/domain/imag-calendar/Cargo.toml +++ b/bin/domain/imag-calendar/Cargo.toml @@ -22,6 +22,7 @@ maintenance = { status = "actively-developed" } [dependencies] log = "0.4" +anyhow = "1" failure = "0.1" walkdir = "2.2.8" vobject = "0.7" diff --git a/bin/domain/imag-calendar/src/filters.rs b/bin/domain/imag-calendar/src/filters.rs index 7e452e94..d57d2129 100644 --- a/bin/domain/imag-calendar/src/filters.rs +++ b/bin/domain/imag-calendar/src/filters.rs @@ -18,7 +18,7 @@ // use chrono::NaiveDateTime; -use failure::Fallible as Result; +use anyhow::Result; use vobject::icalendar::Event; use libimagerror::trace::MapErrTrace; @@ -35,7 +35,7 @@ pub fn event_is_before<'a>(event: &Event<'a>, before_spec: &NaiveDateTime) -> bo Ok(result) }) .unwrap_or_else(|| Err({ - format_err!("Entry with UID {} has no end time, cannot determine whether to list it", + anyhow!("Entry with UID {} has no end time, cannot determine whether to list it", uid()) })); @@ -47,7 +47,7 @@ pub fn event_is_before<'a>(event: &Event<'a>, before_spec: &NaiveDateTime) -> bo Ok(result) }) .unwrap_or_else(|| Err({ - format_err!("Entry with UID {} has no timestamp, cannot determine whether to list it", + anyhow!("Entry with UID {} has no timestamp, cannot determine whether to list it", uid()) })); @@ -72,6 +72,6 @@ fn try_to_parse_datetime(s: &str) -> Result<NaiveDateTime> { ]; ::libimagutil::date::try_to_parse_datetime_from_string(s, FORMATS.iter()) - .ok_or_else(|| format_err!("Cannot parse datetime: {}", s)) + .ok_or_else(|| anyhow!("Cannot parse datetime: {}", s)) } diff --git a/bin/domain/imag-calendar/src/lib.rs b/bin/domain/imag-calendar/src/lib.rs index 5c549195..5e0bf058 100644 --- a/bin/domain/imag-calendar/src/lib.rs +++ b/bin/domain/imag-calendar/src/lib.rs @@ -34,7 +34,7 @@ while_true, )] -#[macro_use] extern crate failure; +#[macro_use] extern crate anyhow; #[macro_use] extern crate log; extern crate clap; extern crate toml_query; @@ -43,6 +43,7 @@ extern crate handlebars; extern crate chrono; extern crate kairos; extern crate resiter; +extern crate failure; extern crate libimagrt; extern crate libimagcalendar; @@ -53,9 +54,9 @@ extern crate libimagutil; use std::path::PathBuf; use std::io::Write; -use failure::Error; -use failure::err_msg; -use failure::Fallible as Result; +use anyhow::Error; + +use anyhow::Result; use toml_query::read::Partial; use toml_query::read::TomlValueReadExt; use walkdir::DirEntry; @@ -82,7 +83,7 @@ mod util; pub enum ImagCalendar {} impl ImagApplication for ImagCalendar { fn run(rt: Runtime) -> Result<()> { - match rt.cli().subcommand_name().ok_or_else(|| err_msg("No subcommand called"))? { + match rt.cli().subcommand_name().ok_or_else(|| anyhow!("No subcommand called"))? { "import" => import(&rt), "list" => list(&rt), "show" => show(&rt), @@ -92,7 +93,7 @@ impl ImagApplication for ImagCalendar { if rt.handle_unknown_subcommand("imag-calendar", other, rt.cli())?.success() { Ok(()) } else { - Err(err_msg("Failed to handle unknown subcommand")) + Err(anyhow!("Failed to handle unknown subcommand")) } }, } @@ -121,14 +122,14 @@ fn import(rt: &Runtime) -> Result<()> { let do_fail = scmd.is_present("import-fail"); let force_override = scmd.is_present("import-force-override"); let ref_config = rt.config() - .ok_or_else(|| format_err!("No configuration, cannot continue!"))? + .ok_or_else(|| anyhow!("No configuration, cannot continue!"))? .read_partial::<libimagentryref::reference::Config>()? - .ok_or_else(|| format_err!("Configuration missing: {}", libimagentryref::reference::Config::LOCATION))?; + .ok_or_else(|| anyhow!("Configuration missing: {}", libimagentryref::reference::Config::LOCATION))?; // sanity check debug!("Doing sanity check on config, to see whether the configuration required for importing is there"); if ref_config.get(collection_name).is_none() { - return Err(format_err!("Configuration missing: {}.{}", libimagentryref::reference::Config::LOCATION, collection_name)) + return Err(anyhow!("Configuration missing: {}.{}", libimagentryref::reference::Config::LOCATION, collection_name)) } debug!("Starting import..."); @@ -191,9 +192,9 @@ fn list(rt: &Runtime) -> Result<()> { let do_filter_before = scmd.value_of("list-before"); let do_filter_after = scmd.value_of("list-after"); let ref_config = rt.config() - .ok_or_else(|| format_err!("No configuration, cannot continue!"))? + .ok_or_else(|| anyhow!("No configuration, cannot continue!"))? .read_partial::<libimagentryref::reference::Config>()? - .ok_or_else(|| format_err!("Configuration missing: {}", libimagentryref::reference::Config::LOCATION))?; + .ok_or_else(|| anyhow!("Configuration missing: {}", libimagentryref::reference::Config::LOCATION))?; debug!("List format: {:?}", list_format); debug!("Ref config : {:?}", ref_config); @@ -234,13 +235,13 @@ fn list(rt: &Runtime) -> Result<()> { rt.store() .all_events()? .and_then_ok(|sid| rt.store().get(sid)) - .map_inner_ok_or_else(|| err_msg("Missing entrty while calling all_events()")) + .map_inner_ok_or_else(|| anyhow!("Missing entrty while calling all_events()")) .and_then_ok(|ev| ParsedEventFLE::parse(ev, &ref_config)) .and_then_ok(|parsed_entry| { parsed_entry .get_data() .events() - .map_err(|component| format_err!("Failed to parse entry: {}", component.name)) + .map_err(|component| anyhow!("Failed to parse entry: {}", component.name)) .and_then_ok(|event| { event_filter(&event).map(|b| (event, b)) }) @@ -264,9 +265,9 @@ fn list(rt: &Runtime) -> Result<()> { fn show(rt: &Runtime) -> Result<()> { let scmd = rt.cli().subcommand_matches("show").unwrap(); // safe by clap let ref_config = rt.config() - .ok_or_else(|| format_err!("No configuration, cannot continue!"))? + .ok_or_else(|| anyhow!("No configuration, cannot continue!"))? .read_partial::<libimagentryref::reference::Config>()? - .ok_or_else(|| format_err!("Configuration missing: {}", libimagentryref::reference::Config::LOCATION))?; + .ok_or_else(|| anyhow!("Configuration missing: {}", libimagentryref::reference::Config::LOCATION))?; let list_format = util::get_event_print_format("calendar.show_format", rt, &scmd)?; @@ -280,14 +281,14 @@ fn show(rt: &Runtime) -> Result<()> { Ok((e, id)) }) .and_then_ok(|tpl| match tpl { - (None, id) => Err(format_err!("Missing entry: {}", id)), + (None, id) => Err(anyhow!("Missing entry: {}", id)), (Some(e), id) => Ok((e, id)), }) .and_then_ok(|(parsed_entry, id)| { parsed_entry .get_data() .events() - .map_err(|component| format_err!("Failed to parse entry: {}", component.name)) + .map_err(|component| anyhow!("Failed to parse entry: {}", component.name)) .filter_ok(|pent| { let relevant = pent.uid().map(|uid| uid.raw().starts_with(id)).unwrap_or(false); debug!("Relevant {} => {}", parsed_entry.get_entry().get_location(), relevant); diff --git a/bin/domain/imag-calendar/src/util.rs b/bin/domain/imag-calendar/src/util.rs index 8dd382d9..1467d310 100644 --- a/bin/domain/imag-calendar/src/util.rs +++ b/bin/domain/imag-calendar/src/util.rs @@ -23,9 +23,10 @@ use clap::ArgMatches; use vobject::icalendar::ICalendar; use vobject::icalendar::Event; use handlebars::Handlebars; -use failure::Fallible as Result; -use failure::Error; -use failure::err_msg; +use anyhow::Result; +use anyhow::Error; +use failure::Fail; + use toml_query::read::TomlValueReadTypeExt; use chrono::NaiveDateTime; @@ -36,7 +37,6 @@ use libimagentryref::reference::fassade::RefFassade; use libimagentryref::reference::Ref; use libimagentryref::reference::Config; use libimagentryref::hasher::default::DefaultHasher; -use libimagerror::trace::MapErrTrace; use crate::libimagcalendar::store::EventStore; #[derive(Debug)] @@ -54,8 +54,8 @@ impl<'a> ParsedEventFLE<'a> { pub fn parse(fle: FileLockEntry<'a>, refconfig: &Config) -> Result<Self> { fle.as_ref_with_hasher::<DefaultHasher>() .get_path(refconfig) - .and_then(|p| ::std::fs::read_to_string(p).map_err(Error::from)) - .and_then(|s| ICalendar::build(&s).map_err(Error::from)) + .and_then(|p| ::std::fs::read_to_string(p).map_err(|e| Error::from(e.compat()))) + .and_then(|s| ICalendar::build(&s).map_err(|e| Error::from(e.compat()))) .map(|cal| ParsedEventFLE { inner: fle, data: cal, @@ -79,9 +79,9 @@ pub fn get_event_print_format(config_value_path: &'static str, rt: &Runtime, scm .map(Ok) .unwrap_or_else(|| { rt.config() - .ok_or_else(|| err_msg("No configuration file"))? + .ok_or_else(|| anyhow!("No configuration file"))? .read_string(config_value_path)? - .ok_or_else(|| err_msg("Configuration 'contact.list_format' does not exist")) + .ok_or_else(|| anyhow!("Configuration 'contact.list_format' does not exist")) }) .and_then(|fmt| { let mut hb = Handlebars::new(); @@ -124,19 +124,19 @@ pub fn build_data_object_for_handlebars<'a>(i: usize, event: &Event<'a>) } pub fn kairos_parse(spec: &str) -> Result<NaiveDateTime> { - match ::kairos::parser::parse(spec).map_err_trace_exit_unwrap() { + match ::kairos::parser::parse(spec).map_err(|e| Error::from(e.compat()))? { ::kairos::parser::Parsed::Iterator(_) => { trace!("before-filter spec resulted in iterator"); - Err(format_err!("Not a moment in time: {}", spec)) + Err(anyhow!("Not a moment in time: {}", spec)) } ::kairos::parser::Parsed::TimeType(tt) => { trace!("before-filter spec resulted in timetype"); tt.calculate() - .map_err_trace_exit_unwrap() + .map_err(|e| Error::from(e.compat()))? .get_moment() .cloned() - .ok_or_else(|| format_err!("Not a moment in time: {}", spec)) + .ok_or_else(|| anyhow!("Not a moment in time: {}", spec)) } } } @@ -151,7 +151,7 @@ pub fn find_event_by_id<'a>(store: &'a Store, id: &str, refconfig: &Config) -> R let sid = sid?; let event = store.get(sid.clone())?.ok_or_else(|| { - format_err!("Cannot get {}, which should be there.", sid) + anyhow!("Cannot get {}, which should be there.", sid) })?; trace!("Checking whether {} is represented by {}", id, event.get_location()); diff --git a/bin/domain/imag-contact/Cargo.toml b/bin/domain/imag-contact/Cargo.toml index da559479..03f8ad40 100644 --- a/bin/domain/imag-contact/Cargo.toml +++ b/bin/domain/imag-contact/Cargo.toml @@ -27,7 +27,7 @@ handlebars = "2" walkdir = "2.2.8" uuid = { version = "0.7.4", features = ["v4"] } serde_json = "1.0.39" -failure = "0.1.5" +anyhow = "1" resiter = "0.4" libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" } diff --git a/bin/domain/imag-contact/src/create.rs b/bin/domain/imag-contact/src/create.rs index 22394f7a..3c8b3f07 100644 --- a/bin/domain/imag-contact/src/create.rs +++ b/bin/domain/imag-contact/src/create.rs @@ -45,10 +45,10 @@ use toml_query::read::TomlValueReadExt; use toml_query::read::Partial; use toml::Value; use uuid::Uuid; -use failure::Error; -use failure::err_msg; -use failure::Fallible as Result; -use failure::ResultExt; +use anyhow::Error; + +use anyhow::Result; +use anyhow::Context; use libimagcontact::store::ContactStore; use libimagrt::runtime::Runtime; @@ -85,15 +85,15 @@ pub fn create(rt: &Runtime) -> Result<()> { let collection_name = String::from(collection_name); let ref_config = rt // TODO: Re-Deserialize to libimagentryref::reference::Config .config() - .ok_or_else(|| err_msg("Configuration missing, cannot continue!"))? + .ok_or_else(|| anyhow!("Configuration missing, cannot continue!"))? .read_partial::<libimagentryref::reference::Config>()? - .ok_or_else(|| format_err!("Configuration missing: {}", libimagentryref::reference::Config::LOCATION))?; + .ok_or_else(|| anyhow!("Configuration missing: {}", libimagentryref::reference::Config::LOCATION))?; // TODO: Refactor the above to libimagutil or libimagrt? let (mut dest, location, uuid) : (Box<dyn Write>, Option<PathBuf>, String) = { if let Some(mut fl) = scmd.value_of("file-location").map(PathBuf::from) { let uuid = if fl.is_file() { - return Err(err_msg("File does exist, cannot create/override")) + return Err(anyhow!("File does exist, cannot create/override")) } else if fl.is_dir() { let uuid = Uuid::new_v4().to_hyphenated().to_string(); fl.push(uuid.clone()); @@ -135,7 +135,7 @@ pub fn create(rt: &Runtime) -> Result<()> { None => fl.file_name() .and_then(|fname| fname.to_str()) .map(String::from) - .ok_or_else(|| err_msg("Cannot calculate UUID for vcard"))?, + .ok_or_else(|| anyhow!("Cannot calculate UUID for vcard"))?, }; (Box::new(file), Some(fl), uuid_string) @@ -147,7 +147,7 @@ pub fn create(rt: &Runtime) -> Result<()> { }; let mut input = rt.stdin().ok_or_else(|| { - err_msg("No input stream. Cannot ask for permission") + anyhow!("No input stream. Cannot ask for permission") })?; let mut output = rt.stdout(); @@ -156,7 +156,7 @@ pub fn create(rt: &Runtime) -> Result<()> { ::libimagentryedit::edit::edit_in_tmpfile(&rt, &mut template)?; if template == TEMPLATE || template.is_empty() { - return Err(err_msg("No (changed) content in tempfile. Not doing anything.")) + return Err(anyhow!("No (changed) content in tempfile. Not doing anything.")) } match ::toml::de::from_str(&template) @@ -265,7 +265,7 @@ fn parse_toml_into_vcard(output: &mut dyn Write, input: &mut dyn Read, toml: Val if ask_continue(input, output)? { return Ok(None) } else { - return Err(format_err!("Key 'nickname.[{}].name' missing", i)) + return Err(anyhow!("Key 'nickname.[{}].name' missing", i)) } }, }; @@ -286,7 +286,7 @@ fn parse_toml_into_vcard(output: &mut dyn Write, input: &mut dyn Read, toml: Val if ask_continue(input, output)? { return Ok(None) } else { - return Err(format_err!("Type Error: Expected Array or String at 'nickname'")) + return Err(anyhow!("Type Error: Expected Array or String at 'nickname'")) } }, None => { @@ -326,7 +326,7 @@ fn parse_toml_into_vcard(output: &mut dyn Write, input: &mut dyn Read, toml: Val if ask_continue(input, output)? { return Ok(None) } else { - return Err(format_err!("Key 'phones.[{}].type' missing", i)) + return Err(anyhow!("Key 'phones.[{}].type' missing", i)) } } }; @@ -338,7 +338,7 @@ fn parse_toml_into_vcard(output: &mut dyn Write, input: &mut dyn Read, toml: Val |