reworked `Object`: is now a concrete single type (Currently an alias for serde_json::Value, this may prove a sufficient medium, if not, we can make our own pipeline intermedate type)

master
Avril 3 years ago
parent 1f1b6e68df
commit 191a466809
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,10 +1,5 @@
use super::*; use super::*;
pub use object::{
ObjectExt,
ObjectNewExt,
};
pub trait Tuple2Ext<T,U>: Sized pub trait Tuple2Ext<T,U>: Sized
{ {
fn swap(self) -> (U, T); fn swap(self) -> (U, T);

@ -1,9 +1,7 @@
//! The available formats to convert //! The available formats to convert
use super::*; use super::*;
use std::io; use std::io;
use object::{ use object::Object;
Object, OwnedObject,
};
/// Config for the encode/decode /// Config for the encode/decode
pub struct Config; pub struct Config;
@ -12,8 +10,8 @@ pub struct Config;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct FormatDirective pub struct FormatDirective
{ {
encode: fn (&Config, &mut dyn io::Write, &dyn Object) -> io::Result<usize>, encode: fn (&Config, &mut dyn io::Write, &Object) -> io::Result<usize>,
decode: fn (&Config, &mut dyn io::Read) -> io::Result<(OwnedObject<'static>, usize)>, decode: fn (&Config, &mut dyn io::Read) -> io::Result<(Object, usize)>,
} }
macro_rules! directive { macro_rules! directive {

@ -1,4 +1,5 @@
#![cfg_attr(debug_assertions, allow(unused_imports))]
#![allow(dead_code)] #![allow(dead_code)]
mod ext; use ext::*; mod ext; use ext::*;
@ -14,13 +15,11 @@ mod formats;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serde::{Serialize, Deserialize}; use super::object::Object;
use serde_json::Value as JsonValue;
use serde_lexpr::Value as LexprValue;
#[test] #[test]
fn from_json_to_lisp() { fn from_json_to_lisp() {
let value_js: JsonValue = serde_json::json!({ let value_js: Object = serde_json::json!({
"number": 200.10, "number": 200.10,
"boolean": true, "boolean": true,
"null_value": null, "null_value": null,
@ -28,9 +27,9 @@ mod tests {
"array": ["string", false, null, 100.0, []], "array": ["string", false, null, 100.0, []],
"empty": {} "empty": {}
} }
}); }).into();
eprintln!("Value (json): {}", value_js); eprintln!("Value (Object): {}", value_js);
println!("JSON: {}", serde_json::to_string(&value_js).expect("ser json")); println!("JSON: {}", serde_json::to_string(&value_js).expect("ser json"));
println!("LISP: {}", serde_lexpr::to_string(&value_js).expect("ser lisp")); println!("LISP: {}", serde_lexpr::to_string(&value_js).expect("ser lisp"));

@ -1,65 +1,9 @@
use super::*; use super::*;
use std::io; use std::io;
use smallbox::{
SmallBox,
space,
smallbox,
};
type OwnedObjectStackSize = space::S4; /// The intermediate representation object.
///
/// An object that can be expose serialise+deserialise methods polymorphically. /// # Pipeline
pub trait Object { /// This is used as the deserialisation target output, which is then re-serialized with the other serializer to output:
fn to_writer(&self, to: &mut dyn io::Write) -> io::Result<usize>; /// * input -(de)> `Object` -(ser)> output
fn from_reader(&mut self, from: &mut dyn io::Read) -> io::Result<usize>; //ugh, this SUUUCKS... pub type Object = serde_json::Value;
#[inline] fn to_vec_with_cap(&self, cap: usize) -> io::Result<Vec<u8>>
{
let mut string = Vec::with_capacity(cap);
self.to_writer(&mut string)?;
Ok(string)
}
#[inline] fn to_vec(&self) -> io::Result<Vec<u8>>
{
let mut string = Vec::new();
self.to_writer(&mut string)?;
Ok(string)
}
#[inline] fn from_bytes(&mut self, mut bytes: &[u8]) -> io::Result<usize>
{
self.from_reader(&mut bytes)
}
}
/// A polymorphic `Object` that is owned.
pub type OwnedObject<'a> = SmallBox<dyn Object + 'a, OwnedObjectStackSize>;
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<R: io::Read>(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<R: io::Read>(mut from: R) -> io::Result<(OwnedObject<'a>, usize)> {
let mut this = T::default();
Ok((this.from_reader(&mut from)?, this.into_owned()).swap())
}
}

Loading…
Cancel
Save