diff --git a/maraiah/cenum.rs b/maraiah/cenum.rs index d17dca4..120d624 100644 --- a/maraiah/cenum.rs +++ b/maraiah/cenum.rs @@ -57,7 +57,7 @@ macro_rules! c_enum { match n { $($va => Ok($t::$en),)+ - n => Err(Self::Error::new(n)) + n => Err(Self::Error::new(stringify!($t), n)) } } } diff --git a/maraiah/defl.rs b/maraiah/defl.rs index d13f2b1..cd5bc7c 100644 --- a/maraiah/defl.rs +++ b/maraiah/defl.rs @@ -413,7 +413,7 @@ impl HuffmanTable code <<= 1; } - Err(repr_error(code)) + Err(ReprError::new("DEFLATE code", code).into()) } } diff --git a/maraiah/err.rs b/maraiah/err.rs index 910dc7e..a6bc647 100644 --- a/maraiah/err.rs +++ b/maraiah/err.rs @@ -15,9 +15,13 @@ macro_rules! ok { macro_rules! flag_ok { ($t:ident$(::$tc:ident)*, $v:expr) => { - match $t$(::$tc)*::from_bits($v) { - Some(v) => Ok(v), - None => Err($crate::err::err_msg(concat!("bad ", stringify!($t)))), + { + let v = $v; + + match $t$(::$tc)*::from_bits(v) { + Some(v) => Ok(v), + None => Err($crate::err::ReprError::new(stringify!($t), v)), + } } }; } @@ -40,33 +44,22 @@ macro_rules! bail { /// ``` pub fn err_msg(msg: &'static str) -> Error {ErrMsg(msg).into()} -/// Returns an `Error` from a `ReprError`. -/// -/// # Examples -/// -/// ``` -/// use maraiah::err::repr_error; -/// -/// assert_eq!(format!("{}", repr_error(77)), -/// "representation error (got 77)"); -/// ``` -pub fn repr_error>(n: T) -> Error {ReprError::new(n).into()} - impl ReprError { - /// Creates a new `ReprError`. + /// Returns an `Error` with a message for representation errata. /// /// # Examples /// /// ``` - /// use maraiah::err::ReprError; + /// use maraiah::err::repr_error; /// - /// let err = ReprError::new(7); - /// - /// assert_eq!(format!("{}", err), "representation error (got 7)"); + /// assert_eq!(format!("{}", repr_error("TypeName", 77)), + /// "bad TypeName (77)"); /// ``` - #[inline] - pub fn new>(n: T) -> Self {Self(n.into())} + pub fn new>(t: &'static str, n: T) -> Self + { + Self(t, n.into()) + } } impl Fail for ReprError {} @@ -76,7 +69,7 @@ impl fmt::Display for ReprError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "representation error (got {})", self.0) + write!(f, "bad {} ({})", self.0, self.1) } } @@ -90,7 +83,7 @@ impl fmt::Display for ErrMsg /// A representation error for an integer. #[derive(Debug, Eq, Ord, PartialEq, PartialOrd)] -pub struct ReprError(i64); +pub struct ReprError(&'static str, i64); #[derive(Debug)] struct ErrMsg(&'static str); diff --git a/maraiah/map/poly.rs b/maraiah/map/poly.rs index c928537..f543dd3 100644 --- a/maraiah/map/poly.rs +++ b/maraiah/map/poly.rs @@ -94,7 +94,7 @@ impl PolygonType 21 => Ok(PolygonType::Glue), 22 => Ok(PolygonType::GlueTrigger(pdata)), 23 => Ok(PolygonType::GlueSuper), - n => Err(ReprError::new(n)), + n => Err(ReprError::new("PolygonType", n)), } } @@ -118,7 +118,7 @@ impl PolygonType 13 => Ok(PolygonType::GlueSuper), 14 => Ok(PolygonType::MustExplore), 15 => Ok(PolygonType::AutoExit), - n => Err(ReprError::new(n)), + n => Err(ReprError::new("PolygonType", n)), } } } diff --git a/maraiah/map/trmg.rs b/maraiah/map/trmg.rs index 8289feb..1486a40 100644 --- a/maraiah/map/trmg.rs +++ b/maraiah/map/trmg.rs @@ -35,7 +35,7 @@ pub fn read(b: &[u8]) -> ResultS<(InterGroup, usize)> 14 => GroupType::Camera(pdata), 15 => GroupType::Static(pdata), 16 => GroupType::Tag(pdata), - n => return Err(ReprError::new(n).into()), + n => return Err(ReprError::new("GroupType", n).into()), }; Ok((InterGroup{flags, ttype, lines, beg, len}, 12))