error: take Error type from ser/error.rs

This commit is contained in:
Matt Bilker
2018-07-09 09:26:36 -04:00
parent 168c481ac5
commit 44e2f153dc
3 changed files with 58 additions and 70 deletions

View File

@@ -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<FromUtf8Error> for KbinError {
inner.context(KbinErrorKind::Utf8).into()
}
}
#[derive(Clone, Debug)]
pub enum Error {
Message(String),
Wrapped(Compat<KbinError>),
}
impl ser::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error::Message(msg.to_string())
}
}
impl de::Error for Error {
fn custom<T: Display>(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<KbinError> for Error {
fn from(inner: KbinError) -> Self {
Error::Wrapped(inner.compat())
}
}
impl From<KbinErrorKind> for Error {
fn from(inner: KbinErrorKind) -> Self {
Error::Wrapped(KbinError::from(inner).compat())
}
}
impl From<Context<KbinErrorKind>> for Error {
fn from(inner: Context<KbinErrorKind>) -> Self {
Error::Wrapped(KbinError::from(inner).compat())
}
}

View File

@@ -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<KbinError>),
}
impl ser::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error::Message(msg.to_string())
}
}
impl de::Error for Error {
fn custom<T: Display>(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<KbinError> for Error {
fn from(inner: KbinError) -> Self {
Error::Wrapped(inner.compat())
}
}
impl From<KbinErrorKind> for Error {
fn from(inner: KbinErrorKind) -> Self {
Error::Wrapped(KbinError::from(inner).compat())
}
}
impl From<Context<KbinErrorKind>> for Error {
fn from(inner: Context<KbinErrorKind>) -> Self {
Error::Wrapped(KbinError::from(inner).compat())
}
}

View File

@@ -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<T> = StdResult<T, Error>;
// Writing arrays should not be aligned after each write. Buffer realignment