Start playing around with real serialization

This commit is contained in:
avitex 2019-01-22 17:04:44 +11:00
parent 4b47a96acd
commit 60f5a8e05a
4 changed files with 146 additions and 14 deletions

View File

@ -1,3 +1,5 @@
# TODO # TODO
- tests
- text and atom escaping - text and atom escaping
- documentation

View File

@ -10,7 +10,9 @@ use super::{
Serializer, Serializer,
SerializeDict, SerializeDict,
SerializeList, SerializeList,
SerializeMultilineText SerializeMultilineText,
SerializeReal,
SerializeRealPrimitive
}; };
use self::real::{ use self::real::{
@ -85,13 +87,13 @@ where
} }
#[inline] #[inline]
fn write_delim(&mut self, delim: u8) -> Result<(), W::Error> { fn write_delim(&mut self, delim: u8) -> Result<(), SerializerError<W::Error>> {
unsafe { self.out.write_delim_unchecked(delim) } unsafe { Ok(self.out.write_delim_unchecked(delim)?) }
} }
#[inline] #[inline]
fn write_utf8(&mut self, slice: &[u8]) -> Result<(), W::Error> { fn write_utf8(&mut self, slice: &[u8]) -> Result<(), SerializerError<W::Error>> {
unsafe { self.out.write_utf8_unchecked(slice) } unsafe { Ok(self.out.write_utf8_unchecked(slice)?) }
} }
} }
@ -102,10 +104,12 @@ where
{ {
type Error = SerializerError<W::Error>; type Error = SerializerError<W::Error>;
type SerializeReal = SerializeRealBase<'se, C, W>;
type SerializeDict = SerializeDictBase<'se, C, W>; type SerializeDict = SerializeDictBase<'se, C, W>;
type SerializeList = SerializeListBase<'se, C, W>; type SerializeList = SerializeListBase<'se, C, W>;
type SerializeMultilineText = SerializeMultilineTextBase<'se, C, W>; type SerializeMultilineText = SerializeMultilineTextBase<'se, C, W>;
#[inline] #[inline]
fn serialize_real_u8(self, real: u8) -> Result<(), Self::Error> { fn serialize_real_u8(self, real: u8) -> Result<(), Self::Error> {
self.serialize_real::<u8>(real) self.serialize_real::<u8>(real)
@ -163,19 +167,19 @@ where
self.serialize_atom(name)?; self.serialize_atom(name)?;
self.write_delim(GRAMMAR_PAREN_OPEN)?; self.write_delim(GRAMMAR_PAREN_OPEN)?;
self.serialize_any(value)?; self.serialize_any(value)?;
Ok(self.write_delim(GRAMMAR_PAREN_CLOSE)?) self.write_delim(GRAMMAR_PAREN_CLOSE)
} }
#[inline] #[inline]
fn serialize_atom<'a>(self, atom: &'a str) -> Result<(), Self::Error> { fn serialize_atom<'a>(self, atom: &'a str) -> Result<(), Self::Error> {
Ok(self.write_utf8(atom.as_bytes())?) self.write_utf8(atom.as_bytes())
} }
#[inline] #[inline]
fn serialize_text<'a>(self, text: &'a str) -> Result<(), Self::Error> { fn serialize_text<'a>(self, text: &'a str) -> Result<(), Self::Error> {
self.write_delim(GRAMMAR_QUOTE)?; self.write_delim(GRAMMAR_QUOTE)?;
self.write_utf8(text.as_bytes())?; self.write_utf8(text.as_bytes())?;
Ok(self.write_delim(GRAMMAR_QUOTE)?) self.write_delim(GRAMMAR_QUOTE)
} }
#[inline] #[inline]
@ -255,7 +259,7 @@ where
for item in list.into_iter() { for item in list.into_iter() {
state.serialize_item(item)?; state.serialize_item(item)?;
} }
Ok(state.end()?) state.end()
} }
} }
@ -280,7 +284,7 @@ where
#[inline] #[inline]
fn end(self) -> Result<(), Self::Error> { fn end(self) -> Result<(), Self::Error> {
Ok(self.ser.write_delim(GRAMMAR_BRACE_CLOSE)?) self.ser.write_delim(GRAMMAR_BRACE_CLOSE)
} }
} }
@ -349,7 +353,7 @@ where
#[inline] #[inline]
fn end(self) -> Result<(), Self::Error> { fn end(self) -> Result<(), Self::Error> {
Ok(self.ser.write_delim(GRAMMAR_BRACE_CLOSE)?) self.ser.write_delim(GRAMMAR_BRACE_CLOSE)
} }
} }
@ -403,6 +407,48 @@ where
#[inline] #[inline]
fn end(self) -> Result<(), Self::Error> { fn end(self) -> Result<(), Self::Error> {
Ok(self.ser.write_delim(GRAMMAR_BTICK)?) self.ser.write_delim(GRAMMAR_BTICK)
} }
} }
///////////////////////////////////////////////////////////////////////////////
pub struct SerializeRealBase<'se, C, W>
where
C: Config,
W: Write
{
ser: &'se mut BaseSerializer<C, W>
}
impl<'se, C, W> SerializeRealBase<'se, C, W>
where
C: Config,
W: Write
{
#[inline]
fn new(ser: &'se mut BaseSerializer<C, W>) -> Result<Self, SerializerError<W::Error>> {
Ok(Self {
ser
})
}
}
impl<'se, C, W> SerializeRealPrimitive<u8> for SerializeRealBase<'se, C, W>
where
C: Config,
W: Write
{
type Error = SerializerError<W::Error>;
fn serialize_real_primitive(self, real: u8) -> Result<(), Self::Error> {
self.ser.write_utf8(real.to_string().as_bytes())
}
}
impl<'se, C, W> SerializeReal for SerializeRealBase<'se, C, W>
where
C: Config,
W: Write
{}

View File

@ -8,6 +8,15 @@ pub trait Serialize {
where S: Serializer; where S: Serializer;
} }
pub trait SerializeRealPrimitive<R> {
type Error;
fn serialize_real_primitive(self, real: R) -> Result<(), Self::Error>;
}
pub trait SerializeReal: SerializeRealPrimitive<u8> {
}
pub trait SerializeDict { pub trait SerializeDict {
type Error; type Error;
@ -39,6 +48,7 @@ pub trait SerializeMultilineText {
pub trait Serializer: Sized { pub trait Serializer: Sized {
type Error; type Error;
type SerializeReal: SerializeReal;
type SerializeDict: SerializeDict; type SerializeDict: SerializeDict;
type SerializeList: SerializeList; type SerializeList: SerializeList;
type SerializeMultilineText: SerializeMultilineText; type SerializeMultilineText: SerializeMultilineText;
@ -50,6 +60,14 @@ pub trait Serializer: Sized {
any.serialize(self) any.serialize(self)
} }
fn serialize_real<R>(self, r: R) -> Result<(), Self::Error>
where Self::SerializeReal: SerializeRealPrimitive<R>
{
Self::SerializeReal::serialize_real_primitive(r)
//Ok(C::RealSerializer::serialize_real::<W>(&mut self.out, r)?)
//Ok(())
}
fn serialize_real_u8(self, real: u8) -> Result<(), Self::Error>; fn serialize_real_u8(self, real: u8) -> Result<(), Self::Error>;
fn serialize_real_u16(self, real: u16) -> Result<(), Self::Error>; fn serialize_real_u16(self, real: u16) -> Result<(), Self::Error>;
fn serialize_real_u32(self, real: u32) -> Result<(), Self::Error>; fn serialize_real_u32(self, real: u32) -> Result<(), Self::Error>;

66
sehn/src/se/pretty/mod.rs Normal file
View File

@ -0,0 +1,66 @@
use super::base::*;
impl Serializer for PrettySerializer {
type Error;
type SerializeDict: SerializeDictBase;
type SerializeList: SerializeListBase;
type SerializeMultilineText: SerializeMultilineTextBase;
#[inline]
fn serialize_real_u8(self, real: u8) -> Result<(), Self::Error> {
self.base.serialize_real_u8(real)
}
fn serialize_real_u16(self, real: u16) -> Result<(), Self::Error> {
self.base.serialize_real_u8(real)
}
fn serialize_real_u32(self, real: u32) -> Result<(), Self::Error> {
self.base.serialize_real_u8(real)
}
fn serialize_real_u64(self, real: u64) -> Result<(), Self::Error> {
self.base.serialize_real_u8(real)
}
fn serialize_real_i8(self, real: i8) -> Result<(), Self::Error> {
self.base.serialize_real_u8(real)
}
fn serialize_real_i16(self, real: i16) -> Result<(), Self::Error> {
self.base.serialize_real_u8(real)
}
fn serialize_real_i32(self, real: i32) -> Result<(), Self::Error> {
self.base.serialize_real_u8(real)
}
fn serialize_real_i64(self, real: i64) -> Result<(), Self::Error> {
self.base.serialize_real_i64(real)
}
fn serialize_real_f32(self, real: f32) -> Result<(), Self::Error> {
self.base.serialize_real_f32(real)
}
fn serialize_real_f64(self, real: f64) -> Result<(), Self::Error> {
self.base.serialize_real_f64(real)
}
fn serialize_atom<'a>(self, atom: &'a str) -> Result<(), Self::Error>;
fn serialize_text<'a>(self, text: &'a str) -> Result<(), Self::Error>;
fn serialize_multiline_text<'a, M>(self, lines: M) -> Result<(), Self::Error>
where M: IntoIterator<Item = &'a str>;
fn serialize_unit<'a, V>(self, name: &'a str, value: V) -> Result<(), Self::Error>
where V: Serialize;
fn serialize_list_parts(self) -> Result<Self::SerializeList, Self::Error>;
fn serialize_dict_parts(self) -> Result<Self::SerializeDict, Self::Error>;
fn serialize_multiline_text_parts(self) -> Result<Self::SerializeMultilineText, Self::Error>;
fn serialize_list<V, L>(self, list: L) -> Result<(), Self::Error>
where
V: Serialize,
L: IntoIterator<Item = V>;
fn serialize_dict<K, V, D>(self, dict: D) -> Result<(), Self::Error>
where
K: Serialize,
V: Serialize,
D: IntoIterator<Item = (K, V)>;
}