From c26b11e924875e7c93daf5bd9dfb4b2bac97c66a Mon Sep 17 00:00:00 2001 From: ShiXin Huang Date: Tue, 21 Nov 2023 23:40:33 +0800 Subject: [PATCH] refactor: remove unnecessary thiserror (#21) * remove unnecessary thiserror * remove dependency --- Cargo.lock | 21 --------------------- Cargo.toml | 1 - src/error.rs | 15 --------------- src/github_api/mod.rs | 14 +++++++++----- src/main.rs | 18 ++++++++++-------- src/repository.rs | 9 ++++----- src/scafalra.rs | 11 ++++++++--- 7 files changed, 31 insertions(+), 58 deletions(-) delete mode 100644 src/error.rs diff --git a/Cargo.lock b/Cargo.lock index bc60de4..25a79ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1386,7 +1386,6 @@ dependencies = [ "tar", "tempfile", "term_grid", - "thiserror", "toml", "ureq", "zip", @@ -1654,26 +1653,6 @@ dependencies = [ "unicode-width", ] -[[package]] -name = "thiserror" -version = "1.0.49" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1177e8c6d7ede7afde3585fd2513e611227efd6481bd78d2e82ba1ce16557ed4" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "1.0.49" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10712f02019e9288794769fba95cd6847df9874d49d871d062172f9dd41bc4cc" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.37", -] - [[package]] name = "time" version = "0.3.30" diff --git a/Cargo.toml b/Cargo.toml index 64c88d6..968e799 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,6 @@ serde = { version = "1.0.158", features = ["derive"] } tabled = { version = "0.14.0", features = ["color"] } tar = "0.4.38" term_grid = "0.2.0" -thiserror = "1.0.49" toml = "0.8.0" ureq = { version = "2.6.2", features = ["json"] } diff --git a/src/error.rs b/src/error.rs deleted file mode 100644 index d5b5283..0000000 --- a/src/error.rs +++ /dev/null @@ -1,15 +0,0 @@ -use std::path::PathBuf; - -use thiserror::Error; - -#[derive(Error, Debug)] -pub enum ScafalraError { - #[error("No GitHub personal access token configured")] - NoToken, - #[error("Call to GitHub api error: {}", .0)] - GitHubApiError(String), - #[error("Could not parse the input: `{}`", .0)] - RepositoryParseError(String), - #[error("`{}` it is not valid UTF-8 path", .0.display())] - NonUtf8Path(PathBuf), -} diff --git a/src/github_api/mod.rs b/src/github_api/mod.rs index d7d130f..8767859 100644 --- a/src/github_api/mod.rs +++ b/src/github_api/mod.rs @@ -15,7 +15,6 @@ use ureq::Agent; use crate::{ debug, - error::ScafalraError, repository::Repository, utils::{build_proxy_agent, get_self_version}, }; @@ -50,7 +49,7 @@ impl GitHubApi { T: DeserializeOwned + std::fmt::Debug, { let Some(ref token) = *self.token.borrow() else { - anyhow::bail!(ScafalraError::NoToken); + anyhow::bail!("No GitHub personal access token configured"); }; let response: GraphQLResponse = self @@ -67,9 +66,14 @@ impl GitHubApi { let GraphQLResponse { data, errors } = response; if let Some(errors) = errors { - anyhow::bail!(ScafalraError::GitHubApiError( - errors[0].message.clone() - )); + if errors.is_empty() { + anyhow::bail!("Call to GitHub api error"); + } else { + anyhow::bail!( + "Call to GitHub api error: {}", + errors[0].message + ); + } } data.ok_or(anyhow::anyhow!("No response data")) diff --git a/src/main.rs b/src/main.rs index 6cc6dc2..20e0647 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,6 @@ mod cli; mod colorize; mod config; mod debug; -mod error; mod github_api; mod repository; mod scafalra; @@ -17,7 +16,6 @@ use camino::Utf8PathBuf; use clap::Parser; use cli::{Cli, Command}; use debug::trun_on_debug; -use error::ScafalraError; use scafalra::Scafalra; fn main() { @@ -27,12 +25,16 @@ fn main() { } fn try_main() -> Result<()> { - let Some(home_dir) = home::home_dir() else { - anyhow::bail!("Impossible to get your home dir"); - }; - - let home_dir = Utf8PathBuf::from_path_buf(home_dir) - .map_err(ScafalraError::NonUtf8Path)?; + let home_dir = Utf8PathBuf::from_path_buf( + home::home_dir() + .ok_or(anyhow::anyhow!("Impossible to get your home directory"))?, + ) + .map_err(|err_path| { + anyhow::anyhow!( + "Home directory `{}` it is not valid UTF-8 path", + err_path.display() + ) + })?; let cli = Cli::parse(); diff --git a/src/repository.rs b/src/repository.rs index 9f69dd8..e8f3a8a 100644 --- a/src/repository.rs +++ b/src/repository.rs @@ -1,6 +1,6 @@ use std::sync::OnceLock; -use anyhow::{anyhow, Result}; +use anyhow::Result; use camino::{Utf8Path, Utf8PathBuf}; use fs_err as fs; use regex::Regex; @@ -8,7 +8,6 @@ use remove_dir_all::remove_dir_all; use crate::{ debug, - error::ScafalraError, utils::{download, tar_unpack}, }; @@ -36,9 +35,9 @@ pub enum Query { impl Repository { pub fn parse(input: &str) -> Result { - let caps = get_repo_re().captures(input).ok_or(anyhow!( - ScafalraError::RepositoryParseError(input.to_string()) - ))?; + let caps = get_repo_re() + .captures(input) + .ok_or(anyhow::anyhow!("Could not parse the input: `{}`", input))?; let owner = caps[1].to_string(); let name = caps[2].to_string(); diff --git a/src/scafalra.rs b/src/scafalra.rs index c0f4b30..cd25f7f 100644 --- a/src/scafalra.rs +++ b/src/scafalra.rs @@ -15,7 +15,6 @@ use crate::{ }, config::Config, debug, - error::ScafalraError, github_api::GitHubApi, repository::Repository, store::{Scaffold, Store}, @@ -166,8 +165,14 @@ impl Scafalra { anyhow::bail!("No such scaffold `{}`", args.name); }; - let cwd = Utf8PathBuf::from_path_buf(env::current_dir()?) - .map_err(ScafalraError::NonUtf8Path)?; + let cwd = Utf8PathBuf::from_path_buf(env::current_dir()?).map_err( + |err_path| { + anyhow::anyhow!( + "Current working directory `{}` it is not valid UTF-8 path", + err_path.display() + ) + }, + )?; debug!("current directory: {}", cwd);