From 6e71ba43eb6bd62f2b567224e387333016bd3a5c Mon Sep 17 00:00:00 2001 From: Sam Scott Date: Sat, 20 May 2017 22:07:44 +0100 Subject: Rewrite of most components. Simplify `ser` logic, and expand `de` functionality. --- src/ser/part.rs | 232 -------------------------------------------------------- 1 file changed, 232 deletions(-) delete mode 100644 src/ser/part.rs (limited to 'src/ser/part.rs') diff --git a/src/ser/part.rs b/src/ser/part.rs deleted file mode 100644 index ad2d9dd..0000000 --- a/src/ser/part.rs +++ /dev/null @@ -1,232 +0,0 @@ -use dtoa; -use itoa; -use ser::Error; -use serde; -use serde::ser; -use std::marker::PhantomData; -use std::str; - -pub struct PartSerializer> { - sink: S, - marker: PhantomData, -} - -impl> PartSerializer { - pub fn new(sink: S) -> Self { - PartSerializer { - sink: sink, - marker: PhantomData, - } - } -} - -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; - - 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; - - fn unsupported(&self) -> Error; -} - -impl> ser::Serializer for PartSerializer { - type Ok = SO; - type Error = Error; - 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; - - fn serialize_bool(self, v: bool) -> Result { - self.sink.serialize_static_str(if v { "true" } else { "false" }) - } - - fn serialize_i8(self, v: i8) -> Result { - self.serialize_integer(v) - } - - fn serialize_i16(self, v: i16) -> Result { - self.serialize_integer(v) - } - - fn serialize_i32(self, v: i32) -> Result { - self.serialize_integer(v) - } - - fn serialize_i64(self, v: i64) -> Result { - self.serialize_integer(v) - } - - fn serialize_u8(self, v: u8) -> Result { - self.serialize_integer(v) - } - - fn serialize_u16(self, v: u16) -> Result { - self.serialize_integer(v) - } - - fn serialize_u32(self, v: u32) -> Result { - self.serialize_integer(v) - } - - fn serialize_u64(self, v: u64) -> Result { - self.serialize_integer(v) - } - - fn serialize_f32(self, v: f32) -> Result { - self.serialize_floating(v) - } - - fn serialize_f64(self, v: f64) -> Result { - self.serialize_floating(v) - } - - fn serialize_char(self, v: char) -> Result { - self.sink.serialize_string(v.to_string()) - } - - fn serialize_str(self, value: &str) -> Result { - self.sink.serialize_str(value) - } - - 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 { - Err(self.sink.unsupported()) - } - - fn serialize_unit_struct(self, name: &'static str) -> Result { - self.sink.serialize_static_str(name.into()) - } - - fn serialize_unit_variant(self, - _name: &'static str, - _variant_index: u32, - variant: &'static str) - -> Result { - self.sink.serialize_static_str(variant.into()) - } - - fn serialize_newtype_struct - (self, - _name: &'static str, - value: &T) - -> Result { - value.serialize(self) - } - - fn serialize_newtype_variant - (self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _value: &T) - -> Result { - Err(self.sink.unsupported()) - } - - fn serialize_none(self) -> Result { - self.sink.serialize_none() - } - - fn serialize_some(self, - value: &T) - -> Result { - self.sink.serialize_some(value) - } - - fn serialize_seq(self, - _len: Option) - -> Result { - Ok(self.sink) - } - - fn serialize_tuple(self, - _len: usize) - -> Result { - Err(self.sink.unsupported()) - } - - fn serialize_tuple_struct(self, - _name: &'static str, - _len: usize) - -> Result { - Err(self.sink.unsupported()) - } - - fn serialize_tuple_variant - (self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _len: usize) - -> Result { - Err(self.sink.unsupported()) - } - - fn serialize_map(self, - _len: Option) - -> Result { - Err(self.sink.unsupported()) - } - - fn serialize_struct(self, - _name: &'static str, - _len: usize) - -> Result { - // Err(self.sink.unsupported()) - Ok(self.sink) - - } - - fn serialize_struct_variant - (self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _len: usize) - -> Result { - Err(self.sink.unsupported()) - } -} - -impl> PartSerializer { - fn serialize_integer(self, value: I) -> Result - where I: itoa::Integer, - { - let mut buf = [b'\0'; 20]; - let len = itoa::write(&mut buf[..], value).unwrap(); - let part = unsafe { str::from_utf8_unchecked(&buf[0..len]) }; - ser::Serializer::serialize_str(self, part) - } - - fn serialize_floating(self, value: F) -> Result - where F: dtoa::Floating, - { - let mut buf = [b'\0'; 24]; - let len = dtoa::write(&mut buf[..], value).unwrap(); - let part = unsafe { str::from_utf8_unchecked(&buf[0..len]) }; - ser::Serializer::serialize_str(self, part) - } -} -- cgit v1.2.3