From a9932ee6a19b3352091be176e8657df8ecc450db Mon Sep 17 00:00:00 2001 From: Tangent 128 Date: Fri, 13 Apr 2018 19:01:40 -0400 Subject: [PATCH] Create unified error type --- src/error.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 56 insertions(+) create mode 100644 src/error.rs diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..2073d53 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,55 @@ +use std::{ + error::Error, + fmt::{ + Display, + Formatter, + Result as FmtResult + }, + io::Error as IoError +}; + +use ebml::EbmlError; + +#[derive(Debug)] +pub enum WebmetroError { + EbmlError(EbmlError), + IoError(IoError), + Unknown(Box) +} + +impl Display for WebmetroError { + fn fmt(&self, f: &mut Formatter) -> FmtResult { + match self { + &WebmetroError::EbmlError(ref err) => err.fmt(f), + &WebmetroError::IoError(ref err) => err.fmt(f), + &WebmetroError::Unknown(ref err) => err.fmt(f), + } + } +} +impl Error for WebmetroError { + fn description(&self) -> &str { + match self { + &WebmetroError::EbmlError(ref err) => err.description(), + &WebmetroError::IoError(ref err) => err.description(), + &WebmetroError::Unknown(ref err) => err.description(), + } + } +} + +impl From for WebmetroError { + fn from(err: EbmlError) -> WebmetroError { + WebmetroError::EbmlError(err) + } +} + +impl From for WebmetroError { + fn from(err: IoError) -> WebmetroError { + WebmetroError::IoError(err) + } +} + +impl From> for WebmetroError { + fn from(err: Box) -> WebmetroError { + WebmetroError::Unknown(err) + } +} diff --git a/src/lib.rs b/src/lib.rs index 3e80f2b..1f064cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ extern crate futures; extern crate odds; pub mod ebml; +pub mod error; pub mod iterator; pub mod slice; pub mod stream_parser;