From 1f1b6e68df24a25eb4793438924e8a952e84cfe1 Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 21 May 2021 01:53:21 +0100 Subject: [PATCH] object rework --- src/ext.rs | 17 ++++++++++++++++- src/formats.rs | 5 +++-- src/lib.rs | 24 ++++++++++++++++++++++-- src/object.rs | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 5 deletions(-) diff --git a/src/ext.rs b/src/ext.rs index a3c9978..1519174 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -1,3 +1,18 @@ use super::*; -pub use object::ObjectExt; +pub use object::{ + ObjectExt, + ObjectNewExt, +}; + +pub trait Tuple2Ext: Sized +{ + fn swap(self) -> (U, T); +} + +impl Tuple2Ext for (T, U) +{ + #[inline(always)] fn swap(self) -> (U, T) { + (self.1, self.0) + } +} diff --git a/src/formats.rs b/src/formats.rs index 3a4e4ac..acd5d7c 100644 --- a/src/formats.rs +++ b/src/formats.rs @@ -5,6 +5,7 @@ use object::{ Object, OwnedObject, }; +/// Config for the encode/decode pub struct Config; /// A directive for a format on how to encode and decode objects. @@ -26,7 +27,7 @@ macro_rules! directive { /// All format directives and their name(s) pub static FORMATS: &[(&'static [&'static str], FormatDirective)] = &[ - directive!("json" => |_, _, _| todo!(), |_, _| todo!()), - directive!("s-expression", "sexpr" => |_, _, _| todo!(), |_, _| todo!()), + directive!("json", "js", "javascript" => |_, _, _| todo!(), |_, _| todo!()), + directive!("s-expression", "sexpr", "lisp", "lexpr" => |_, _, _| todo!(), |_, _| todo!()), directive!("cbor" => |_, _, _| todo!(), |_, _| todo!()), ]; diff --git a/src/lib.rs b/src/lib.rs index 643d2eb..506adf4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,10 +9,30 @@ mod formats; // TODO: External modules/functions/types/whatever +// TODO: FFI module, export C interface + #[cfg(test)] mod tests { + + use serde::{Serialize, Deserialize}; + use serde_json::Value as JsonValue; + use serde_lexpr::Value as LexprValue; + #[test] - fn it_works() { - assert_eq!(2 + 2, 4); + fn from_json_to_lisp() { + let value_js: JsonValue = serde_json::json!({ + "number": 200.10, + "boolean": true, + "null_value": null, + "map": { + "array": ["string", false, null, 100.0, []], + "empty": {} + } + }); + + eprintln!("Value (json): {}", 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 642bf64..8cd8ee3 100644 --- a/src/object.rs +++ b/src/object.rs @@ -12,6 +12,24 @@ type OwnedObjectStackSize = space::S4; 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. @@ -19,9 +37,16 @@ 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 { @@ -29,3 +54,12 @@ where T: Object 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()) + } +}