diff --git a/src/ext.rs b/src/ext.rs index 1519174..9762f26 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -1,10 +1,5 @@ use super::*; -pub use object::{ - ObjectExt, - ObjectNewExt, -}; - pub trait Tuple2Ext: Sized { fn swap(self) -> (U, T); diff --git a/src/formats.rs b/src/formats.rs index acd5d7c..79cd553 100644 --- a/src/formats.rs +++ b/src/formats.rs @@ -1,9 +1,7 @@ //! The available formats to convert use super::*; use std::io; -use object::{ - Object, OwnedObject, -}; +use object::Object; /// Config for the encode/decode pub struct Config; @@ -12,8 +10,8 @@ pub struct Config; #[derive(Clone, Copy)] pub struct FormatDirective { - encode: fn (&Config, &mut dyn io::Write, &dyn Object) -> io::Result, - decode: fn (&Config, &mut dyn io::Read) -> io::Result<(OwnedObject<'static>, usize)>, + encode: fn (&Config, &mut dyn io::Write, &Object) -> io::Result, + decode: fn (&Config, &mut dyn io::Read) -> io::Result<(Object, usize)>, } macro_rules! directive { diff --git a/src/lib.rs b/src/lib.rs index 506adf4..6a79b75 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ +#![cfg_attr(debug_assertions, allow(unused_imports))] #![allow(dead_code)] mod ext; use ext::*; @@ -14,13 +15,11 @@ mod formats; #[cfg(test)] mod tests { - use serde::{Serialize, Deserialize}; - use serde_json::Value as JsonValue; - use serde_lexpr::Value as LexprValue; + use super::object::Object; #[test] fn from_json_to_lisp() { - let value_js: JsonValue = serde_json::json!({ + let value_js: Object = serde_json::json!({ "number": 200.10, "boolean": true, "null_value": null, @@ -28,9 +27,9 @@ mod tests { "array": ["string", false, null, 100.0, []], "empty": {} } - }); + }).into(); - eprintln!("Value (json): {}", value_js); + eprintln!("Value (Object): {}", value_js); println!("JSON: {}", serde_json::to_string(&value_js).expect("ser json")); println!("LISP: {}", serde_lexpr::to_string(&value_js).expect("ser lisp")); diff --git a/src/object.rs b/src/object.rs index 8cd8ee3..d261f83 100644 --- a/src/object.rs +++ b/src/object.rs @@ -1,65 +1,9 @@ use super::*; use std::io; -use smallbox::{ - SmallBox, - space, - smallbox, -}; -type OwnedObjectStackSize = space::S4; - -/// An object that can be expose serialise+deserialise methods polymorphically. -pub trait Object { - fn to_writer(&self, to: &mut dyn io::Write) -> io::Result; - fn from_reader(&mut self, from: &mut dyn io::Read) -> io::Result; //ugh, this SUUUCKS... - - - #[inline] fn to_vec_with_cap(&self, cap: usize) -> io::Result> - { - let mut string = Vec::with_capacity(cap); - self.to_writer(&mut string)?; - Ok(string) - } - #[inline] fn to_vec(&self) -> io::Result> - { - let mut string = Vec::new(); - self.to_writer(&mut string)?; - Ok(string) - } - #[inline] fn from_bytes(&mut self, mut bytes: &[u8]) -> io::Result - { - self.from_reader(&mut bytes) - } -} - -/// A polymorphic `Object` that is owned. -pub type OwnedObject<'a> = SmallBox; - -pub trait ObjectExt<'a>: Sized -{ - /// Consume this object into a dynamic (boxed) one. - fn into_owned(self) -> OwnedObject<'a>; -} - -pub trait ObjectNewExt<'a> -{ - /// Create a new object from this reader - fn new_obj_from_reader(from: R) -> io::Result<(OwnedObject<'a>, usize)>; -} - -impl<'a, T: 'a> ObjectExt<'a> for T -where T: Object -{ - #[inline] fn into_owned(self) -> OwnedObject<'a> { - smallbox!(self) - } -} - -impl<'a, T: 'a> ObjectNewExt<'a> for T -where T: Object + Default -{ - fn new_obj_from_reader(mut from: R) -> io::Result<(OwnedObject<'a>, usize)> { - let mut this = T::default(); - Ok((this.from_reader(&mut from)?, this.into_owned()).swap()) - } -} +/// The intermediate representation object. +/// +/// # Pipeline +/// This is used as the deserialisation target output, which is then re-serialized with the other serializer to output: +/// * input -(de)> `Object` -(ser)> output +pub type Object = serde_json::Value;