object rework

master
Avril 4 years ago
parent 8493506cf8
commit 1f1b6e68df
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,3 +1,18 @@
use super::*;
pub use object::ObjectExt;
pub use object::{
ObjectExt,
ObjectNewExt,
};
pub trait Tuple2Ext<T,U>: Sized
{
fn swap(self) -> (U, T);
}
impl<T, U> Tuple2Ext<T,U> for (T, U)
{
#[inline(always)] fn swap(self) -> (U, T) {
(self.1, self.0)
}
}

@ -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!()),
];

@ -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"));
}
}

@ -12,6 +12,24 @@ type OwnedObjectStackSize = space::S4;
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.
@ -19,9 +37,16 @@ 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
{
@ -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<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