obj-rs 0.4.7

Wavefront obj parser for Rust. It handles both 'obj' and 'mtl' formats.
//! Contains helper structs for error handling

use std::result;
use std::io::Error;
use std::num::{ParseIntError, ParseFloatError};
use std::convert::From;

/// A type for results generated by `load_obj` and `load_mtl` where the `Err` type is hard-wired to
/// `ObjError`
///
/// This typedef is generally used to avoid writing out `ObjError` directly and is otherwise a
/// direct mapping to `std::result::Result`.
pub type ObjResult<T> = result::Result<T, ObjError>;

#[derive(Debug)]
enum ObjError {
    Io(Error),
    ParseInt(ParseIntError),
    ParseFloat(ParseFloatError),
    Load(LoadError)
}

impl From<Error> for ObjError {
    fn from(err: Error) -> Self {
        ObjError::Io(err)
    }
}

impl From<ParseIntError> for ObjError {
    fn from(err: ParseIntError) -> Self {
        ObjError::ParseInt(err)
    }
}

impl From<ParseFloatError> for ObjError {
    fn from(err: ParseFloatError) -> Self {
        ObjError::ParseFloat(err)
    }
}

impl From<LoadError> for ObjError {
    fn from(err: LoadError) -> Self {
        ObjError::Load(err)
    }
}

/// The error type for parse operations and loading of the `Obj` struct.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct LoadError {
    kind: LoadErrorKind,
    desc: &'static str,
}

/// A list specifying general categories of load error.
#[derive(Copy, PartialEq, Eq, Clone, Debug)]
pub enum LoadErrorKind {
    /// Met unexpected statement.
    UnexpectedStatement,
    /// Received wrong number of arguments.
    WrongNumberOfArguments,
    /// Received unexpected type of arguments.
    WrongTypeOfArguments,
    /// Model should be triangulated first to be loaded properly.
    UntriangulatedModel,
    /// Model cannot be transformed into requested form.
    InsufficientData,
}

impl LoadError {
    /// Creates a new custom error from a specified kind/description.
    pub fn new(kind: LoadErrorKind, desc: &'static str) -> Self {
        LoadError { kind: kind, desc: desc }
    }
}

macro_rules! error {
    ($kind:ident, $desc:expr) => {
        return Err(
            ::std::convert::From::from(
                $crate::error::LoadError::new(
                    $crate::error::LoadErrorKind::$kind, $desc
                )
            )
        )
    }
}