maraiah: give better error messages on ReprErrors

master
an 2019-07-01 01:21:27 -04:00
parent 8f1b043b93
commit 14d9850653
5 changed files with 22 additions and 29 deletions

View File

@ -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))
}
}
}

View File

@ -413,7 +413,7 @@ impl HuffmanTable
code <<= 1;
}
Err(repr_error(code))
Err(ReprError::new("DEFLATE code", code).into())
}
}

View File

@ -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<T: Into<i64>>(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<T: Into<i64>>(n: T) -> Self {Self(n.into())}
pub fn new<T: Into<i64>>(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);

View File

@ -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)),
}
}
}

View File

@ -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))