use std::fmt;
use std::io::Error;
use std::num::{ParseIntError, ParseFloatError};
use std::convert::From;
pub type ObjResult<T> = Result<T, ObjError>;
#[derive(Debug)]
pub enum ObjError {
Io(Error),
ParseInt(ParseIntError),
ParseFloat(ParseFloatError),
Load(LoadError)
}
macro_rules! implmnt {
($name:ident, $error:ident) => (
impl From<$error> for ObjError {
fn from(err: $error) -> Self {
ObjError::$name(err)
}
}
)
}
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),
}
}
}
implmnt!(Io, Error);
implmnt!(ParseInt, ParseIntError);
implmnt!(ParseFloat, ParseFloatError);
implmnt!(Load, LoadError);
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct LoadError {
kind: LoadErrorKind,
desc: &'static str,
}
#[derive(Copy, PartialEq, Eq, Clone, Debug)]
pub enum LoadErrorKind {
UnexpectedStatement,
WrongNumberOfArguments,
WrongTypeOfArguments,
UntriangulatedModel,
InsufficientData,
}
impl LoadError {
pub fn new(kind: LoadErrorKind, desc: &'static str) -> Self {
LoadError { kind: kind, desc: desc }
}
}
impl fmt::Display for LoadError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let basic = match self.kind {
LoadErrorKind::UnexpectedStatement => "Met unexpected statement. ",
LoadErrorKind::WrongNumberOfArguments => "Received wrong number of arguments. ",
LoadErrorKind::WrongTypeOfArguments => "Received unexpected type of arguments. ",
LoadErrorKind::UntriangulatedModel => "Model should be triangulated first to be loaded properly. ",
LoadErrorKind::InsufficientData => "Model cannot be transformed into requested form. ",
};
write!(fmt, "{} {}", basic, self.desc)
}
}
macro_rules! error {
($kind:ident, $desc:expr) => {
return Err(
::std::convert::From::from(
$crate::error::LoadError::new(
$crate::error::LoadErrorKind::$kind, $desc
)
)
)
}
}