From a006ccb8aff2b2397b5ca838e867fe181d7f5423 Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 28 May 2021 17:01:58 +0100 Subject: [PATCH] formats implemented --- src/formats.rs | 45 +++++++++++++++++++++++++++++++++++++++----- src/formats/cbor.rs | 10 ++++++---- src/formats/json.rs | 10 ++++++---- src/formats/sexpr.rs | 10 ++++++---- 4 files changed, 58 insertions(+), 17 deletions(-) diff --git a/src/formats.rs b/src/formats.rs index 14c91e3..3167932 100644 --- a/src/formats.rs +++ b/src/formats.rs @@ -33,8 +33,8 @@ pub struct FormatDirective /// Trait for statically implementing formatters to/from `Object`. pub trait Format { - fn encode(cfg: &Config, to: W, obj: &Object) -> Result; - fn decode(cfg: &Config, from: R) -> Result; + fn encode(cfg: &Config, to: W, obj: &Object) -> Result; + fn decode(cfg: &Config, from: R) -> Result; } macro_rules! directive { @@ -46,10 +46,10 @@ macro_rules! directive { }; (for $fmt:path => $($name:literal),+) => { directive!($($name),+ => |c, w, o| { - <$fmt as Format>::encode(c, w, o) + <$fmt as Format>::encode(c, w, o).map_err(Error::encode) }, |c, r| { let mut r = r.with_counter(); - let obj = <$fmt as Format>::decode(c, &mut r)?; + let obj = <$fmt as Format>::decode(c, &mut r).map_err(Error::decode)?; Ok((obj, r.count())) }) } @@ -67,6 +67,9 @@ pub static FORMATS: &[(&'static [&'static str], FormatDirective)] = &[ pub enum ErrorKind { IO(io::Error), + Json(serde_json::Error), + Cbor(serde_cbor::Error), + Lexpr(serde_lexpr::Error), Unknown, } @@ -78,12 +81,41 @@ impl From for ErrorKind } } +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 { @@ -119,6 +151,9 @@ 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, }) } @@ -145,7 +180,6 @@ impl From<(io::Error, bool)> for Error } } - impl From for io::Error { fn from(from: Error) -> Self @@ -157,3 +191,4 @@ impl From for io::Error } } } + diff --git a/src/formats/cbor.rs b/src/formats/cbor.rs index 0c924f6..a5baa76 100644 --- a/src/formats/cbor.rs +++ b/src/formats/cbor.rs @@ -5,10 +5,12 @@ pub struct CborFormatter; impl Format for CborFormatter { - fn encode(cfg: &Config, to: W, obj: &Object) -> Result { - todo!() + fn encode(_cfg: &Config, to: W, obj: &Object) -> Result { + let mut to = to.with_counter(); + serde_cbor::to_writer(&mut to, obj)?; + Ok(to.count()) } - fn decode(cfg: &Config, from: R) -> Result { - todo!() + fn decode(_cfg: &Config, from: R) -> Result { + Ok(serde_cbor::from_reader(from)?) } } diff --git a/src/formats/json.rs b/src/formats/json.rs index 022a143..312ac48 100644 --- a/src/formats/json.rs +++ b/src/formats/json.rs @@ -5,10 +5,12 @@ pub struct JsonFormatter; impl Format for JsonFormatter { - fn encode(cfg: &Config, to: W, obj: &Object) -> Result { - todo!() + fn encode(_cfg: &Config, to: W, obj: &Object) -> Result { + let mut to = to.with_counter(); + serde_json::to_writer(&mut to, obj)?; + Ok(to.count()) } - fn decode(cfg: &Config, from: R) -> Result { - todo!() + fn decode(_cfg: &Config, mut from: R) -> Result { + Ok(serde_json::from_reader(&mut from)?) } } diff --git a/src/formats/sexpr.rs b/src/formats/sexpr.rs index 8bdb444..d94ebfd 100644 --- a/src/formats/sexpr.rs +++ b/src/formats/sexpr.rs @@ -5,10 +5,12 @@ pub struct SExpressionFormatter; impl Format for SExpressionFormatter { - fn encode(cfg: &Config, to: W, obj: &Object) -> Result { - todo!() + fn encode(_cfg: &Config, to: W, obj: &Object) -> Result { + let mut to = to.with_counter(); + serde_lexpr::to_writer(&mut to, obj)?; + Ok(to.count()) } - fn decode(cfg: &Config, from: R) -> Result { - todo!() + fn decode(_cfg: &Config, from: R) -> Result { + Ok(serde_lexpr::from_reader(from)?) } }