From 2bfa51234cf31eec0ee8e6009424d1aa4bcfc012 Mon Sep 17 00:00:00 2001 From: Shaishav Gandhi <shaishgandhi@gmail.com> Date: Sat, 2 May 2020 15:10:19 -0700 Subject: [PATCH] Move to anyhow instead of failure (#73) --- Cargo.lock | 42 +++++++----------------------------------- Cargo.toml | 2 +- src/auth.rs | 10 ++++------ src/diffs.rs | 4 ++-- src/main.rs | 6 +++--- src/preferences.rs | 2 +- src/summary.rs | 2 +- src/tasks.rs | 4 ++-- src/users.rs | 2 +- 9 files changed, 22 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eef4434..99c8804 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,11 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "anyhow" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9a60d744a80c30fcb657dfe2c1b22bcb3e814c1a1e3674f32bf5820b570fbff" + [[package]] name = "arc-swap" version = "0.4.5" @@ -358,6 +364,7 @@ dependencies = [ name = "fab" version = "0.4.2" dependencies = [ + "anyhow", "clap", "clap_generate", "comfy-table", @@ -365,7 +372,6 @@ dependencies = [ "console", "dialoguer", "dirs", - "failure", "futures", "reqwest", "serde", @@ -373,28 +379,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "failure" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8529c2421efa3066a5cbd8063d2244603824daccb6936b079010bb2aa89464b" -dependencies = [ - "backtrace", - "failure_derive", -] - -[[package]] -name = "failure_derive" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "030a733c8287d6213886dd487564ff5c8f6aae10278b3588ed177f9d18f8d231" -dependencies = [ - "proc-macro2", - "quote", - "syn", - "synstructure", -] - [[package]] name = "fnv" version = "1.0.6" @@ -1388,18 +1372,6 @@ dependencies = [ "syn", ] -[[package]] -name = "synstructure" -version = "0.12.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67656ea1dc1b41b1451851562ea232ec2e5a80242139f7e679ceccfb5d61f545" -dependencies = [ - "proc-macro2", - "quote", - "syn", - "unicode-xid", -] - [[package]] name = "tempdir" version = "0.3.7" diff --git a/Cargo.toml b/Cargo.toml index 16a4422..744552e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,6 @@ comfy-table = "0.1.0" console="0.10.0" confy = "0.4.0" dialoguer = "0.5.0" -failure = "0.1.7" +anyhow = "1.0.28" futures= {version = "0.3.4", features = ["thread-pool"]} tokio = "0.2.16" \ No newline at end of file diff --git a/src/auth.rs b/src/auth.rs index db6a231..011b1e2 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,6 +1,6 @@ use crate::structs::{FabConfig, WhoAmIResponse}; use crate::WHO_AM_I; -use failure::Error; +use anyhow::{anyhow, Error}; use reqwest::RequestBuilder; use serde::Deserialize; use std::fs::{read_to_string, File}; @@ -76,9 +76,7 @@ pub async fn send<T: serde::de::DeserializeOwned>( } } } - Result::Err(failure::err_msg( - "Token regenerated. Please try the command again", - )) + Err(anyhow!("Token regenerated. Please try the command again",)) } /// Prompts for a token and writes the token to the configuration file. @@ -92,7 +90,7 @@ fn prompt_token(hosted_instance: &str) -> Result<String, Error> { api_token = api_token.trim().to_string(); if api_token.is_empty() { - return Result::Err(failure::err_msg("API Token cannot be null or empty")); + return Result::Err(anyhow!("API Token cannot be null or empty")); } Result::Ok(api_token) @@ -113,7 +111,7 @@ fn prompt_hosted_instance() -> Result<String, Error> { // Make sure hosted instance is present. if hosted_instance.is_empty() { - return Result::Err(failure::err_msg("Hosted instance cannot be empty")); + return Result::Err(anyhow!("Hosted instance cannot be empty")); } Ok(hosted_instance) diff --git a/src/diffs.rs b/src/diffs.rs index d9db7c2..d3be6f1 100644 --- a/src/diffs.rs +++ b/src/diffs.rs @@ -1,9 +1,9 @@ use crate::structs::{FabConfig, Revision, RevisionData}; use crate::NO_BORDER_PRESET; use crate::{auth, users}; +use anyhow::{anyhow, Error}; use clap::ArgMatches; use comfy_table::{Attribute, Cell, CellAlignment, ContentArrangement, Table}; -use failure::Error; use serde_json::{Map, Value}; use tokio::runtime::Runtime; @@ -36,7 +36,7 @@ pub async fn get_authored_diffs(config: &FabConfig) -> Result<Vec<Revision>, Err /// Get diffs authored by given author pub async fn get_diffs(config: &FabConfig, author: &Option<&str>) -> Result<Vec<Revision>, Error> { if author.is_none() { - return Err(failure::err_msg("No author specified")); + return Err(anyhow!("No author specified")); } let author = author.unwrap(); diff --git a/src/main.rs b/src/main.rs index 0772078..404a24f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,9 @@ extern crate serde_json; use crate::preferences::Preferences; +use anyhow::{anyhow, Error}; use clap_generate::generate; use clap_generate::generators::{Bash, Elvish, Fish, PowerShell, Zsh}; -use failure::Error; use std::io; mod auth; mod cli; @@ -61,7 +61,7 @@ fn main() -> Result<(), Error> { "fab", &mut io::stdout(), ), - _ => return Err(failure::err_msg("No matching shell specified")), + _ => return Err(anyhow!("No matching shell specified")), } } Ok(()) @@ -71,7 +71,7 @@ fn main() -> Result<(), Error> { /// /// It's important to remember to add any default values otherwise confy will blow /// up with a BadTomlError -fn migrate_preferences(preferences: Preferences) -> Result<Preferences, failure::Error> { +fn migrate_preferences(preferences: Preferences) -> Result<Preferences, Error> { let preferences = Preferences { default_limit_str: preferences.default_limit.to_string(), default_limit: preferences.default_limit, diff --git a/src/preferences.rs b/src/preferences.rs index 349fb5a..9e79e22 100644 --- a/src/preferences.rs +++ b/src/preferences.rs @@ -1,8 +1,8 @@ +use anyhow::Error; use clap::ArgMatches; use console::style; use dialoguer::theme::ColorfulTheme; use dialoguer::{Checkboxes, Input, Select}; -use failure::Error; use serde::{Deserialize, Serialize}; use std::io; diff --git a/src/summary.rs b/src/summary.rs index 8cbaa24..2fd907e 100644 --- a/src/summary.rs +++ b/src/summary.rs @@ -2,9 +2,9 @@ use crate::diffs::{get_authored_diffs, get_needs_review_diffs, render_diffs}; use crate::preferences::Preferences; use crate::structs::FabConfig; use crate::tasks::{get_tasks, render_tasks, Priority}; +use anyhow::Error; use clap::ArgMatches; use console::style; -use failure::Error; use futures::future::join3; pub fn process_summary( diff --git a/src/tasks.rs b/src/tasks.rs index 2f9e471..a4393d9 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -1,9 +1,9 @@ use crate::preferences::Preferences; use crate::structs::FabConfig; use crate::{auth, NO_BORDER_PRESET}; +use anyhow::{anyhow, Error}; use clap::ArgMatches; use comfy_table::{Attribute, Cell, CellAlignment, Color, ContentArrangement, Table}; -use failure::Error; use serde::Deserialize; use serde_json::{Map, Value}; @@ -183,7 +183,7 @@ impl Priority { "normal" => Result::Ok(50), "low" => Result::Ok(25), "wishlist" => Result::Ok(0), - _ => Result::Err(failure::err_msg("Unknown value of priority")), + _ => Result::Err(anyhow!("Unknown value of priority")), } } } diff --git a/src/users.rs b/src/users.rs index 9744254..3aa6522 100644 --- a/src/users.rs +++ b/src/users.rs @@ -1,6 +1,6 @@ use crate::auth; use crate::structs::FabConfig; -use failure::Error; +use anyhow::Error; use serde::{Deserialize, Serialize}; use serde_json::{Map, Value};