Browse Source

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 6 months ago
parent
commit
191a466809
Signed by: flanchan GPG Key ID: 284488987C31F630
  1. 5
      src/ext.rs
  2. 8
      src/formats.rs
  3. 11
      src/lib.rs
  4. 68
      src/object.rs

5
src/ext.rs

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

8
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<usize>,
decode: fn (&Config, &mut dyn io::Read) -> io::Result<(OwnedObject<'static>, usize)>,
encode: fn (&Config, &mut dyn io::Write, &Object) -> io::Result<usize>,
decode: fn (&Config, &mut dyn io::Read) -> io::Result<(Object, usize)>,
}
macro_rules! directive {

11
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"));

68
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<usize>;
fn from_reader(&mut self, from: &mut dyn io::Read) -> io::Result<usize>; //ugh, this SUUUCKS...
#[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())
}
}
/// 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;

Loading…
Cancel
Save