Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
simnalamburt committed Sep 28, 2024
1 parent a8bc628 commit 0cd6aac
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 44 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions obj-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
62 changes: 18 additions & 44 deletions obj-rs/src/error.rs
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -13,55 +13,29 @@ use std::num::{ParseFloatError, ParseIntError};
pub type ObjResult<T> = Result<T, ObjError>;

/// 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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0cd6aac

Please sign in to comment.