Skip to content

Commit

Permalink
refactor: remove unnecessary thiserror (#21)
Browse files Browse the repository at this point in the history
* remove unnecessary thiserror

* remove dependency
  • Loading branch information
shixinhuang99 committed Nov 23, 2023
1 parent 8b7a5a2 commit 3ef67df
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 58 deletions.
21 changes: 0 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
15 changes: 0 additions & 15 deletions src/error.rs

This file was deleted.

14 changes: 9 additions & 5 deletions src/github_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use ureq::Agent;

use crate::{
debug,
error::ScafalraError,
repository::Repository,
utils::{build_proxy_agent, get_self_version},
};
Expand Down Expand Up @@ -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<T> = self
Expand All @@ -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"))
Expand Down
18 changes: 10 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod cli;
mod colorize;
mod config;
mod debug;
mod error;
mod github_api;
mod repository;
mod scafalra;
Expand All @@ -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() {
Expand All @@ -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();

Expand Down
9 changes: 4 additions & 5 deletions src/repository.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::sync::OnceLock;

use anyhow::{anyhow, Result};
use anyhow::Result;
use camino::{Utf8Path, Utf8PathBuf};
use fs_err as fs;
use regex::Regex;
use remove_dir_all::remove_dir_all;

use crate::{
debug,
error::ScafalraError,
utils::{download, tar_unpack},
};

Expand Down Expand Up @@ -36,9 +35,9 @@ pub enum Query {

impl Repository {
pub fn parse(input: &str) -> Result<Self> {
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();
Expand Down
11 changes: 8 additions & 3 deletions src/scafalra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use crate::{
},
config::Config,
debug,
error::ScafalraError,
github_api::GitHubApi,
repository::Repository,
store::{Scaffold, Store},
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 3ef67df

Please sign in to comment.