formats implemented

master
Avril 3 years ago
parent ad442d6678
commit a006ccb8af
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -33,8 +33,8 @@ pub struct FormatDirective
/// Trait for statically implementing formatters to/from `Object`.
pub trait Format
{
fn encode<W: io::Write>(cfg: &Config, to: W, obj: &Object) -> Result<usize, Error>;
fn decode<R: io::Read>(cfg: &Config, from: R) -> Result<Object, Error>;
fn encode<W: io::Write>(cfg: &Config, to: W, obj: &Object) -> Result<usize, ErrorKind>;
fn decode<R: io::Read>(cfg: &Config, from: R) -> Result<Object, ErrorKind>;
}
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<io::Error> for ErrorKind
}
}
impl From<serde_json::Error> for ErrorKind
{
fn from(from: serde_json::Error) -> Self
{
Self::Json(from)
}
}
impl From<serde_cbor::Error> for ErrorKind
{
fn from(from: serde_cbor::Error) -> Self
{
Self::Cbor(from)
}
}
impl From<serde_lexpr::Error> 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<ErrorKind>) -> 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<Error> for io::Error
{
fn from(from: Error) -> Self
@ -157,3 +191,4 @@ impl From<Error> for io::Error
}
}
}

@ -5,10 +5,12 @@ pub struct CborFormatter;
impl Format for CborFormatter
{
fn encode<W: io::Write>(cfg: &Config, to: W, obj: &Object) -> Result<usize, Error> {
todo!()
fn encode<W: io::Write>(_cfg: &Config, to: W, obj: &Object) -> Result<usize, ErrorKind> {
let mut to = to.with_counter();
serde_cbor::to_writer(&mut to, obj)?;
Ok(to.count())
}
fn decode<R: io::Read>(cfg: &Config, from: R) -> Result<Object, Error> {
todo!()
fn decode<R: io::Read>(_cfg: &Config, from: R) -> Result<Object, ErrorKind> {
Ok(serde_cbor::from_reader(from)?)
}
}

@ -5,10 +5,12 @@ pub struct JsonFormatter;
impl Format for JsonFormatter
{
fn encode<W: io::Write>(cfg: &Config, to: W, obj: &Object) -> Result<usize, Error> {
todo!()
fn encode<W: io::Write>(_cfg: &Config, to: W, obj: &Object) -> Result<usize, ErrorKind> {
let mut to = to.with_counter();
serde_json::to_writer(&mut to, obj)?;
Ok(to.count())
}
fn decode<R: io::Read>(cfg: &Config, from: R) -> Result<Object, Error> {
todo!()
fn decode<R: io::Read>(_cfg: &Config, mut from: R) -> Result<Object, ErrorKind> {
Ok(serde_json::from_reader(&mut from)?)
}
}

@ -5,10 +5,12 @@ pub struct SExpressionFormatter;
impl Format for SExpressionFormatter
{
fn encode<W: io::Write>(cfg: &Config, to: W, obj: &Object) -> Result<usize, Error> {
todo!()
fn encode<W: io::Write>(_cfg: &Config, to: W, obj: &Object) -> Result<usize, ErrorKind> {
let mut to = to.with_counter();
serde_lexpr::to_writer(&mut to, obj)?;
Ok(to.count())
}
fn decode<R: io::Read>(cfg: &Config, from: R) -> Result<Object, Error> {
todo!()
fn decode<R: io::Read>(_cfg: &Config, from: R) -> Result<Object, ErrorKind> {
Ok(serde_lexpr::from_reader(from)?)
}
}

Loading…
Cancel
Save