From cfb8eac3f96c5d1ee369f02fa9366dbc595be137 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20H=C3=B6lting?= <87192362+moritz-hoelting@users.noreply.github.com> Date: Tue, 16 Apr 2024 16:02:50 +0200 Subject: [PATCH] switch from thiserror to color-eyre --- Cargo.toml | 1 + src/cli.rs | 6 ++---- src/error.rs | 10 ---------- src/main.rs | 13 ++++++------- src/subcommands/build.rs | 9 +++++---- src/subcommands/init.rs | 9 +++++---- src/subcommands/lang_debug.rs | 2 +- src/subcommands/package.rs | 9 +++++---- 8 files changed, 25 insertions(+), 34 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 00885ca..f2b17b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/cli.rs b/src/cli.rs index d3fd6b9..ffe35b2 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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)] diff --git a/src/error.rs b/src/error.rs index 96d6289..57b319c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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 = std::result::Result; diff --git a/src/main.rs b/src/main.rs index f32b514..d67dfa4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(()) } diff --git a/src/subcommands/build.rs b/src/subcommands/build.rs index 805d1e7..e318b22 100644 --- a/src/subcommands/build.rs +++ b/src/subcommands/build.rs @@ -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)?; diff --git a/src/subcommands/init.rs b/src/subcommands/init.rs index 2c61473..9a6e783 100644 --- a/src/subcommands/init.rs +++ b/src/subcommands/init.rs @@ -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 diff --git a/src/subcommands/lang_debug.rs b/src/subcommands/lang_debug.rs index 06f37df..9475708 100644 --- a/src/subcommands/lang_debug.rs +++ b/src/subcommands/lang_debug.rs @@ -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)] diff --git a/src/subcommands/package.rs b/src/subcommands/package.rs index d9c48f3..203ad54 100644 --- a/src/subcommands/package.rs +++ b/src/subcommands/package.rs @@ -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)?;