switch from thiserror to color-eyre

This commit is contained in:
Moritz Hölting 2024-04-16 16:02:50 +02:00
parent 82d085b11e
commit cfb8eac3f9
8 changed files with 25 additions and 34 deletions

View File

@ -21,4 +21,5 @@ shulkerscript-lang = {path = "../shulkerscript-lang", features = ["shulkerbox"],
shulkerbox = {path = "../shulkerbox", default-features = false}
git2 = { version = "0.18.3", default-features = false }
path-absolutize = "3.1.1"
color-eyre = "0.6.3"

View File

@ -1,8 +1,6 @@
use crate::{
error::Result,
subcommands::{self, BuildArgs, InitArgs},
};
use crate::subcommands::{self, BuildArgs, InitArgs};
use clap::{Parser, Subcommand};
use color_eyre::eyre::Result;
#[derive(Debug, Parser)]
#[command(version, about, long_about = None)]

View File

@ -2,12 +2,6 @@ use std::path::PathBuf;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("An error occurred while parsing command-line arguments.")]
IoError(#[from] std::io::Error),
#[error("An error occured while serializing to TOML.")]
TomlSerializeError(#[from] toml::ser::Error),
#[error("An error occured while deserializing from TOML.")]
TomlDeserializeError(#[from] toml::de::Error),
#[error("No file/directory found at path {0}.")]
PathNotFoundError(PathBuf),
#[error("An error occured because the directory {0} is not empty.")]
@ -16,10 +10,6 @@ pub enum Error {
NotDirectoryError(PathBuf),
#[error("An error occured because the path is neither a pack directory or a pack.toml file.")]
InvalidPackPathError(PathBuf),
#[error("An error occured while compiling the project.")]
ShulkerScriptError(#[from] shulkerscript_lang::base::Error),
#[error("An error occured during a git action.")]
GitError(#[from] git2::Error),
}
pub type Result<T> = std::result::Result<T, Error>;

View File

@ -1,13 +1,12 @@
use std::process::ExitCode;
use clap::Parser;
use color_eyre::eyre::Result;
use shulkerscript::cli::Args;
fn main() -> ExitCode {
fn main() -> Result<()> {
color_eyre::install()?;
let args = Args::parse();
match args.run() {
Ok(_) => ExitCode::SUCCESS,
Err(_) => ExitCode::FAILURE,
}
args.run()?;
Ok(())
}

View File

@ -1,8 +1,9 @@
use color_eyre::eyre::Result;
use path_absolutize::Absolutize;
use crate::{
config::ProjectConfig,
error::{Error, Result},
error::Error,
terminal_output::{print_error, print_info},
};
use std::{fs, path::PathBuf};
@ -24,12 +25,12 @@ pub fn build(_verbose: bool, args: &BuildArgs) -> Result<()> {
let toml_path = if !path.exists() {
print_error("The specified path does not exist.");
return Err(Error::PathNotFoundError(path.to_path_buf()));
return Err(Error::PathNotFoundError(path.to_path_buf()))?;
} else if path.is_dir() {
let toml_path = path.join("pack.toml");
if !toml_path.exists() {
print_error("The specified directory does not contain a pack.toml file.");
return Err(Error::InvalidPackPathError(path.to_path_buf()));
Err(Error::InvalidPackPathError(path.to_path_buf()))?;
}
toml_path
} else if path.is_file()
@ -41,7 +42,7 @@ pub fn build(_verbose: bool, args: &BuildArgs) -> Result<()> {
path.to_path_buf()
} else {
print_error("The specified path is neither a directory nor a pack.toml file.");
return Err(Error::InvalidPackPathError(path.to_path_buf()));
return Err(Error::InvalidPackPathError(path.to_path_buf()))?;
};
let toml_content = fs::read_to_string(&toml_path)?;

View File

@ -4,6 +4,7 @@ use std::{
};
use clap::ValueEnum;
use color_eyre::eyre::Result;
use git2::{
IndexAddOption as GitIndexAddOption, Repository as GitRepository, Signature as GitSignature,
};
@ -11,7 +12,7 @@ use path_absolutize::Absolutize;
use crate::{
config::ProjectConfig,
error::{Error, Result},
error::Error,
terminal_output::{print_error, print_info, print_success},
};
@ -52,13 +53,13 @@ pub fn init(verbose: bool, args: &InitArgs) -> Result<()> {
if !path.exists() {
print_error("The specified path does not exist.");
Err(Error::PathNotFoundError(path.to_path_buf()))
Err(Error::PathNotFoundError(path.to_path_buf()))?
} else if !path.is_dir() {
print_error("The specified path is not a directory.");
Err(Error::NotDirectoryError(path.to_path_buf()))
Err(Error::NotDirectoryError(path.to_path_buf()))?
} else if !force && path.read_dir()?.next().is_some() {
print_error("The specified directory is not empty.");
Err(Error::NonEmptyDirectoryError(path.to_path_buf()))
Err(Error::NonEmptyDirectoryError(path.to_path_buf()))?
} else {
let name = args
.name

View File

@ -1,6 +1,6 @@
use clap::ValueEnum;
use crate::error::Result;
use color_eyre::eyre::Result;
use std::path::PathBuf;
#[derive(Debug, clap::Args, Clone)]

View File

@ -1,10 +1,11 @@
use std::fs;
use color_eyre::eyre::Result;
use path_absolutize::Absolutize;
use crate::{
config::ProjectConfig,
error::{Error, Result},
error::Error,
terminal_output::{print_error, print_info},
};
@ -26,12 +27,12 @@ pub fn package(_verbose: bool, args: &PackageArgs) -> Result<()> {
let toml_path = if !path.exists() {
print_error("The specified path does not exist.");
return Err(Error::PathNotFoundError(path.to_path_buf()));
return Err(Error::PathNotFoundError(path.to_path_buf()))?;
} else if path.is_dir() {
let toml_path = path.join("pack.toml");
if !toml_path.exists() {
print_error("The specified directory does not contain a pack.toml file.");
return Err(Error::InvalidPackPathError(path.to_path_buf()));
Err(Error::InvalidPackPathError(path.to_path_buf()))?;
}
toml_path
} else if path.is_file()
@ -43,7 +44,7 @@ pub fn package(_verbose: bool, args: &PackageArgs) -> Result<()> {
path.to_path_buf()
} else {
print_error("The specified path is neither a directory nor a pack.toml file.");
return Err(Error::InvalidPackPathError(path.to_path_buf()));
return Err(Error::InvalidPackPathError(path.to_path_buf()))?;
};
let toml_content = fs::read_to_string(&toml_path)?;