diff options
Diffstat (limited to 'bin/domain/imag-todo/src')
-rw-r--r-- | bin/domain/imag-todo/src/import.rs | 23 | ||||
-rw-r--r-- | bin/domain/imag-todo/src/lib.rs | 64 | ||||
-rw-r--r-- | bin/domain/imag-todo/src/ui.rs | 2 | ||||
-rw-r--r-- | bin/domain/imag-todo/src/util.rs | 15 |
4 files changed, 49 insertions, 55 deletions
diff --git a/bin/domain/imag-todo/src/import.rs b/bin/domain/imag-todo/src/import.rs index 8d5b6e81..810faa55 100644 --- a/bin/domain/imag-todo/src/import.rs +++ b/bin/domain/imag-todo/src/import.rs @@ -17,8 +17,8 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // -use failure::Fallible as Result; -use failure::err_msg; +use anyhow::Result; +use anyhow::Error; use libimagrt::runtime::Runtime; @@ -26,14 +26,14 @@ pub fn import(rt: &Runtime) -> Result<()> { let scmd = rt.cli().subcommand().1.unwrap(); match scmd.subcommand_name() { - None => Err(err_msg("No subcommand called")), + None => Err(anyhow!("No subcommand called")), Some("taskwarrior") => import_taskwarrior(rt), Some(other) => { debug!("Unknown command"); if rt.handle_unknown_subcommand("imag-todo-import", other, rt.cli())?.success() { Ok(()) } else { - Err(err_msg("Failed to handle unknown subcommand")) + Err(anyhow!("Failed to handle unknown subcommand")) } }, } @@ -43,7 +43,7 @@ pub fn import(rt: &Runtime) -> Result<()> { fn import_taskwarrior(rt: &Runtime) -> Result<()> { #[cfg(not(feature = "import-taskwarrior"))] { - Err(err_msg("Binary not compiled with taskwarrior import functionality")) + Err(anyhow!("Binary not compiled with taskwarrior import functionality")) } #[cfg(feature = "import-taskwarrior")] @@ -65,7 +65,7 @@ fn import_taskwarrior(rt: &Runtime) -> Result<()> { let store = rt.store(); if !rt.input_is_pipe() { - return Err(err_msg("Cannot get stdin for importing tasks")) + return Err(anyhow!("Cannot get stdin for importing tasks")) } let stdin = ::std::io::stdin(); @@ -86,7 +86,8 @@ fn import_taskwarrior(rt: &Runtime) -> Result<()> { } }; - taskwarrior_import(stdin)? + taskwarrior_import(stdin) + .map_err(|e| Error::from(e.compat()))? .into_iter() .map(|task| { let mut todo = store @@ -103,12 +104,12 @@ fn import_taskwarrior(rt: &Runtime) -> Result<()> { todo.set_content(task.description().clone()); if let Some(tags) = task.tags() { - tags.iter().map(|tag| { + tags.iter().map(String::from).map(|tag| { if libimagentrytag::tag::is_tag_str(&tag).is_err() { warn!("Not a valid tag, ignoring: {}", tag); Ok(()) } else { - todo.add_tag(tag.clone()) + todo.add_tag(tag) } }).collect::<Result<Vec<_>>>()?; } @@ -142,14 +143,14 @@ fn import_taskwarrior(rt: &Runtime) -> Result<()> { .filter(|(_, list)| !list.is_empty()) .map(|(key, list)| { let mut entry = store.get_todo_by_uuid(key)?.ok_or_else(|| { - format_err!("Cannot find todo by UUID: {}", key) + anyhow!("Cannot find todo by UUID: {}", key) })?; list.iter() .map(move |element| { store.get_todo_by_uuid(element)? .ok_or_else(|| { - format_err!("Cannot find todo by UUID: {}", key) + anyhow!("Cannot find todo by UUID: {}", key) }) .and_then(|mut target| entry.add_link(&mut target)) }) diff --git a/bin/domain/imag-todo/src/lib.rs b/bin/domain/imag-todo/src/lib.rs index 42f9baeb..db93004d 100644 --- a/bin/domain/imag-todo/src/lib.rs +++ b/bin/domain/imag-todo/src/lib.rs @@ -41,7 +41,7 @@ extern crate chrono; extern crate filters; extern crate kairos; #[macro_use] extern crate log; -#[macro_use] extern crate failure; +#[macro_use] extern crate anyhow; extern crate resiter; extern crate handlebars; extern crate prettytable; @@ -69,14 +69,13 @@ extern crate libimaginteraction; use std::ops::Deref; use std::io::Write; -use std::result::Result as RResult; use std::str::FromStr; use clap::ArgMatches; use chrono::NaiveDateTime; -use failure::Error; -use failure::Fallible as Result; -use failure::err_msg; +use anyhow::Error; +use anyhow::Result; + use clap::App; use resiter::AndThen; use resiter::IterInnerOkOrElse; @@ -121,7 +120,7 @@ impl ImagApplication for ImagTodo { if rt.handle_unknown_subcommand("imag-todo", other, rt.cli())?.success() { Ok(()) } else { - Err(err_msg("Failed to handle unknown subcommand")) + Err(anyhow!("Failed to handle unknown subcommand")) } } } // end match scmd @@ -219,11 +218,11 @@ fn create(rt: &Runtime) -> Result<()> { fn mark(rt: &Runtime) -> Result<()> { fn mark_todos_as(rt: &Runtime, status: Status) -> 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(|e| rt.report_touched(e.get_location()).map(|_| e)) .and_then_ok(|mut e| e.set_status(status.clone())) .collect() @@ -234,8 +233,8 @@ fn mark(rt: &Runtime) -> Result<()> { Some("done") => mark_todos_as(rt, Status::Done), Some("deleted") => mark_todos_as(rt, Status::Deleted), Some("pending") => mark_todos_as(rt, Status::Pending), - Some(other) => Err(format_err!("Unknown mark type selected: {}", other)), - None => Err(format_err!("No mark type selected, doing nothing!")), + Some(other) => Err(anyhow!("Unknown mark type selected: {}", other)), + None => Err(anyhow!("No mark type selected, doing nothing!")), } } @@ -249,19 +248,17 @@ fn list_todos(rt: &Runtime, matcher: &StatusMatcher, show_hidden: bool) -> Resul details: bool, } impl Viewer for TodoViewer { - fn view_entry<W>(&self, entry: &Entry, sink: &mut W) -> RResult<(), libimagentryview::error::Error> + fn view_entry<W>(&self, entry: &Entry, sink: &mut W) -> Result<()> where W: Write { - use libimagentryview::error::Error as E; - trace!("Viewing entry: {}", entry.get_location()); - if !entry.is_todo().map_err(E::from)? { - return Err(format_err!("Not a Todo: {}", entry.get_location())).map_err(E::from); + if !entry.is_todo()? { + return Err(anyhow!("Not a Todo: {}", entry.get_location())); } - let uuid = entry.get_uuid().map_err(E::from)?; - let status = entry.get_status().map_err(E::from)?; + let uuid = entry.get_uuid()?; + let status = entry.get_status()?; let status = status.as_str(); let first_line = entry.get_content() .lines() @@ -272,14 +269,14 @@ fn list_todos(rt: &Runtime, matcher: &StatusMatcher, show_hidden: bool) -> Resul let r = writeln!(sink, "{uuid} - {status} : {first_line}", uuid = uuid, status = status, - first_line = first_line); + first_line = first_line).map_err(Error::from); trace!("Viewing entry result: {:?}", r); r } else { let sched = util::get_dt_str(entry.get_scheduled(), "Not scheduled")?; let hidden = util::get_dt_str(entry.get_hidden(), "Not hidden")?; let due = util::get_dt_str(entry.get_due(), "No due")?; - let priority = entry.get_priority().map_err(E::from)?.map(|p| p.as_str().to_string()) + let priority = entry.get_priority()?.map(|p| p.as_str().to_string()) .unwrap_or_else(|| "No prio".to_string()); let r = writeln!(sink, "{uuid} - {status} - {sched} - {hidden} - {due} - {prio}: {first_line}", @@ -289,12 +286,11 @@ fn list_todos(rt: &Runtime, matcher: &StatusMatcher, show_hidden: bool) -> Resul hidden = hidden, due = due, prio = priority, - first_line = first_line); + first_line = first_line).map_err(Error::from); trace!("Viewing entry result: {:?}", r); r } - .map_err(libimagentryview::error::Error::from) } } @@ -343,18 +339,18 @@ fn list_todos(rt: &Runtime, matcher: &StatusMatcher, show_hidden: bool) -> Resul if rt.ids_from_stdin() { trace!("Getting IDs from stdin"); 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")); process(&rt, matcher, show_hidden, iter) } else { trace!("Getting IDs from store"); let iter = rt.store().get_todos()? .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")); process(&rt, matcher, show_hidden, iter) } @@ -426,15 +422,13 @@ fn show(rt: &Runtime) -> Result<()> { }; iter.map(|entry| { - use libimagentryview::error::Error as E; - - let uuid = entry.get_uuid().map_err(E::from)?.to_hyphenated().to_string(); - let status = entry.get_status().map_err(E::from)?; + let uuid = entry.get_uuid()?.to_hyphenated().to_string(); + let status = entry.get_status()?; let status = status.as_str().to_string(); let sched = util::get_dt_str(entry.get_scheduled(), "Not scheduled")?; let hidden = util::get_dt_str(entry.get_hidden(), "Not hidden")?; let due = util::get_dt_str(entry.get_due(), "No due")?; - let priority = entry.get_priority().map_err(E::from)?.map(|p| p.as_str().to_string()).unwrap_or_else(|| "No prio".to_string()); + let priority = entry.get_priority()?.map(|p| p.as_str().to_string()).unwrap_or_else(|| "No prio".to_string()); let text = entry.get_content().to_owned(); @@ -461,11 +455,11 @@ fn show(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")) .and_then_ok(|e| rt.report_touched(e.get_location()).map(|_| e)) .collect::<Result<Vec<_>>>()? .into_iter(); @@ -493,13 +487,13 @@ fn get_datetime_arg(scmd: &ArgMatches, argname: &'static str) -> Result<Option<N match scmd.value_of(argname) { None => Ok(None), - Some(v) => match parser::parse(v)? { + Some(v) => match parser::parse(v).map_err(|e| Error::from(e.compat()))? { parser::Parsed::TimeType(TimeType::Moment(moment)) => Ok(Some(moment)), parser::Parsed::TimeType(other) => { - Err(format_err!("You did not pass a date, but a {}", other.name())) + Err(anyhow!("You did not pass a date, but a {}", other.name())) }, parser::Parsed::Iterator(_) => { - Err(format_err!("Argument {} results in a list of dates, but we need a single date.", v)) + Err(anyhow!("Argument {} results in a list of dates, but we need a single date.", v)) } } } @@ -510,7 +504,7 @@ fn prio_from_str<S: AsRef<str>>(s: S) -> Result<Priority> { "h" => Ok(Priority::High), "m" => Ok(Priority::Medium), "l" => Ok(Priority::Low), - other => Err(format_err!("Unsupported Priority: '{}'", other)), + other => Err(anyhow!("Unsupported Priority: '{}'", other)), } } diff --git a/bin/domain/imag-todo/src/ui.rs b/bin/domain/imag-todo/src/ui.rs index 1172bb60..ec64799e 100644 --- a/bin/domain/imag-todo/src/ui.rs +++ b/bin/domain/imag-todo/src/ui.rs @@ -19,7 +19,7 @@ use std::path::PathBuf; use clap::{Arg, ArgMatches, App, SubCommand}; -use failure::Fallible as Result; +use anyhow::Result; use libimagstore::storeid::StoreId; use libimagstore::storeid::IntoStoreId; diff --git a/bin/domain/imag-todo/src/util.rs b/bin/domain/imag-todo/src/util.rs index bafd40a1..9fce60e6 100644 --- a/bin/domain/imag-todo/src/util.rs +++ b/bin/domain/imag-todo/src/util.rs @@ -18,11 +18,10 @@ // use std::collections::BTreeMap; -use std::result::Result as RResult; -use failure::Fallible as Result; -use failure::Error; -use failure::err_msg; +use anyhow::Result; +use anyhow::Error; + use handlebars::Handlebars; use clap::ArgMatches; use chrono::NaiveDateTime; @@ -33,8 +32,8 @@ use libimagstore::store::Entry; use libimagtodo::entry::Todo; use libimagutil::date::datetime_to_string; -pub fn get_dt_str(d: Result<Option<NaiveDateTime>>, s: &str) -> RResult<String, libimagentryview::error::Error> { - Ok(d.map_err(libimagentryview::error::Error::from)? +pub fn get_dt_str(d: Result<Option<NaiveDateTime>>, s: &str) -> Result<String> { + Ok(d? .map(|v| datetime_to_string(&v)) .unwrap_or_else(|| s.to_string())) } @@ -44,10 +43,10 @@ pub fn get_todo_print_format(config_value_path: &'static str, rt: &Runtime, scmd Some(s) => Ok(s), None => { rt.config() - .ok_or_else(|| err_msg("No configuration file"))? + .ok_or_else(|| anyhow!("No configuration file"))? .read_string(config_value_path) .map_err(Error::from)? - .ok_or_else(|| format_err!("Configuration '{}' does not exist", config_value_path)) + .ok_or_else(|| anyhow!("Configuration '{}' does not exist", config_value_path)) } }?; |