|
|
|
@ -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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|