diff --git a/Cargo.lock b/Cargo.lock index 861f2ef..02226d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1312,6 +1312,7 @@ dependencies = [ "glium", "num-traits", "serde", + "thiserror", "vulkano", ] diff --git a/obj-rs/Cargo.toml b/obj-rs/Cargo.toml index 0cb0401..84f72e0 100644 --- a/obj-rs/Cargo.toml +++ b/obj-rs/Cargo.toml @@ -32,6 +32,7 @@ glium-support = ["glium"] [dependencies] num-traits = "0.2.11" +thiserror = "1.0.16" # Optional serde support (on by default) serde = { version = "1.0", features = ["derive"], optional = true } diff --git a/obj-rs/src/error.rs b/obj-rs/src/error.rs index fedd193..a927db7 100644 --- a/obj-rs/src/error.rs +++ b/obj-rs/src/error.rs @@ -1,9 +1,9 @@ //! Contains helper structs for error handling -use std::error::Error; use std::fmt; use std::io; use std::num::{ParseFloatError, ParseIntError}; +use thiserror::Error; /// A type for results generated by `load_obj` and `load_mtl` where the `Err` type is hard-wired to /// `ObjError` @@ -13,55 +13,29 @@ use std::num::{ParseFloatError, ParseIntError}; pub type ObjResult = Result; /// The error type for loading of the `obj` file. -#[derive(Debug)] +#[derive(Error, Debug)] pub enum ObjError { /// IO error has been occurred during opening the `obj` file. - Io(io::Error), - /// Tried to parse integer frome the `obj` file, but failed. - ParseInt(ParseIntError), - /// Tried to parse floating point number frome the `obj` file, but failed. - ParseFloat(ParseFloatError), - /// `LoadError` has been occurred during parseing the `obj` file. - Load(LoadError), -} + // TODO + #[error(transparent)] + Io(#[from] io::Error), -macro_rules! implmnt { - ($name:ident, $error:path) => { - impl From<$error> for ObjError { - fn from(err: $error) -> Self { - ObjError::$name(err) - } - } - }; -} + /// Tried to parse integer frome the `obj` file, but failed. + // TODO + #[error(transparent)] + ParseInt(#[from] ParseIntError), -impl fmt::Display for ObjError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - ObjError::Io(ref e) => e.fmt(f), - ObjError::ParseInt(ref e) => e.fmt(f), - ObjError::ParseFloat(ref e) => e.fmt(f), - ObjError::Load(ref e) => e.fmt(f), - } - } -} + /// Tried to parse floating point number frome the `obj` file, but failed. + // TODO + #[error(transparent)] + ParseFloat(#[from] ParseFloatError), -impl Error for ObjError { - fn cause(&self) -> Option<&dyn Error> { - match *self { - ObjError::Io(ref err) => Some(err), - ObjError::ParseInt(ref err) => Some(err), - ObjError::ParseFloat(ref err) => Some(err), - ObjError::Load(ref err) => Some(err), - } - } + /// `LoadError` has been occurred during parseing the `obj` file. + // TODO + #[error(transparent)] + Load(#[from] LoadError), } -implmnt!(Io, io::Error); -implmnt!(ParseInt, ParseIntError); -implmnt!(ParseFloat, ParseFloatError); -implmnt!(Load, LoadError); - /// The error type for parse operations of the `Obj` struct. #[derive(PartialEq, Eq, Clone, Debug)] pub struct LoadError { @@ -113,7 +87,7 @@ impl LoadError { } } -impl Error for LoadError {} +impl std::error::Error for LoadError {} impl fmt::Display for LoadError { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {