From 5f57be3ed9184a41199bf0e5ff6389cfc90490ad Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 28 May 2021 17:13:03 +0100 Subject: [PATCH] box Error container type --- src/formats.rs | 146 ++---------------------------------------- src/formats/config.rs | 10 +++ src/formats/error.rs | 135 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+), 140 deletions(-) create mode 100644 src/formats/config.rs create mode 100644 src/formats/error.rs diff --git a/src/formats.rs b/src/formats.rs index 3167932..15d2b42 100644 --- a/src/formats.rs +++ b/src/formats.rs @@ -1,9 +1,6 @@ //! The available formats to convert use super::*; use std::io; -use std::{ - error, fmt, -}; use object::Object; @@ -14,13 +11,11 @@ mod sexpr; /// `Format` impl for CBOR. mod cbor; -/// Config for the encode/decode -#[derive(Debug)] -pub struct Config{ - //TODO: What goes here? - /// Any arbitrary data. - pub user: Option, -} +pub mod error; +use error::*; + +pub mod config; +use config::*; /// A directive for a format on how to encode and decode objects. #[derive(Clone, Copy)] @@ -62,133 +57,4 @@ pub static FORMATS: &[(&'static [&'static str], FormatDirective)] = &[ directive!(for cbor::CborFormatter => "cbor"), ]; -/// A formatting error kind. See `Error`. -#[derive(Debug)] -pub enum ErrorKind -{ - IO(io::Error), - Json(serde_json::Error), - Cbor(serde_cbor::Error), - Lexpr(serde_lexpr::Error), - Unknown, -} - -impl From for ErrorKind -{ - fn from(from: io::Error) -> Self - { - Self::IO(from) - } -} - -impl From for ErrorKind -{ - fn from(from: serde_json::Error) -> Self - { - Self::Json(from) - } -} - -impl From for ErrorKind -{ - fn from(from: serde_cbor::Error) -> Self - { - Self::Cbor(from) - } -} - -impl From for ErrorKind -{ - fn from(from: serde_lexpr::Error) -> Self - { - Self::Lexpr(from) - } -} - - -/// A formatting error. -#[derive(Debug)] -pub struct Error(ErrorKind, bool); - -impl Error -{ - #[inline(always)] fn into_mode(self, encode: bool) -> Self - { - Self(self.0, encode) - } - /// An encode error - #[inline] pub fn encode(kind: impl Into) -> Self - { - Self(kind.into(), true) - } - /// A decode error - #[inline] pub fn decode(kind: impl Into) -> Self - { - Self(kind.into(), false) - } - - /// What kind of error was it? - #[inline] pub fn kind(&self) -> &ErrorKind - { - &self.0 - } - - /// Is an encode error? - #[inline] pub fn is_encode(&self) -> bool - { - self.1 - } - - /// Is a decode error? - #[inline] pub fn is_decode(&self) -> bool - { - !self.1 - } -} - -impl error::Error for Error -{ - fn source(&self) -> Option<&(dyn error::Error + 'static)> { - Some(match &self.0 { - ErrorKind::IO(i) => i, - ErrorKind::Json(i) => i, - ErrorKind::Cbor(i) => i, - ErrorKind::Lexpr(i) => i, - _ => return None, - }) - } -} - -impl fmt::Display for Error -{ - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result - { - write!(f, "formatting error: ")?; - if self.1 { - write!(f, "encode") - } else { - write!(f, "decode") - } - } -} - -impl From<(io::Error, bool)> for Error -{ - #[inline] fn from(from: (io::Error, bool)) -> Self - { - Self(ErrorKind::IO(from.0), from.1) - } -} - -impl From for io::Error -{ - fn from(from: Error) -> Self - { - match from.0 - { - ErrorKind::IO(io) => io, - _ => io::Error::new(io::ErrorKind::Other, from), - } - } -} - +//const _: &[u8; 0] = &[0u8; std::mem::size_of::()]; // Size of error type check (unboxed 56 -> boxed 16) diff --git a/src/formats/config.rs b/src/formats/config.rs new file mode 100644 index 0000000..9096df0 --- /dev/null +++ b/src/formats/config.rs @@ -0,0 +1,10 @@ +//! Config elements. (If they're needed?) +use super::*; + +/// Config for the encode/decode +#[derive(Debug, Default)] +pub struct Config{ + //TODO: What goes here? + ///// Any arbitrary data. + //pub user: Option, +} diff --git a/src/formats/error.rs b/src/formats/error.rs new file mode 100644 index 0000000..2388498 --- /dev/null +++ b/src/formats/error.rs @@ -0,0 +1,135 @@ +//! Formatting error(s). +use super::*; +use std::{ + error, fmt, +}; + +/// A formatting error kind. See `Error`. +#[derive(Debug)] +pub enum ErrorKind +{ + IO(io::Error), + Json(serde_json::Error), + Cbor(serde_cbor::Error), + Lexpr(serde_lexpr::Error), + Unknown, +} + +impl From for ErrorKind +{ + fn from(from: io::Error) -> Self + { + Self::IO(from) + } +} + +impl From for ErrorKind +{ + fn from(from: serde_json::Error) -> Self + { + Self::Json(from) + } +} + +impl From for ErrorKind +{ + fn from(from: serde_cbor::Error) -> Self + { + Self::Cbor(from) + } +} + +impl From for ErrorKind +{ + fn from(from: serde_lexpr::Error) -> Self + { + Self::Lexpr(from) + } +} + + +/// A formatting error. +#[derive(Debug)] +pub struct Error(Box, bool); + +impl Error +{ + #[inline(always)] fn into_mode(self, encode: bool) -> Self + { + Self(self.0, encode) + } + /// An encode error + #[inline] pub fn encode(kind: impl Into) -> Self + { + Self(Box::new(kind.into()), true) + } + /// A decode error + #[inline] pub fn decode(kind: impl Into) -> Self + { + Self(Box::new(kind.into()), false) + } + + /// What kind of error was it? + #[inline] pub fn kind(&self) -> &ErrorKind + { + &self.0 + } + + /// Is an encode error? + #[inline] pub fn is_encode(&self) -> bool + { + self.1 + } + + /// Is a decode error? + #[inline] pub fn is_decode(&self) -> bool + { + !self.1 + } +} + +impl error::Error for Error +{ + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + Some(match self.0.as_ref() { + ErrorKind::IO(i) => i, + ErrorKind::Json(i) => i, + ErrorKind::Cbor(i) => i, + ErrorKind::Lexpr(i) => i, + _ => return None, + }) + } +} + +impl fmt::Display for Error +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result + { + write!(f, "formatting error: ")?; + if self.1 { + write!(f, "encode") + } else { + write!(f, "decode") + } + } +} + +impl From<(io::Error, bool)> for Error +{ + #[inline] fn from(from: (io::Error, bool)) -> Self + { + Self(Box::new(ErrorKind::IO(from.0)), from.1) + } +} + +impl From for io::Error +{ + fn from(from: Error) -> Self + { + match *from.0 + { + ErrorKind::IO(io) => io, + _ => io::Error::new(io::ErrorKind::Other, from), + } + } +}