diff --git a/Cargo.lock b/Cargo.lock index ed35ba7..ea74146 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,71 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "anyhow" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "080e9890a082662b09c1ad45f567faeeb47f22b5fb23895fbe1e651e718e25ca" + +[[package]] +name = "proc-macro2" +version = "1.0.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +dependencies = [ + "proc-macro2", +] + [[package]] name = "roxy_core" version = "0.1.0" +dependencies = [ + "anyhow", + "thiserror", +] + +[[package]] +name = "syn" +version = "2.0.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "915aea9e586f80826ee59f8453c1101f9d1c4b3964cd2460185ee8e299ada496" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e45bcbe8ed29775f228095caf2cd67af7a4ccf756ebff23a306bf3e8b47b24b" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/Cargo.toml b/Cargo.toml index 5f33b36..cad1086 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,6 @@ name = "roxy_core" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] +anyhow = "1.0.79" +thiserror = "1.0.57" diff --git a/src/error.rs b/src/error.rs index cd4daac..3c3229e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,94 +1,33 @@ -use std::{ - fmt::Display, - io::{Error as IOError, ErrorKind}, - path::StripPrefixError, - string::FromUtf8Error, -}; +use std::fmt::Display; -#[derive(Debug)] +#[derive(thiserror::Error, Debug)] pub struct Error { message: String, - source: Option>, -} - -impl Error { - pub fn new(message: String, source: E) -> Self { - Self { - message, - source: Some(Box::new(source)), - } - } - - fn wrap(value: E) -> Option> { - Some(Box::new(value)) - } + #[source] + source: anyhow::Error, } impl Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", &self.message)?; - - if let Some(source) = &self.source { - write!(f, " ({})", &source)?; - } - - Ok(()) + write!(f, "error: {} ", self.message).and_then(|_| self.source.fmt(f)) } } -impl std::error::Error for Error { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - self.source.as_deref() - } -} - -impl From for Error { - fn from(value: String) -> Self { - Self { - message: value, - source: None, - } - } -} - -impl From for Error { - fn from(value: StripPrefixError) -> Self { +impl From for Error { + fn from(value: anyhow::Error) -> Self { Self { message: value.to_string(), - source: Self::wrap(value), + source: value, } } } -impl From for Error { - fn from(value: IOError) -> Self { - Self { +impl From for Error { + fn from(value: std::io::Error) -> Self { + Self{ message: value.to_string(), - source: Self::wrap(value), + source: anyhow::Error::from(value) } } } -impl From for IOError { - fn from(value: Error) -> Self { - IOError::new(ErrorKind::Other, value.message) - } -} - -impl From for Error { - fn from(value: FromUtf8Error) -> Self { - Self { - message: value.to_string(), - source: Self::wrap(value), - } - } -} - -impl From<&'static dyn std::error::Error> for Error { - fn from(value: &'static dyn std::error::Error) -> Self { - Self { - message: value.to_string(), - source: Self::wrap(value), - } - } -} diff --git a/src/lib.rs b/src/lib.rs index 6b4d9bd..de998ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ pub mod roxy; pub mod error; +pub mod result; diff --git a/src/result.rs b/src/result.rs new file mode 100644 index 0000000..605dc25 --- /dev/null +++ b/src/result.rs @@ -0,0 +1 @@ +pub type Result = std::result::Result; diff --git a/src/roxy.rs b/src/roxy.rs index e9a9dea..ddb134f 100644 --- a/src/roxy.rs +++ b/src/roxy.rs @@ -108,7 +108,7 @@ impl Roxy { fn read_asset( asset: &mut Asset>, parser: &mut Parser, - ) -> Result, Error> { + ) -> crate::result::Result> { let mut src = Vec::new(); let mut dst = Vec::new(); @@ -141,7 +141,7 @@ impl Roxy { match path.with_extension("").file_name() { Some(name) if name == "index" => Ok(path.to_path_buf()), Some(_) => Ok(path.with_extension("").join("index.html")), - None => Err(Error::from("invalid path \"{path:?}\"".to_string())), + None => Err(anyhow::Error::msg("invalid path \"{path:?}\"").into()), } }