added format kind C enum

master
Avril 3 years ago
parent 3a3e66675c
commit 92d7a35dc1
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -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<FormatKind> for u32
{
fn from(from: FormatKind) -> Self
{
from as Self
}
}
impl TryFrom<u32> for FormatKind
{
type Error = ErrorKind;
fn try_from(from: u32) -> Result<Self, Self::Error>
{
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::<Error>()]; // Size of error type check (unboxed 56 -> boxed 16)

@ -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<io::Error> 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()
}
}

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

Loading…
Cancel
Save