diff --git a/Cargo.lock b/Cargo.lock index dfa33a9..0e375fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -242,6 +242,7 @@ dependencies = [ "clap_complete", "serde", "serde_yml", + "thiserror", ] [[package]] @@ -261,6 +262,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "unicode-ident" version = "1.0.18" diff --git a/Cargo.toml b/Cargo.toml index 88e1723..9bcae47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ clap = { version = "4.5.38", features = ["derive"] } clap_complete = "*" serde = { version = "1.0.219", features = ["derive"] } serde_yml = "0.0.12" +thiserror = "2.0.12" [build-dependencies] clap = "*" diff --git a/src/error.rs b/src/error.rs index eecaba5..899981f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,19 +1,10 @@ -use std::fmt; +use std::{io, path::PathBuf}; +use thiserror::Error; -pub struct Error { - pub message: String, -} - -// Implement std::fmt::Display for Error -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.message) // user-facing output - } -} - -// Implement std::fmt::Debug for Error -impl fmt::Debug for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Error message: {}\n{{ file: {}, line: {} }}", self.message, file!(), line!()) // programmer-facing output - } +#[derive(Error, Debug)] +pub enum Error { + #[error("DeserializationError: failed to deserialize {0}\n{1}")] + DeserializationError(PathBuf, serde_yml::Error), + #[error("IoError: {0}")] + IoError(#[from] io::Error), } diff --git a/src/main.rs b/src/main.rs index 871c4b5..920d0fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,11 +28,9 @@ fn main() { config = conf; } Err(e) => { - println!("{}", e); - exit(1); + panic!("{}", e); } } - // = get_config_from_path(config_path); println!("{:?}", config); } @@ -42,19 +40,12 @@ fn get_config_from_path(path: PathBuf) -> Result { Ok(f) => match serde_yml::from_reader(f) { Ok(conf) => return Ok(conf), Err(e) => { - return Err(Error { - message: format!( - "*** Error deserializing config file ({:?}), it's likely deformed ***\n{}", - path, e - ), - }); + return Err(Error::DeserializationError(path, e)); } }, Err(e) => { println!(""); - return Err(Error { - message: format!("*** Error opening config file ({:?}) ***\n{}", path, e), - }); + return Err(Error::IoError(e)); } } }