From 7334ff6bd52be2b7a7360aca4873569d9a45f172 Mon Sep 17 00:00:00 2001 From: Marrub Date: Mon, 4 Mar 2019 08:59:43 -0500 Subject: [PATCH] errors --- Cargo.toml | 2 +- source/durandal/cenum.rs | 8 ++++---- source/durandal/err.rs | 27 +++++++++------------------ 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ce934b8..b4b96a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ members = ["source/leela", "source/tycho"] [dependencies] bitflags = "1.0" -failure = "0.1" +failure = {version = "0.1", features = ["std"]} serde = {version = "1.0", features = ["derive"]} [profile.dev] diff --git a/source/durandal/cenum.rs b/source/durandal/cenum.rs index 0595cc2..001c2da 100644 --- a/source/durandal/cenum.rs +++ b/source/durandal/cenum.rs @@ -32,7 +32,7 @@ macro_rules! c_enum { match n { $($value => Ok($E::$Enum),)+ - n => Err(ReprError(n.into())) + n => Err(ReprError::new(n)) } } } @@ -60,9 +60,9 @@ mod test assert_eq!(TestEnum::from_repr(0), Ok(TestEnum::Zero)); assert_eq!(TestEnum::from_repr(1), Ok(TestEnum::One)); assert_eq!(TestEnum::from_repr(2), Ok(TestEnum::Two)); - assert_eq!(TestEnum::from_repr(3), Err(ReprError(3))); - assert_eq!(TestEnum::from_repr(4), Err(ReprError(4))); - assert_eq!(TestEnum::from_repr(5), Err(ReprError(5))); + assert_eq!(TestEnum::from_repr(3), Err(ReprError::new(3))); + assert_eq!(TestEnum::from_repr(4), Err(ReprError::new(4))); + assert_eq!(TestEnum::from_repr(5), Err(ReprError::new(5))); } #[test] diff --git a/source/durandal/err.rs b/source/durandal/err.rs index 3dd8d91..5159b16 100644 --- a/source/durandal/err.rs +++ b/source/durandal/err.rs @@ -31,6 +31,12 @@ macro_rules! bail { /// Returns an `Error` with a static string. pub fn err_msg(msg: &'static str) -> Error {Error::from(ErrMsg(msg))} +impl ReprError +{ + #[inline] + pub fn new(n: T) -> Self where T: Into {Self(n.into())} +} + impl Fail for ReprError {} impl Fail for ErrMsg {} @@ -42,14 +48,6 @@ impl fmt::Display for ReprError } } -impl fmt::Debug for ReprError -{ - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result - { - fmt::Display::fmt(self, f) - } -} - impl fmt::Display for ErrMsg { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result @@ -58,18 +56,11 @@ impl fmt::Display for ErrMsg } } -impl fmt::Debug for ErrMsg -{ - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result - { - fmt::Display::fmt(self, f) - } -} - /// A representation error for an integer. -#[derive(PartialEq)] -pub struct ReprError(pub i64); +#[derive(Debug, PartialEq)] +pub struct ReprError(i64); +#[derive(Debug)] struct ErrMsg(&'static str); /// A generic `failure` based `Result` type.