From 44e2f153dcf3eb611d01e5f1acb06b035d52a283 Mon Sep 17 00:00:00 2001 From: Matt Bilker Date: Mon, 9 Jul 2018 09:26:36 -0400 Subject: [PATCH] error: take Error type from ser/error.rs --- src/error.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++-- src/ser/error.rs | 59 ------------------------------------------------ src/ser/mod.rs | 11 ++------- 3 files changed, 58 insertions(+), 70 deletions(-) delete mode 100644 src/ser/error.rs diff --git a/src/error.rs b/src/error.rs index 25f776b..0f3c0f9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,8 +1,10 @@ -use std::fmt; +use std::error::Error as StdError; +use std::fmt::{self, Display}; use std::result::Result as StdResult; use std::string::FromUtf8Error; -use failure::{Backtrace, Context, Fail}; +use failure::{Backtrace, Compat, Context, Fail}; +use serde::{de, ser}; use node_types::KbinType; @@ -144,3 +146,55 @@ impl From for KbinError { inner.context(KbinErrorKind::Utf8).into() } } + +#[derive(Clone, Debug)] +pub enum Error { + Message(String), + + Wrapped(Compat), +} + +impl ser::Error for Error { + fn custom(msg: T) -> Self { + Error::Message(msg.to_string()) + } +} + +impl de::Error for Error { + fn custom(msg: T) -> Self { + Error::Message(msg.to_string()) + } +} + +impl Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str(StdError::description(self)) + } +} + +impl StdError for Error { + fn description(&self) -> &str { + match *self { + Error::Message(ref msg) => msg, + Error::Wrapped(ref err) => err.description(), + } + } +} + +impl From for Error { + fn from(inner: KbinError) -> Self { + Error::Wrapped(inner.compat()) + } +} + +impl From for Error { + fn from(inner: KbinErrorKind) -> Self { + Error::Wrapped(KbinError::from(inner).compat()) + } +} + +impl From> for Error { + fn from(inner: Context) -> Self { + Error::Wrapped(KbinError::from(inner).compat()) + } +} diff --git a/src/ser/error.rs b/src/ser/error.rs deleted file mode 100644 index 0bc7cbf..0000000 --- a/src/ser/error.rs +++ /dev/null @@ -1,59 +0,0 @@ -use std::error::Error as StdError; -use std::fmt::{self, Display}; - -use failure::{Compat, Context, Fail}; -use serde::{de, ser}; - -use error::{KbinError, KbinErrorKind}; - -#[derive(Clone, Debug)] -pub enum Error { - Message(String), - - Wrapped(Compat), -} - -impl ser::Error for Error { - fn custom(msg: T) -> Self { - Error::Message(msg.to_string()) - } -} - -impl de::Error for Error { - fn custom(msg: T) -> Self { - Error::Message(msg.to_string()) - } -} - -impl Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_str(StdError::description(self)) - } -} - -impl StdError for Error { - fn description(&self) -> &str { - match *self { - Error::Message(ref msg) => msg, - Error::Wrapped(ref err) => err.description(), - } - } -} - -impl From for Error { - fn from(inner: KbinError) -> Self { - Error::Wrapped(inner.compat()) - } -} - -impl From for Error { - fn from(inner: KbinErrorKind) -> Self { - Error::Wrapped(KbinError::from(inner).compat()) - } -} - -impl From> for Error { - fn from(inner: Context) -> Self { - Error::Wrapped(KbinError::from(inner).compat()) - } -} diff --git a/src/ser/mod.rs b/src/ser/mod.rs index 7071f41..61c3eb5 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -8,22 +8,15 @@ use serde::ser::{self, Impossible, Serialize}; use byte_buffer::ByteBufferWrite; use encoding_type::EncodingType; use node_types::StandardType; -use super::error::{KbinError, KbinErrorKind}; +use error::{Error, KbinError, KbinErrorKind}; +use super::{ARRAY_MASK, SIGNATURE, SIG_COMPRESSED}; -mod error; mod structure; mod tuple; -use self::error::Error; use self::structure::Struct; use self::tuple::Tuple; -const SIGNATURE: u8 = 0xA0; - -const SIG_COMPRESSED: u8 = 0x42; - -const ARRAY_MASK: u8 = 1 << 6; // 1 << 6 = 64 - pub type Result = StdResult; // Writing arrays should not be aligned after each write. Buffer realignment