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
- text and atom escaping
- tests
- text and atom escaping
- documentation

View File

@ -10,7 +10,9 @@ use super::{
Serializer,
SerializeDict,
SerializeList,
SerializeMultilineText
SerializeMultilineText,
SerializeReal,
SerializeRealPrimitive
};
use self::real::{
@ -85,13 +87,13 @@ where
}
#[inline]
fn write_delim(&mut self, delim: u8) -> Result<(), W::Error> {
unsafe { self.out.write_delim_unchecked(delim) }
fn write_delim(&mut self, delim: u8) -> Result<(), SerializerError<W::Error>> {
unsafe { Ok(self.out.write_delim_unchecked(delim)?) }
}
#[inline]
fn write_utf8(&mut self, slice: &[u8]) -> Result<(), W::Error> {
unsafe { self.out.write_utf8_unchecked(slice) }
fn write_utf8(&mut self, slice: &[u8]) -> Result<(), SerializerError<W::Error>> {
unsafe { Ok(self.out.write_utf8_unchecked(slice)?) }
}
}
@ -102,9 +104,11 @@ where
{
type Error = SerializerError<W::Error>;
type SerializeReal = SerializeRealBase<'se, C, W>;
type SerializeDict = SerializeDictBase<'se, C, W>;
type SerializeList = SerializeListBase<'se, C, W>;
type SerializeMultilineText = SerializeMultilineTextBase<'se, C, W>;
#[inline]
fn serialize_real_u8(self, real: u8) -> Result<(), Self::Error> {
@ -163,19 +167,19 @@ where
self.serialize_atom(name)?;
self.write_delim(GRAMMAR_PAREN_OPEN)?;
self.serialize_any(value)?;
Ok(self.write_delim(GRAMMAR_PAREN_CLOSE)?)
self.write_delim(GRAMMAR_PAREN_CLOSE)
}
#[inline]
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]
fn serialize_text<'a>(self, text: &'a str) -> Result<(), Self::Error> {
self.write_delim(GRAMMAR_QUOTE)?;
self.write_utf8(text.as_bytes())?;
Ok(self.write_delim(GRAMMAR_QUOTE)?)
self.write_delim(GRAMMAR_QUOTE)
}
#[inline]
@ -255,7 +259,7 @@ where
for item in list.into_iter() {
state.serialize_item(item)?;
}
Ok(state.end()?)
state.end()
}
}
@ -280,7 +284,7 @@ where
#[inline]
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]
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]
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;
}
pub trait SerializeRealPrimitive<R> {
type Error;
fn serialize_real_primitive(self, real: R) -> Result<(), Self::Error>;
}
pub trait SerializeReal: SerializeRealPrimitive<u8> {
}
pub trait SerializeDict {
type Error;
@ -39,6 +48,7 @@ pub trait SerializeMultilineText {
pub trait Serializer: Sized {
type Error;
type SerializeReal: SerializeReal;
type SerializeDict: SerializeDict;
type SerializeList: SerializeList;
type SerializeMultilineText: SerializeMultilineText;
@ -50,6 +60,14 @@ pub trait Serializer: Sized {
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_u16(self, real: u16) -> 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)>;
}