From 92d7a35dc10a9df7ccff19c61788e87316c12cac Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 28 May 2021 17:50:53 +0100 Subject: [PATCH] added format kind C enum --- src/formats.rs | 37 ++++++++++++++++++++++++++++++++++++ src/formats/error.rs | 45 ++++++++++++++++++++++++++++++++++++-------- src/lib.rs | 6 ++++-- 3 files changed, 78 insertions(+), 10 deletions(-) diff --git a/src/formats.rs b/src/formats.rs index 15d2b42..f9479ed 100644 --- a/src/formats.rs +++ b/src/formats.rs @@ -1,6 +1,7 @@ //! The available formats to convert use super::*; use std::io; +use std::convert::TryFrom; use object::Object; @@ -57,4 +58,40 @@ pub static FORMATS: &[(&'static [&'static str], FormatDirective)] = &[ directive!(for cbor::CborFormatter => "cbor"), ]; +pub(crate) const FORMAT_KIND_JSON: u32 = FormatKind::Json as u32; +pub(crate) const FORMAT_KIND_SEXPR: u32 = FormatKind::SExpr as u32; +pub(crate) const FORMAT_KIND_CBOR: u32 = FormatKind::CBor as u32; +/// The formats available for conversion +#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy, PartialOrd, Ord)] +#[repr(C)] +pub enum FormatKind +{ + Json = 1, + SExpr = 2, + CBor = 3, +} + +impl From for u32 +{ + fn from(from: FormatKind) -> Self + { + from as Self + } +} + +impl TryFrom for FormatKind +{ + type Error = ErrorKind; + + fn try_from(from: u32) -> Result + { + Ok(match from { + FORMAT_KIND_JSON => Self::Json, + FORMAT_KIND_SEXPR => Self::SExpr, + FORMAT_KIND_CBOR => Self::CBor, + _ => return Err(ErrorKind::UnknownFormat(from)), + }) + } +} + //const _: &[u8; 0] = &[0u8; std::mem::size_of::()]; // Size of error type check (unboxed 56 -> boxed 16) diff --git a/src/formats/error.rs b/src/formats/error.rs index 2bd4b84..0204d57 100644 --- a/src/formats/error.rs +++ b/src/formats/error.rs @@ -12,9 +12,43 @@ pub enum ErrorKind Json(serde_json::Error), Cbor(serde_cbor::Error), Lexpr(serde_lexpr::Error), + /// Unknown format ID + UnknownFormat(u32), + /// Unknown error Unknown, } +impl error::Error for ErrorKind +{ + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + Some(match &self { + ErrorKind::IO(i) => i, + ErrorKind::Json(i) => i, + ErrorKind::Cbor(i) => i, + ErrorKind::Lexpr(i) => i, + _ => return None, + }) + } +} +impl fmt::Display for ErrorKind +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result + { + match self { + Self::IO(_) => write!(f, "io"), + + Self::Json(_) => write!(f, "conv json"), + Self::Cbor(_) => write!(f, "conv cbor"), + Self::Lexpr(_) => write!(f, "conv lexpr"), + + Self::UnknownFormat(fmt) => write!(f, "unknown format ID: {}", fmt), + + _ => write!(f, "unknown error") + } + } +} + + impl From for ErrorKind { fn from(from: io::Error) -> Self @@ -92,14 +126,9 @@ impl Error 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, - }) + #[inline] fn source(&self) -> Option<&(dyn error::Error + 'static)> { + use error::Error; + self.0.source() } } diff --git a/src/lib.rs b/src/lib.rs index 6a79b75..2cf4ab4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,8 +5,10 @@ mod ext; use ext::*; // Internal modules -mod object; -mod formats; +pub mod object; +pub mod formats; + +pub use formats::FormatKind; // TODO: External modules/functions/types/whatever