From 6aecad1c9a907c0dc567aef3388bb1857f9ed47d Mon Sep 17 00:00:00 2001 From: Sam Scott Date: Thu, 9 Mar 2017 19:22:01 -0500 Subject: Support serializing sequences. --- src/ser/part.rs | 91 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 48 insertions(+), 43 deletions(-) (limited to 'src/ser/part.rs') diff --git a/src/ser/part.rs b/src/ser/part.rs index d2ade0a..d456f4b 100644 --- a/src/ser/part.rs +++ b/src/ser/part.rs @@ -2,112 +2,117 @@ use dtoa; use itoa; use ser::Error; use serde::ser; +use serde; use std::str; +use std::marker::PhantomData; -pub struct PartSerializer { +pub struct PartSerializer> { sink: S, + marker: PhantomData, } -impl PartSerializer { +impl> PartSerializer { pub fn new(sink: S) -> Self { - PartSerializer { sink: sink } + PartSerializer { sink: sink, marker: PhantomData } } } -pub trait Sink: Sized + ser::SerializeStruct { +pub trait Sink: Sized + ser::SerializeStruct + ser::SerializeSeq + where E: serde::ser::Error +{ // type Ok; - + // type Error; fn serialize_static_str(self, value: &'static str) - -> Result; + -> Result; - fn serialize_str(self, value: &str) -> Result; - fn serialize_string(self, value: String) -> Result; - fn serialize_none(self) -> Result; + fn serialize_str(self, value: &str) -> Result; + fn serialize_string(self, value: String) -> Result; + fn serialize_none(self) -> Result; fn serialize_some (self, value: &T) - -> Result; + -> Result; fn unsupported(self) -> Error; } -impl> ser::Serializer for PartSerializer { - type Ok = S::Ok; +impl> ser::Serializer for PartSerializer { + type Ok = SO; type Error = Error; - type SerializeSeq = ser::Impossible; - type SerializeTuple = ser::Impossible; - type SerializeTupleStruct = ser::Impossible; - type SerializeTupleVariant = ser::Impossible; - type SerializeMap = ser::Impossible; + type SerializeSeq = S; + type SerializeTuple = ser::Impossible; + type SerializeTupleStruct = ser::Impossible; + type SerializeTupleVariant = ser::Impossible; + type SerializeMap = ser::Impossible; type SerializeStruct = S; - type SerializeStructVariant = ser::Impossible; + type SerializeStructVariant = ser::Impossible; - fn serialize_bool(self, v: bool) -> Result { + fn serialize_bool(self, v: bool) -> Result { self.sink.serialize_static_str(if v { "true" } else { "false" }) } - fn serialize_i8(self, v: i8) -> Result { + fn serialize_i8(self, v: i8) -> Result { self.serialize_integer(v) } - fn serialize_i16(self, v: i16) -> Result { + fn serialize_i16(self, v: i16) -> Result { self.serialize_integer(v) } - fn serialize_i32(self, v: i32) -> Result { + fn serialize_i32(self, v: i32) -> Result { self.serialize_integer(v) } - fn serialize_i64(self, v: i64) -> Result { + fn serialize_i64(self, v: i64) -> Result { self.serialize_integer(v) } - fn serialize_u8(self, v: u8) -> Result { + fn serialize_u8(self, v: u8) -> Result { self.serialize_integer(v) } - fn serialize_u16(self, v: u16) -> Result { + fn serialize_u16(self, v: u16) -> Result { self.serialize_integer(v) } - fn serialize_u32(self, v: u32) -> Result { + fn serialize_u32(self, v: u32) -> Result { self.serialize_integer(v) } - fn serialize_u64(self, v: u64) -> Result { + fn serialize_u64(self, v: u64) -> Result { self.serialize_integer(v) } - fn serialize_f32(self, v: f32) -> Result { + fn serialize_f32(self, v: f32) -> Result { self.serialize_floating(v) } - fn serialize_f64(self, v: f64) -> Result { + fn serialize_f64(self, v: f64) -> Result { self.serialize_floating(v) } - fn serialize_char(self, v: char) -> Result { + fn serialize_char(self, v: char) -> Result { self.sink.serialize_string(v.to_string()) } - fn serialize_str(self, value: &str) -> Result { + fn serialize_str(self, value: &str) -> Result { self.sink.serialize_str(value) } - fn serialize_bytes(self, value: &[u8]) -> Result { + fn serialize_bytes(self, value: &[u8]) -> Result { match str::from_utf8(value) { Ok(value) => self.sink.serialize_str(value), Err(err) => Err(Error::Utf8(err)), } } - fn serialize_unit(self) -> Result { + fn serialize_unit(self) -> Result { Err(self.sink.unsupported()) } - fn serialize_unit_struct(self, name: &'static str) -> Result { + fn serialize_unit_struct(self, name: &'static str) -> Result { self.sink.serialize_static_str(name.into()) } @@ -115,7 +120,7 @@ impl> ser::Serializer for PartSerializer { _name: &'static str, _variant_index: usize, variant: &'static str) - -> Result { + -> Result { self.sink.serialize_static_str(variant.into()) } @@ -123,7 +128,7 @@ impl> ser::Serializer for PartSerializer { (self, _name: &'static str, value: &T) - -> Result { + -> Result { value.serialize(self) } @@ -133,24 +138,24 @@ impl> ser::Serializer for PartSerializer { _variant_index: usize, _variant: &'static str, _value: &T) - -> Result { + -> Result { Err(self.sink.unsupported()) } - fn serialize_none(self) -> Result { + fn serialize_none(self) -> Result { self.sink.serialize_none() } fn serialize_some(self, value: &T) - -> Result { + -> Result { self.sink.serialize_some(value) } fn serialize_seq(self, _len: Option) -> Result { - Err(self.sink.unsupported()) + Ok(self.sink) } @@ -209,8 +214,8 @@ impl> ser::Serializer for PartSerializer { } } -impl> PartSerializer { - fn serialize_integer(self, value: I) -> Result +impl> PartSerializer { + fn serialize_integer(self, value: I) -> Result where I: itoa::Integer, { let mut buf = [b'\0'; 20]; @@ -219,7 +224,7 @@ impl> PartSerializer { ser::Serializer::serialize_str(self, part) } - fn serialize_floating(self, value: F) -> Result + fn serialize_floating(self, value: F) -> Result where F: dtoa::Floating, { let mut buf = [b'\0'; 24]; -- cgit v1.2.3