Replace 'failure' crate with anyhow+thiserror

Closes #148
This commit is contained in:
dbr
2021-03-25 16:33:35 +11:00
parent 3cccdc7e32
commit 4d9755c43b
41 changed files with 255 additions and 316 deletions

View File

@@ -1,5 +1,4 @@
use crate::argparse::{DescriptionMod, Modification};
use failure::Fallible;
use taskchampion::{Replica, Status};
use termcolor::WriteColor;
@@ -7,7 +6,7 @@ pub(crate) fn execute<W: WriteColor>(
w: &mut W,
replica: &mut Replica,
modification: Modification,
) -> Fallible<()> {
) -> anyhow::Result<()> {
let description = match modification.description {
DescriptionMod::Set(ref s) => s.clone(),
_ => "(no description)".to_owned(),

View File

@@ -1,8 +1,7 @@
use failure::Fallible;
use taskchampion::Replica;
use termcolor::WriteColor;
pub(crate) fn execute<W: WriteColor>(w: &mut W, replica: &mut Replica) -> Fallible<()> {
pub(crate) fn execute<W: WriteColor>(w: &mut W, replica: &mut Replica) -> anyhow::Result<()> {
log::debug!("rebuilding working set");
replica.rebuild_working_set(true)?;
writeln!(w, "garbage collected.")?;

View File

@@ -1,12 +1,11 @@
use crate::usage::Usage;
use failure::Fallible;
use termcolor::WriteColor;
pub(crate) fn execute<W: WriteColor>(
w: &mut W,
command_name: String,
summary: bool,
) -> Fallible<()> {
) -> anyhow::Result<()> {
let usage = Usage::new();
usage.write_help(w, command_name.as_ref(), summary)?;
Ok(())

View File

@@ -1,7 +1,6 @@
use crate::argparse::Filter;
use crate::invocation::filtered_tasks;
use crate::table;
use failure::Fallible;
use prettytable::{cell, row, Table};
use taskchampion::Replica;
use termcolor::WriteColor;
@@ -11,7 +10,7 @@ pub(crate) fn execute<W: WriteColor>(
replica: &mut Replica,
filter: Filter,
debug: bool,
) -> Fallible<()> {
) -> anyhow::Result<()> {
let working_set = replica.working_set()?;
for task in filtered_tasks(replica, &filter)? {

View File

@@ -1,6 +1,5 @@
use crate::argparse::{Filter, Modification};
use crate::invocation::{apply_modification, filtered_tasks};
use failure::Fallible;
use taskchampion::Replica;
use termcolor::WriteColor;
@@ -9,7 +8,7 @@ pub(crate) fn execute<W: WriteColor>(
replica: &mut Replica,
filter: Filter,
modification: Modification,
) -> Fallible<()> {
) -> anyhow::Result<()> {
for task in filtered_tasks(replica, &filter)? {
let mut task = task.into_mut(replica);

View File

@@ -1,7 +1,6 @@
use crate::argparse::Filter;
use crate::invocation::display_report;
use config::Config;
use failure::Fallible;
use taskchampion::Replica;
use termcolor::WriteColor;
@@ -11,7 +10,7 @@ pub(crate) fn execute<W: WriteColor>(
settings: &Config,
report_name: String,
filter: Filter,
) -> Fallible<()> {
) -> anyhow::Result<()> {
display_report(w, replica, settings, report_name, filter)
}

View File

@@ -1,4 +1,3 @@
use failure::Fallible;
use taskchampion::{server::Server, Replica};
use termcolor::WriteColor;
@@ -6,8 +5,8 @@ pub(crate) fn execute<W: WriteColor>(
w: &mut W,
replica: &mut Replica,
server: &mut Box<dyn Server>,
) -> Fallible<()> {
replica.sync(server)?;
) -> anyhow::Result<()> {
replica.sync(server).unwrap();
writeln!(w, "sync complete.")?;
Ok(())
}

View File

@@ -1,7 +1,6 @@
use failure::Fallible;
use termcolor::{ColorSpec, WriteColor};
pub(crate) fn execute<W: WriteColor>(w: &mut W) -> Fallible<()> {
pub(crate) fn execute<W: WriteColor>(w: &mut W) -> anyhow::Result<()> {
write!(w, "TaskChampion ")?;
w.set_color(ColorSpec::new().set_bold(true))?;
writeln!(w, "{}", env!("CARGO_PKG_VERSION"))?;

View File

@@ -1,5 +1,4 @@
use crate::argparse::{Condition, Filter, TaskId};
use failure::Fallible;
use std::collections::HashSet;
use std::convert::TryInto;
use taskchampion::{Replica, Status, Tag, Task, Uuid, WorkingSet};
@@ -108,7 +107,7 @@ fn universe_for_filter(filter: &Filter) -> Universe {
pub(super) fn filtered_tasks(
replica: &mut Replica,
filter: &Filter,
) -> Fallible<impl Iterator<Item = Task>> {
) -> anyhow::Result<impl Iterator<Item = Task>> {
let mut res = vec![];
log::debug!("Applying filter {:?}", filter);
@@ -253,7 +252,7 @@ mod test {
}
#[test]
fn tag_filtering() -> Fallible<()> {
fn tag_filtering() -> anyhow::Result<()> {
let mut replica = test_replica();
let yes: Tag = "yes".try_into()?;
let no: Tag = "no".try_into()?;

View File

@@ -2,7 +2,6 @@
use crate::argparse::{Command, Subcommand};
use config::Config;
use failure::{format_err, Fallible};
use taskchampion::{Replica, Server, ServerConfig, StorageConfig, Uuid};
use termcolor::{ColorChoice, StandardStream};
@@ -20,7 +19,7 @@ use report::display_report;
/// Invoke the given Command in the context of the given settings
#[allow(clippy::needless_return)]
pub(crate) fn invoke(command: Command, settings: Config) -> Fallible<()> {
pub(crate) fn invoke(command: Command, settings: Config) -> anyhow::Result<()> {
log::debug!("command: {:?}", command);
log::debug!("settings: {:?}", settings);
@@ -101,7 +100,7 @@ pub(crate) fn invoke(command: Command, settings: Config) -> Fallible<()> {
// utilities for invoke
/// Get the replica for this invocation
fn get_replica(settings: &Config) -> Fallible<Replica> {
fn get_replica(settings: &Config) -> anyhow::Result<Replica> {
let taskdb_dir = settings.get_str("data_dir")?.into();
log::debug!("Replica data_dir: {:?}", taskdb_dir);
let storage_config = StorageConfig::OnDisk { taskdb_dir };
@@ -109,7 +108,7 @@ fn get_replica(settings: &Config) -> Fallible<Replica> {
}
/// Get the server for this invocation
fn get_server(settings: &Config) -> Fallible<Box<dyn Server>> {
fn get_server(settings: &Config) -> anyhow::Result<Box<dyn Server>> {
// if server_client_key and server_origin are both set, use
// the remote server
let config = if let (Ok(client_key), Ok(origin)) = (
@@ -119,7 +118,7 @@ fn get_server(settings: &Config) -> Fallible<Box<dyn Server>> {
let client_key = Uuid::parse_str(&client_key)?;
let encryption_secret = settings
.get_str("encryption_secret")
.map_err(|_| format_err!("Could not read `encryption_secret` configuration"))?;
.map_err(|_| anyhow::anyhow!("Could not read `encryption_secret` configuration"))?;
log::debug!("Using sync-server with origin {}", origin);
log::debug!("Sync client ID: {}", client_key);
@@ -137,7 +136,7 @@ fn get_server(settings: &Config) -> Fallible<Box<dyn Server>> {
}
/// Get a WriteColor implementation based on whether the output is a tty.
fn get_writer() -> Fallible<StandardStream> {
fn get_writer() -> anyhow::Result<StandardStream> {
Ok(StandardStream::stdout(if atty::is(atty::Stream::Stdout) {
ColorChoice::Auto
} else {

View File

@@ -1,5 +1,4 @@
use crate::argparse::{DescriptionMod, Modification};
use failure::Fallible;
use std::convert::TryInto;
use taskchampion::TaskMut;
use termcolor::WriteColor;
@@ -9,7 +8,7 @@ pub(super) fn apply_modification<W: WriteColor>(
w: &mut W,
task: &mut TaskMut,
modification: &Modification,
) -> Fallible<()> {
) -> anyhow::Result<()> {
match modification.description {
DescriptionMod::Set(ref description) => task.set_description(description.clone())?,
DescriptionMod::Prepend(ref description) => {

View File

@@ -3,7 +3,6 @@ use crate::invocation::filtered_tasks;
use crate::report::{Column, Property, Report, SortBy};
use crate::table;
use config::Config;
use failure::{format_err, Fallible};
use prettytable::{Row, Table};
use std::cmp::Ordering;
use taskchampion::{Replica, Task, WorkingSet};
@@ -83,13 +82,13 @@ pub(super) fn display_report<W: WriteColor>(
settings: &Config,
report_name: String,
filter: Filter,
) -> Fallible<()> {
) -> anyhow::Result<()> {
let mut t = Table::new();
let working_set = replica.working_set()?;
// Get the report from settings
let mut report = Report::from_config(settings.get(&format!("reports.{}", report_name))?)
.map_err(|e| format_err!("report.{}{}", report_name, e))?;
.map_err(|e| anyhow::anyhow!("report.{}{}", report_name, e))?;
// include any user-supplied filter conditions
report.filter = report.filter.intersect(filter);