diff options
author | bors-ng[bot] <bors-ng[bot]@users.noreply.github.com> | 2017-01-29 09:30:09 +0000 |
---|---|---|
committer | bors-ng[bot] <bors-ng[bot]@users.noreply.github.com> | 2017-01-29 09:30:09 +0000 |
commit | 2cc32847ac45bee7d0b60020edda3cce7a07b4ce (patch) | |
tree | 49a0fc14c3ed50947c2d2f771998cdf38c3948a2 /src | |
parent | 9aefb1c02d8cb5746f07dc6ac461ce8eedced5e3 (diff) | |
parent | 3e9ebe4fd7428da26dd75e2cc6e5025c847c99e6 (diff) |
Merge #10
10: Update to serde 0.9.0 and various improvements
Diffstat (limited to 'src')
-rw-r--r-- | src/de.rs | 45 | ||||
-rw-r--r-- | src/lib.rs | 3 | ||||
-rw-r--r-- | src/ser/key.rs | 312 | ||||
-rw-r--r-- | src/ser/mod.rs | 531 | ||||
-rw-r--r-- | src/ser/pair.rs | 362 | ||||
-rw-r--r-- | src/ser/part.rs | 229 | ||||
-rw-r--r-- | src/ser/value.rs | 333 | ||||
-rw-r--r-- | src/ser/void.rs | 122 |
8 files changed, 875 insertions, 1062 deletions
@@ -1,13 +1,12 @@ //! Deserialization support for the `application/x-www-form-urlencoded` format. use serde::de; + +pub use serde::de::value::Error; use serde::de::value::MapDeserializer; -use std::borrow::Cow; use url::form_urlencoded::Parse as UrlEncodedParse; use url::form_urlencoded::parse; -pub use serde::de::value::Error; - /// Deserializes a `application/x-wwww-url-encoded` value from a `&[u8]`. /// /// ``` @@ -24,7 +23,7 @@ pub use serde::de::value::Error; /// Ok(meal)); /// ``` pub fn from_bytes<T: de::Deserialize>(input: &[u8]) -> Result<T, Error> { - T::deserialize(&mut Deserializer::new(parse(input))) + T::deserialize(Deserializer::new(parse(input))) } /// Deserializes a `application/x-wwww-url-encoded` value from a `&str`. @@ -56,61 +55,52 @@ pub fn from_str<T: de::Deserialize>(input: &str) -> Result<T, Error> { /// * Everything else but `deserialize_seq` and `deserialize_seq_fixed_size` /// defers to `deserialize`. pub struct Deserializer<'a> { - inner: - MapDeserializer<UrlEncodedParse<'a>, Cow<'a, str>, Cow<'a, str>, Error>, + inner: MapDeserializer<UrlEncodedParse<'a>, Error>, } impl<'a> Deserializer<'a> { /// Returns a new `Deserializer`. pub fn new(parser: UrlEncodedParse<'a>) -> Self { - Deserializer { inner: MapDeserializer::unbounded(parser) } + Deserializer { inner: MapDeserializer::new(parser) } } } -impl<'a> de::Deserializer for Deserializer<'a> -{ +impl<'a> de::Deserializer for Deserializer<'a> { type Error = Error; - fn deserialize<V>( - &mut self, visitor: V) - -> Result<V::Value, Self::Error> + fn deserialize<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: de::Visitor, { self.deserialize_map(visitor) } - fn deserialize_map<V>( - &mut self, mut visitor: V) - -> Result<V::Value, Self::Error> + fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: de::Visitor, { - visitor.visit_map(&mut self.inner) + visitor.visit_map(self.inner) } - fn deserialize_seq<V>( - &mut self, mut visitor: V) - -> Result<V::Value, Self::Error> + fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: de::Visitor, { - visitor.visit_seq(&mut self.inner) + visitor.visit_seq(self.inner) } - fn deserialize_seq_fixed_size<V>( - &mut self, _len: usize, mut visitor: V) - -> Result<V::Value, Self::Error> - where V: de::Visitor + fn deserialize_seq_fixed_size<V>(self, + _len: usize, + visitor: V) + -> Result<V::Value, Self::Error> + where V: de::Visitor, { - visitor.visit_seq(&mut self.inner) + visitor.visit_seq(self.inner) } forward_to_deserialize! { bool - usize u8 u16 u32 u64 - isize i8 i16 i32 @@ -123,6 +113,7 @@ impl<'a> de::Deserializer for Deserializer<'a> unit option bytes + byte_buf unit_struct newtype_struct tuple_struct @@ -1,8 +1,11 @@ //! `x-www-form-urlencoded` meets Serde +extern crate itoa; +extern crate dtoa; #[macro_use] extern crate serde; extern crate url; +extern crate void; pub mod de; pub mod ser; diff --git a/src/ser/key.rs b/src/ser/key.rs index a26c563..6cbbff4 100644 --- a/src/ser/key.rs +++ b/src/ser/key.rs @@ -1,292 +1,76 @@ -use serde::{Serialize, Serializer}; use ser::Error; +use ser::part::Sink; +use serde::Serialize; use std::borrow::Cow; -use std::str; +use std::ops::Deref; -pub struct MapKeySerializer<'key>(&'key mut Option<Cow<'static, str>>); - -impl<'key> MapKeySerializer<'key> { - pub fn new(output: &'key mut Option<Cow<'static, str>>) -> Self { - MapKeySerializer(output) - } - - fn set_key<T>(&mut self, key: T) -> Result<(), Error> - where T: Into<Cow<'static, str>> - { - *self.0 = Some(key.into()); - Ok(()) - } +pub enum Key<'key> { + Static(&'static str), + Dynamic(Cow<'key, str>), } -impl<'key> Serializer for MapKeySerializer<'key> { - type Error = Error; - type SeqState = (); - type TupleState = (); - type TupleStructState = (); - type TupleVariantState = (); - type MapState = (); - type StructState = (); - type StructVariantState = (); - - fn serialize_bool(&mut self, _v: bool) -> Result<(), Error> { - Err(Error::unsupported_key()) - } - - fn serialize_isize(&mut self, v: isize) -> Result<(), Error> { - self.set_key(v.to_string()) - } - - fn serialize_i8(&mut self, v: i8) -> Result<(), Error> { - self.set_key(v.to_string()) - } - - fn serialize_i16(&mut self, v: i16) -> Result<(), Error> { - self.set_key(v.to_string()) - } - - fn serialize_i32(&mut self, v: i32) -> Result<(), Error> { - self.set_key(v.to_string()) - } - - fn serialize_i64(&mut self, v: i64) -> Result<(), Error> { - self.set_key(v.to_string()) - } - - fn serialize_usize(&mut self, v: usize) -> Result<(), Error> { - self.set_key(v.to_string()) - } - - fn serialize_u8(&mut self, v: u8) -> Result<(), Error> { - self.set_key(v.to_string()) - } - - fn serialize_u16(&mut self, v: u16) -> Result<(), Error> { - self.set_key(v.to_string()) - } - - fn serialize_u32(&mut self, v: u32) -> Result<(), Error> { - self.set_key(v.to_string()) - } - - fn serialize_u64(&mut self, v: u64) -> Result<(), Error> { - self.set_key(v.to_string()) - } - - fn serialize_f32(&mut self, v: f32) -> Result<(), Error> { - self.set_key(v.to_string()) - } - - fn serialize_f64(&mut self, v: f64) -> Result<(), Error> { - self.set_key(v.to_string()) - } - - fn serialize_char(&mut self, v: char) -> Result<(), Error> { - self.set_key(v.to_string()) - } +impl<'key> Deref for Key<'key> { + type Target = str; - fn serialize_str(&mut self, value: &str) -> Result<(), Error> { - self.set_key(String::from(value)) - } - - fn serialize_bytes(&mut self, value: &[u8]) -> Result<(), Error> { - match str::from_utf8(value) { - Ok(value) => self.set_key(String::from(value)), - Err(err) => Err(Error::Utf8(err)), + fn deref(&self) -> &str { + match *self { + Key::Static(key) => key, + Key::Dynamic(ref key) => key, } } +} - fn serialize_unit(&mut self) -> Result<(), Error> { - Err(Error::unsupported_key()) - } - - fn serialize_unit_struct( - &mut self, name: &'static str) - -> Result<(), Error> { - self.set_key(name) - } - - fn serialize_unit_variant( - &mut self, - _name: &'static str, - _variant_index: usize, - variant: &'static str) - -> Result<(), Error> { - self.set_key(variant) - } - - fn serialize_newtype_struct<T>( - &mut self, _name: &'static str, value: T) - -> Result<(), Error> - where T: Serialize - { - value.serialize(self) - } - - fn serialize_newtype_variant<T>( - &mut self, - _name: &'static str, - _variant_index: usize, - _variant: &'static str, - _value: T) - -> Result<(), Error> - where T: Serialize - { - Err(Error::unsupported_key()) - } - - fn serialize_none(&mut self) -> Result<(), Error> { - Err(Error::unsupported_key()) - } - - fn serialize_some<T>(&mut self, _value: T) -> Result<(), Error> - where T: Serialize - { - Err(Error::unsupported_key()) - } - - fn serialize_seq(&mut self, _len: Option<usize>) -> Result<(), Error> { - Err(Error::unsupported_key()) - } - - fn serialize_seq_elt<T>( - &mut self, _state: &mut (), _value: T) - -> Result<(), Error> - where T: Serialize - { - Err(Error::unsupported_key()) - } - - fn serialize_seq_end(&mut self, _state: ()) -> Result<(), Error> { - Err(Error::unsupported_key()) - } - - fn serialize_seq_fixed_size(&mut self, _size: usize) -> Result<(), Error> { - Err(Error::unsupported_key()) - } - - fn serialize_tuple(&mut self, _len: usize) -> Result<(), Error> { - Err(Error::unsupported_key()) - } - - fn serialize_tuple_elt<T>( - &mut self, _state: &mut (), _value: T) - -> Result<(), Error> - where T: Serialize - { - Err(Error::unsupported_key()) - } - - fn serialize_tuple_end(&mut self, _state: ()) -> Result<(), Error> { - Err(Error::unsupported_key()) - } - - fn serialize_tuple_struct( - &mut self, _name: &'static str, _len: usize) - -> Result<(), Error> { - Err(Error::unsupported_key()) - } - - fn serialize_tuple_struct_elt<T>( - &mut self, _state: &mut (), _value: T) - -> Result<(), Error> - where T: Serialize - { - Err(Error::unsupported_key()) - } - - fn serialize_tuple_struct_end(&mut self, _state: ()) -> Result<(), Error> { - Err(Error::unsupported_key()) - } - - fn serialize_tuple_variant( - &mut self, - _name: &'static str, - _variant_index: usize, - _variant: &'static str, - _len: usize) - -> Result<(), Error> { - Err(Error::unsupported_key()) - } - - fn serialize_tuple_variant_elt<T>( - &mut self, _state: &mut (), _value: T) - -> Result<(), Error> - where T: Serialize - { - Err(Error::unsupported_key()) - } - - fn serialize_tuple_variant_end(&mut self, _state: ()) -> Result<(), Error> { - Err(Error::unsupported_key()) - } - - fn serialize_map(&mut self, _len: Option<usize>) -> Result<(), Error> { - Err(Error::unsupported_key()) - } - - fn serialize_map_key<T>( - &mut self, _state: &mut (), _key: T) - -> Result<(), Error> - where T: Serialize - { - Err(Error::unsupported_key()) +impl<'key> From<Key<'key>> for Cow<'static, str> { + fn from(key: Key<'key>) -> Self { + match key { + Key::Static(key) => key.into(), + Key::Dynamic(key) => key.into_owned().into(), + } } +} - fn serialize_map_value<T>( - &mut self, _state: &mut (), _value: T) - -> Result<(), Error> - where T: Serialize - { - Err(Error::unsupported_key()) - } +pub struct KeySink<End> { + end: End, +} - fn serialize_map_end(&mut self, _state: ()) -> Result<(), Error> { - Err(Error::unsupported_key()) +impl<End, Ok> KeySink<End> + where End: for<'key> FnOnce(Key<'key>) -> Result<Ok, Error> +{ + pub fn new(end: End) -> Self { + KeySink { end: end } } +} - fn serialize_struct( - &mut self, _name: &'static str, _len: usize) - -> Result<(), Error> { - Err(Error::unsupported_key()) - } +impl<End, Ok> Sink for KeySink<End> + where End: for<'key> FnOnce(Key<'key>) -> Result<Ok, Error> +{ + type Ok = Ok; - fn serialize_struct_elt<T>( - &mut self, _state: &mut (), _key: &'static str, _value: T) - -> Result<(), Error> - where T: Serialize - { - Err(Error::unsupported_key()) + fn serialize_static_str(self, + value: &'static str) + -> Result<Ok, Error> { + (self.end)(Key::Static(value)) } - fn serialize_struct_end(&mut self, _state: ()) -> Result<(), Error> { - Err(Error::unsupported_key()) + fn serialize_str(self, value: &str) -> Result<Ok, Error> { + (self.end)(Key::Dynamic(value.into())) } - fn serialize_struct_variant( - &mut self, - _name: &'static str, - _variant_index: usize, - _variant: &'static str, - _len: usize) - -> Result<(), Error> { - Err(Error::unsupported_key()) + fn serialize_string(self, value: String) -> Result<Ok, Error> { + (self.end)(Key::Dynamic(value.into())) } - fn serialize_struct_variant_elt<T>( - &mut self, _state: &mut (), _key: &'static str, _value: T) - -> Result<(), Error> { - Err(Error::unsupported_key()) + fn serialize_none(self) -> Result<Ok, Error> { + Err(self.unsupported()) } - fn serialize_struct_variant_end( - &mut self, _state: ()) - -> Result<(), Error> { - Err(Error::unsupported_key()) + fn serialize_some<T: ?Sized + Serialize>(self, + _value: &T) + -> Result<Ok, Error> { + Err(self.unsupported()) } -} -impl Error { - fn unsupported_key() -> Self { + fn unsupported(self) -> Error { Error::Custom("unsupported key".into()) } } diff --git a/src/ser/mod.rs b/src/ser/mod.rs index f04c956..50355fd 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -2,7 +2,9 @@ mod key; mod pair; +mod part; mod value; +mod void; use serde::ser; use std::borrow::Cow; @@ -27,12 +29,9 @@ use url::form_urlencoded::Target as UrlEncodedTarget; /// Ok("bread=baguette&cheese=comt%C3%A9&meat=ham&fat=butter".to_owned())); /// ``` pub fn to_string<T: ser::Serialize>(input: &T) -> Result<String, Error> { - let mut output = String::new(); - { - let mut urlencoder = UrlEncodedSerializer::new(&mut output); - try!(input.serialize(&mut Serializer::new(&mut urlencoder))); - } - Ok(output) + let mut urlencoder = UrlEncodedSerializer::new("".to_owned()); + input.serialize(Serializer::new(&mut urlencoder))?; + Ok(urlencoder.finish()) } /// A serializer for the `application/x-www-form-urlencoded` format. @@ -44,13 +43,13 @@ pub fn to_string<T: ser::Serialize>(input: &T) -> Result<String, Error> { /// unit structs and unit variants. /// /// * Newtype structs defer to their inner values. -pub struct Serializer<'output, T: 'output + UrlEncodedTarget> { - urlencoder: &'output mut UrlEncodedSerializer<T> +pub struct Serializer<'output, Target: 'output + UrlEncodedTarget> { + urlencoder: &'output mut UrlEncodedSerializer<Target>, } -impl<'output, T: 'output + UrlEncodedTarget> Serializer<'output, T> { +impl<'output, Target: 'output + UrlEncodedTarget> Serializer<'output, Target> { /// Returns a new `Serializer`. - pub fn new(urlencoder: &'output mut UrlEncodedSerializer<T>) -> Self { + pub fn new(urlencoder: &'output mut UrlEncodedSerializer<Target>) -> Self { Serializer { urlencoder: urlencoder } } } @@ -59,15 +58,13 @@ impl<'output, T: 'output + UrlEncodedTarget> Serializer<'output, T> { #[derive(Clone, Debug, PartialEq, Eq)] pub enum Error { Custom(Cow<'static, str>), - InvalidValue(Cow<'static, str>), Utf8(str::Utf8Error), } impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { Error::Custom(ref msg) => msg.fmt(f), - Error::InvalidValue(ref msg) => write!(f, "invalid value: {}", msg), Error::Utf8(ref err) => write!(f, "invalid UTF-8: {}", err), } } @@ -77,7 +74,6 @@ impl error::Error for Error { fn description(&self) -> &str { match *self { Error::Custom(ref msg) => msg, - Error::InvalidValue(ref msg) => msg, Error::Utf8(ref err) => error::Error::description(err), } } @@ -85,409 +81,432 @@ impl error::Error for Error { /// The lower-level cause of this error, in the case of a `Utf8` error. fn cause(&self) -> Option<&error::Error> { match *self { - Error::Custom(_) | Error::InvalidValue(_) => None, + Error::Custom(_) => None, Error::Utf8(ref err) => Some(err), } } } impl ser::Error for Error { - fn custom<T: Into<String>>(msg: T) -> Self { - Error::Custom(msg.into().into()) - } - - fn invalid_value(msg: &str) -> Self { - Error::InvalidValue(String::from(msg).into()) + fn custom<T: fmt::Display>(msg: T) -> Self { + Error::Custom(format!("{}", msg).into()) } } -/// State used when serializing sequences. -pub struct SeqState { - _state: (), +/// Sequence serializer. +pub struct SeqSerializer<'output, Target: 'output + UrlEncodedTarget> { + urlencoder: &'output mut UrlEncodedSerializer<Target>, } -/// State used when serializing tuples. -pub struct TupleState { - _state: (), +/// Tuple serializer. +/// +/// Never instantiated, tuples are not supported at top-level. +pub struct TupleSerializer<'output, T: 'output + UrlEncodedTarget> { + inner: void::VoidSerializer<&'output mut UrlEncodedSerializer<T>>, } -/// State used when serializing tuple structs. -pub struct TupleStructState { - _state: (), +/// Tuple struct serializer. +/// +/// Never instantiated, tuple structs are not supported. +pub struct TupleStructSerializer<'output, T: 'output + UrlEncodedTarget> { + inner: void::VoidSerializer<&'output mut UrlEncodedSerializer<T>>, } -/// State used when serializing tuple variants. -pub struct TupleVariantState { - _state: (), +/// Tuple variant serializer. +/// +/// Never instantiated, tuple variants are not supported. +pub struct TupleVariantSerializer<'output, T: 'output + UrlEncodedTarget> { + inner: void::VoidSerializer<&'output mut UrlEncodedSerializer<T>>, } -/// State used when serializing maps. -pub struct MapState { - key: Option<Cow<'static, str>> +/// Map serializer. +pub struct MapSerializer<'output, Target: 'output + UrlEncodedTarget> { + urlencoder: &'output mut UrlEncodedSerializer<Target>, + key: Option<Cow<'static, str>>, } -/// State used when serializing structs. -pub struct StructState { - _state: (), +/// Struct serializer. +pub struct StructSerializer<'output, Target: 'output + UrlEncodedTarget> { + urlencoder: &'output mut UrlEncodedSerializer<Target>, } -/// State used when serializing struct variants. -pub struct StructVariantState { - _state: (), +/// Struct variant serializer. +/// +/// Never instantiated, struct variants are not supported. +pub struct StructVariantSerializer<'output, T: 'output + UrlEncodedTarget> { + inner: void::VoidSerializer<&'output mut UrlEncodedSerializer<T>>, } impl<'output, Target> ser::Serializer for Serializer<'output, Target> - where Target: 'output + UrlEncodedTarget + where Target: 'output + UrlEncodedTarget, { + type Ok = &'output mut UrlEncodedSerializer<Target>; type Error = Error; - - /// State used when serializing sequences. - type SeqState = SeqState; - - /// State used when serializing tuples. - type TupleState = TupleState; - - /// State used when serializing tuple structs. - type TupleStructState = TupleStructState; - - /// State used when serializing tuple variants. - type TupleVariantState = TupleVariantState; - - /// State used when serializing maps. - type MapState = MapState; - - /// State used when serializing structs. - type StructState = StructState; - - /// State used when serializing struct variants. - type StructVariantState = StructVariantState; - - /// Returns an error. - fn serialize_bool(&mut self, _v: bool) -> Result<(), Error> { - Err(Error::top_level()) - } - - /// Returns an error. - fn serialize_isize(&mut self, _v: isize) -> Result<(), Error> { - Err(Error::top_level()) - } + type SerializeSeq = SeqSerializer<'output, Target>; + type SerializeTuple = TupleSerializer<'output, Target>; + type SerializeTupleStruct = TupleStructSerializer<'output, Target>; + type SerializeTupleVariant = TupleVariantSerializer<'output, Target>; + type SerializeMap = MapSerializer<'output, Target>; + type SerializeStruct = StructSerializer<'output, Target>; + type SerializeStructVariant = StructVariantSerializer<'output, Target>; /// Returns an error. - fn serialize_i8(&mut self, _v: i8) -> Result<(), Error> { + fn serialize_bool(self, _v: bool) -> Result<Self::Ok, Error> { Err(Error::top_level()) } /// Returns an error. - fn serialize_i16(&mut self, _v: i16) -> Result<(), Error> { + fn serialize_i8(self, _v: i8) -> Result<Self::Ok, Error> { Err(Error::top_level()) } /// Returns an error. - fn serialize_i32(&mut self, _v: i32) -> Result<(), Error> { + fn serialize_i16(self, _v: i16) -> Result<Self::Ok, Error> { Err(Error::top_level()) } /// Returns an error. - fn serialize_i64(&mut self, _v: i64) -> Result<(), Error> { + fn serialize_i32(self, _v: i32) -> Result<Self::Ok, Error> { Err(Error::top_level()) } /// Returns an error. - fn serialize_usize(&mut self, _v: usize) -> Result<(), Error> { + fn serialize_i64(self, _v: i64) -> Result<Self::Ok, Error> { Err(Error::top_level()) } /// Returns an error. - fn serialize_u8(&mut self, _v: u8) -> Result<(), Error> { + fn serialize_u8(self, _v: u8) -> Result<Self::Ok, Error> { Err(Error::top_level()) } /// Returns an error. - fn serialize_u16(&mut self, _v: u16) -> Result<(), Error> { + fn serialize_u16(self, _v: u16) -> Result<Self::Ok, Error> { Err(Error::top_level()) } /// Returns an error. - fn serialize_u32(&mut self, _v: u32) -> Result<(), Error> { + fn serialize_u32(self, _v: u32) -> Result<Self::Ok, Error> { Err(Error::top_level()) } /// Returns an error. - fn serialize_u64(&mut self, _v: u64) -> Result<(), Error> { + fn serialize_u64(self, _v: u64) -> Result<Self::Ok, Error> { Err(Error::top_level()) } /// Returns an error. - fn serialize_f32(&mut self, _v: f32) -> Result<(), Error> { + fn serialize_f32(self, _v: f32) -> Result<Self::Ok, Error> { Err(Error::top_level()) } /// Returns an error. - fn serialize_f64(&mut self, _v: f64) -> Result<(), Error> { + fn serialize_f64(self, _v: f64) -> Result<Self::Ok, Error> { Err(Error::top_level()) } /// Returns an error. - fn serialize_char(&mut self, _v: char) -> Result<(), Error> { + fn serialize_char(self, _v: char) -> Result<Self::Ok, Error> { Err(Error::top_level()) } /// Returns an error. - fn serialize_str(&mut self, _value: &str) -> Result<(), Error> { + fn serialize_str(self, _value: &str) -> Result<Self::Ok, Error> { Err(Error::top_level()) } /// Returns an error. - fn serialize_bytes(&mut self, _value: &[u8]) -> Result<(), Error> { + fn serialize_bytes(self, _value: &[u8]) -> Result<Self::Ok, Error> { Err(Error::top_level()) } /// Returns an error. - fn serialize_unit(&mut self) -> Result<(), Error> { + fn serialize_unit(self) -> Result<Self::Ok, Error> { Err(Error::top_level()) } /// Returns an error. - fn serialize_unit_struct( - &mut self, _name: &'static str) - -> Result<(), Error> { + fn serialize_unit_struct(self, + _name: &'static str) + -> Result<Self::Ok, Error> { Err(Error::top_level()) } /// Returns an error. - fn serialize_unit_variant( - &mut self, - _name: &'static str, - _variant_index: usize, - _variant: &'static str) - -> Result<(), Error> { + fn serialize_unit_variant(self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str) + -> Result<Self::Ok, Error> { Err(Error::top_level()) } /// Serializes the inner value, ignoring the newtype name. - fn serialize_newtype_struct<T>( - &mut self, _name: &'static str, value: T) - -> Result<(), Error> - where T: ser::Serialize - { + fn serialize_newtype_struct<T: ?Sized + ser::Serialize> + (self, + _name: &'static str, + value: &T) + -> Result<Self::Ok, Error> { value.serialize(self) } /// Returns an error. - fn serialize_newtype_variant<T>( - &mut self, - _name: &'static str, - _variant_index: usize, - _variant: &'static str, - _value: T) - -> Result<(), Error> - where T: ser::Serialize - { + fn serialize_newtype_variant<T: ?Sized + ser::Serialize> + (self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _value: &T) + -> Result<Self::Ok, Error> { Err(Error::top_level()) } - /// Returns an error. - fn serialize_none(&mut self) -> Result<(), Error> { - Ok(()) + /// Returns `Ok`. + fn serialize_none(self) -> Result<Self::Ok, Error> { + Ok(self.urlencoder) } - /// Returns an error. - fn serialize_some<T>(&mut self, value: T) -> Result<(), Error> - where T: ser::Serialize - { + /// Serializes the given value. + fn serialize_some<T: ?Sized + ser::Serialize> + (self, + value: &T) + -> Result<Self::Ok, Error> { value.serialize(self) } - /// Begins to serialize a sequence, given length (if any) is ignored. - fn serialize_seq( - &mut self, _len: Option<usize>) - -> Result<SeqState, Error> { - Ok(SeqState { _state: () }) + /// Serialize a sequence, given length (if any) is ignored. + fn serialize_seq(self, + _len: Option<usize>) + -> Result<Self::SerializeSeq, Error> { + Ok(SeqSerializer { urlencoder: self.urlencoder }) } - /// Serializes a sequence element. - fn serialize_seq_elt<T>( - &mut self, _state: &mut SeqState, value: T) - -> Result<(), Error> - where T: ser::Serialize - { - value.serialize(&mut pair::PairSerializer::new(self.urlencoder)) - } - - /// Finishes serializing a sequence. - fn serialize_seq_end(&mut self, _state: SeqState) -> Result<(), Error> { - Ok(()) - } - - /// Begins to serialize a sequence, given length is ignored. - fn serialize_seq_fixed_size( - &mut self, _length: usize) - -> Result<SeqState, Error> { - Ok(SeqState { _state: () }) + /// Serializes a sequence, given length is ignored. + fn serialize_seq_fixed_size(self, + _len: usize) + -> Result<Self::SerializeSeq, Error> { + Ok(SeqSerializer { urlencoder: self.urlencoder }) } /// Returns an error. - fn serialize_tuple(&mut self, _len: usize) -> Result<TupleState, Error> { + fn serialize_tuple(self, + _len: usize) + -> Result<Self::SerializeTuple, Error> { Err(Error::top_level()) } /// Returns an error. - fn serialize_tuple_elt<T>( - &mut self, _state: &mut TupleState, _value: T) - -> Result<(), Error> - where T: ser::Serialize - { + fn serialize_tuple_struct(self, + _name: &'static str, + _len: usize) + -> Result<Self::SerializeTupleStruct, Error> { Err(Error::top_level()) } /// Returns an error. - fn serialize_tuple_end(&mut self, _state: TupleState) -> Result<(), Error> { + fn serialize_tuple_variant + (self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _len: usize) + -> Result<Self::SerializeTupleVariant, Error> { Err(Error::top_level()) } - /// Returns an error. - fn serialize_tuple_struct( - &mut self, _name: &'static str, _len: usize) - -> Result<TupleStructState, Error> { - Err(Error::top_level()) + /// Serializes a map, given length is ignored. + fn serialize_map(self, + _len: Option<usize>) + -> Result<Self::SerializeMap, Error> { + Ok(MapSerializer { + urlencoder: self.urlencoder, + key: None, + }) } - /// Returns an error. - fn serialize_tuple_struct_elt<T>( - &mut self, _state: &mut TupleStructState, _value: T) - -> Result<(), Error> - where T: ser::Serialize - { - Err(Error::top_level()) + /// Serializes a struct, given length is ignored. + fn serialize_struct(self, + _name: &'static str, + _len: usize) + -> Result<Self::SerializeStruct, Error> { + Ok(StructSerializer { urlencoder: self.urlencoder }) } /// Returns an error. - fn serialize_tuple_struct_end( - &mut self, _state: TupleStructState) - -> Result<(), Error> - { + fn serialize_struct_variant + (self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _len: usize) + -> Result<Self::SerializeStructVariant, Error> { Err(Error::top_level()) } +} - /// Returns an error. - fn serialize_tuple_variant( - &mut self, - _name: &'static str, - _variant_index: usize, - _variant: &'static str, - _len: usize) - -> Result<TupleVariantState, Error> { - Err(Error::top_level()) +impl<'output, Target> ser::SerializeSeq for SeqSerializer<'output, Target> + where Target: 'output + UrlEncodedTarget, +{ + type Ok = &'output mut UrlEncodedSerializer<Target>; + type Error = Error; + + fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, + value: &T) + -> Result<(), Error> { + value.serialize(pair::PairSerializer::new(self.urlencoder)) } - /// Returns an error. - fn serialize_tuple_variant_elt<T>( - &mut self, _state: &mut TupleVariantState, _value: T) - -> Result<(), Error> - where T: ser::Serialize - { - Err(Error::top_level()) + fn end(self) -> Result<Self::Ok, Error> { + Ok(self.urlencoder) } +} - /// Returns an error. - fn serialize_tuple_variant_end( - &mut self, _state: TupleVariantState) - -> Result<(), Error> { - Err(Error::top_level()) +impl<'output, Target> ser::SerializeTuple for TupleSerializer<'output, Target> + where Target: 'output + UrlEncodedTarget, +{ + type Ok = &'output mut UrlEncodedSerializer<Target>; + type Error = Error; + + fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, + value: &T) + -> Result<(), Error> { + self.inner.serialize_element(value) } - /// Begins to serialize a map, given length (if any) is ignored. - fn serialize_map( - &mut self, _len: Option<usize>) - -> Result<MapState, Error> { - Ok(MapState { key: None }) + fn end(self) -> Result<Self::Ok, Error> { + self.inner.end() } +} - /// Serializes a map key. - fn serialize_map_key<T>( - &mut self, state: &mut MapState, key: T) - -> Result<(), Error> - where T: ser::Serialize - { - key.serialize(&mut key::MapKeySerializer::new(&mut state.key)) +impl<'output, Target> ser::SerializeTupleStruct + for TupleStructSerializer<'output, Target> + where Target: 'output + UrlEncodedTarget, +{ + type Ok = &'output mut UrlEncodedSerializer<Target>; + type Error = Error; + + fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, + value: &T) + -> Result<(), Error> { + self.inner.serialize_field(value) } - /// Serializes a map value. - fn serialize_map_value<T>( - &mut self, state: &mut MapState, value: T) - -> Result<(), Error> - where T: ser::Serialize - { - let mut value_serializer = - try!(value::ValueSerializer::new(&mut state.key, self.urlencoder)); - value.serialize(&mut value_serializer) + fn end(self) -> Result<Self::Ok, Error> { + self.inner.end() } +} - /// Finishes serializing a map. - fn serialize_map_end(&mut self, _state: MapState) -> Result<(), Error> { - Ok(()) +impl<'output, Target> ser::SerializeTupleVariant + for TupleVariantSerializer<'output, Target> + where Target: 'output + UrlEncodedTarget, +{ + type Ok = &'output mut UrlEncodedSerializer<Target>; + type Error = Error; + + fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, + value: &T) + -> Result<(), Error> { + self.inner.serialize_field(value) } - /// Begins to serialize a struct, given length is ignored. - fn serialize_struct( - &mut self, _name: &'static str, _len: usize) - -> Result<StructState, Error> { - Ok(StructState { _state: () }) + fn end(self) -> Result<Self::Ok, Error> { + self.inner.end() } +} - /// Serializes a struct element. - fn serialize_struct_elt<T>( - &mut self, - _state: &mut StructState, - key: &'static str, - value: T) - -> Result<(), Error> - where T: ser::Serialize - { - let mut key = Some(key.into()); - let mut value_serializer = - value::ValueSerializer::new(&mut key, self.urlencoder).unwrap(); - value.serialize(&mut value_serializer) +impl<'output, Target> ser::SerializeMap for MapSerializer<'output, Target> + where Target: 'output + UrlEncodedTarget, +{ + type Ok = &'output mut UrlEncodedSerializer<Target>; + type Error = Error; + + fn serialize_entry<K: ?Sized + ser::Serialize, V: ?Sized + ser::Serialize> + (&mut self, + key: &K, + value: &V) + -> Result<(), Error> { + let key_sink = key::KeySink::new(|key| { + let value_sink = value::ValueSink::new(self.urlencoder, &key); + value.serialize(part::PartSerializer::new(value_sink))?; + self.key = None; + Ok(()) + }); + let entry_serializer = part::PartSerializer::new(key_sink); + key.serialize(entry_serializer) + } + + fn serialize_key<T: ?Sized + ser::Serialize>(&mut self, + key: &T) + -> Result<(), Error> { + let key_sink = key::KeySink::new(|key| Ok(key.into())); + let key_serializer = part::PartSerializer::new(key_sink); + self.key = Some(key.serialize(key_serializer)?); + Ok(()) } - /// Finishes serializing a struct. - fn serialize_struct_end(&mut self, _state: StructState) - -> Result<(), Error> { + fn serialize_value<T: ?Sized + ser::Serialize>(&mut self, + value: &T) + -> Result<(), Error> { + { + let key = self.key.as_ref().ok_or_else(|| Error::no_key())?; + let value_sink = value::ValueSink::new(self.urlencoder, &key); + value.serialize(part::PartSerializer::new(value_sink))?; + } + self.key = None; Ok(()) } - /// Returns an error. - fn serialize_struct_variant( - &mut self, - _name: &'static str, - _variant_index: usize, - _variant: &'static str, - _len: usize) - -> Result<StructVariantState, Error> { - Err(Error::top_level()) + fn end(self) -> Result<Self::Ok, Error> { + Ok(self.urlencoder) } +} - /// Returns an error. - fn serialize_struct_variant_elt<T>( - &mut self, - _state: &mut StructVariantState, - _key: &'static str, - _value: T) - -> Result<(), Error> { - Err(Error::top_level()) +impl<'output, Target> ser::SerializeStruct for StructSerializer<'output, Target> + where Target: 'output + UrlEncodedTarget, +{ + type Ok = &'output mut UrlEncodedSerializer<Target>; + type Error = Error; + + fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, + key: &'static str, + value: &T) + -> Result<(), Error> { + let value_sink = value::ValueSink::new(self.urlencoder, key); + value.serialize(part::PartSerializer::new(value_sink)) } - /// Returns an error. - fn serialize_struct_variant_end( - &mut self, _state: StructVariantState) - -> Result<(), Error> { - Err(Error::top_level()) + fn end(self) -> Result<Self::Ok, Error> { + Ok(self.urlencoder) + } +} + +impl<'output, Target> ser::SerializeStructVariant + for StructVariantSerializer<'output, Target> + where Target: 'output + UrlEncodedTarget, +{ + type Ok = &'output mut UrlEncodedSerializer<Target>; + type Error = Error; + + fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, + key: &'static str, + value: &T) + -> Result<(), Error> { + self.inner.serialize_field(key, value) + } + + fn end(self) -> Result<Self::Ok, Error> { + self.inner.end() } } impl Error { fn top_level() -> Self { - Error::Custom( - "top-level serializer supports only maps and structs".into()) + let msg = "top-level serializer supports only maps and structs"; + Error::Custom(msg.into()) + } + + fn no_key() -> Self { + let msg = "tried to serialize a value before serializing key"; + Error::Custom(msg.into()) } } diff --git a/src/ser/pair.rs b/src/ser/pair.rs index 6dfed3d..743b3dd 100644 --- a/src/ser/pair.rs +++ b/src/ser/pair.rs @@ -1,329 +1,261 @@ -use ser::{Error, key, value}; -use serde::{Serialize, Serializer}; +use ser::Error; +use ser::key::KeySink; +use ser::part::PartSerializer; +use ser::value::ValueSink; +use ser::void::VoidSerializer; +use serde::ser; use std::borrow::Cow; -use url::form_urlencoded; +use std::mem; +use url::form_urlencoded::Serializer as UrlEncodedSerializer; +use url::form_urlencoded::Target as UrlEncodedTarget; -pub struct PairSerializer<'target, Target>( - &'target mut form_urlencoded::Serializer<Target>) - where Target: 'target + form_urlencoded::Target; +pub struct PairSerializer<'target, Target: 'target + UrlEncodedTarget> { + urlencoder: &'target mut UrlEncodedSerializer<Target>, + state: PairState, +} impl<'target, Target> PairSerializer<'target, Target> - where Target: 'target + form_urlencoded::Target + where Target: 'target + UrlEncodedTarget, { - pub fn new( - serializer: &'target mut form_urlencoded::Serializer<Target>) - -> Self { - PairSerializer(serializer) + pub fn new(urlencoder: &'target mut UrlEncodedSerializer<Target>) -> Self { + PairSerializer { + urlencoder: urlencoder, + state: PairState::WaitingForKey, + } } } -pub struct TupleState(Option<Option<Cow<'static, str>>>); -pub struct TupleStructState(TupleState); - -impl<'target, Target> Serializer for PairSerializer<'target, Target> - where Target: 'target + form_urlencoded::Target +impl<'target, Target> ser::Serializer for PairSerializer<'target, Target> + where Target: 'target + UrlEncodedTarget, { + type Ok = (); type Error = Error; - type SeqState = (); - type TupleState = TupleState; - type TupleStructState = TupleStructState; - type TupleVariantState = (); - type MapState = (); - type StructState = (); - type StructVariantState = (); - - fn serialize_bool(&mut self, _v: bool) -> Result<(), Error> { - Err(Error::Custom("booleans are not supported values".into())) - } - - fn serialize_isize(&mut self, _v: isize) -> Result<(), Error> { - Err(Error::unsupported_pair()) - } + type SerializeSeq = VoidSerializer<()>; + type SerializeTuple = Self; + type SerializeTupleStruct = VoidSerializer<()>; + type SerializeTupleVariant = VoidSerializer<()>; + type SerializeMap = VoidSerializer<()>; + type SerializeStruct = VoidSerializer<()>; + type SerializeStructVariant = VoidSerializer<()>; - fn serialize_i8(&mut self, _v: i8) -> Result<(), Error> { + fn serialize_bool(self, _v: bool) -> Result<(), Error> { Err(Error::unsupported_pair()) } - fn serialize_i16(&mut self, _v: i16) -> Result<(), Error> { + fn serialize_i8(self, _v: i8) -> Result<(), Error> { Err(Error::unsupported_pair()) } - fn serialize_i32(&mut self, _v: i32) -> Result<(), Error> { + fn serialize_i16(self, _v: i16) -> Result<(), Error> { Err(Error::unsupported_pair()) } - fn serialize_i64(&mut self, _v: i64) -> Result<(), Error> { + fn serialize_i32(self, _v: i32) -> Result<(), Error> { Err(Error::unsupported_pair()) } - fn serialize_usize(&mut self, _v: usize) -> Result<(), Error> { + fn serialize_i64(self, _v: i64) -> Result<(), Error> { Err(Error::unsupported_pair()) } - fn serialize_u8(&mut self, _v: u8) -> Result<(), Error> { + fn serialize_u8(self, _v: u8) -> Result<(), Error> { Err(Error::unsupported_pair()) } - fn serialize_u16(&mut self, _v: u16) -> Result<(), Error> { + fn serialize_u16(self, _v: u16) -> Result<(), Error> { Err(Error::unsupported_pair()) } - fn serialize_u32(&mut self, _v: u32) -> Result<(), Error> { + fn serialize_u32(self, _v: u32) -> Result<(), Error> { Err(Error::unsupported_pair()) } - fn serialize_u64(&mut self, _v: u64) -> Result<(), Error> { + fn serialize_u64(self, _v: u64) -> Result<(), Error> { Err(Error::unsupported_pair()) } - fn serialize_f32(&mut self, _v: f32) -> Result<(), Error> { + fn serialize_f32(self, _v: f32) -> Result<(), Error> { Err(Error::unsupported_pair()) } - fn serialize_f64(&mut self, _v: f64) -> Result<(), Error> { + fn serialize_f64(self, _v: f64) -> Result<(), Error> { Err(Error::unsupported_pair()) } - fn serialize_char(&mut self, _v: char) -> Result<(), Error> { + fn serialize_char(self, _v: char) -> Result<(), Error> { Err(Error::unsupported_pair()) } - fn serialize_str(&mut self, _value: &str) -> Result<(), Error> { + fn serialize_str(self, _value: &str) -> Result<(), Error> { Err(Error::unsupported_pair()) } - fn serialize_bytes(&mut self, _value: &[u8]) -> Result<(), Error> { + fn serialize_bytes(self, _value: &[u8]) -> Result<(), Error> { Err(Error::unsupported_pair()) } - fn serialize_unit(&mut self) -> Result<(), Error> { + fn serialize_unit(self) -> Result<(), Error> { Err(Error::unsupported_pair()) } - fn serialize_unit_struct( - &mut self, _name: &'static str) - -> Result<(), Error> { + fn serialize_unit_struct(self, _name: &'static str) -> Result<(), Error> { Err(Error::unsupported_pair()) } - fn serialize_unit_variant( - &mut self, - _name: &'static str, - _variant_index: usize, - _variant: &'static str) - -> Result<(), Error> { + fn serialize_unit_variant(self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str) + -> Result<(), Error> { Err(Error::unsupported_pair()) } - fn serialize_newtype_struct<T>( - &mut self, _name: &'static str, value: T) - -> Result<(), Error> - where T: Serialize - { + fn serialize_newtype_struct<T: ?Sized + ser::Serialize> + (self, + _name: &'static str, + value: &T) + -> Result<(), Error> { value.serialize(self) } - fn serialize_newtype_variant<T>( - &mut self, - _name: &'static str, - _variant_index: usize, - _variant: &'static str, - _value: T) - -> Result<(), Error> - where T: Serialize - { + fn serialize_newtype_variant<T: ?Sized + ser::Serialize> + (self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _value: &T) + -> Result<(), Error> { Err(Error::unsupported_pair()) } - fn serialize_none(&mut self) -> Result<(), Error> { + fn serialize_none(self) -> Result<(), Error> { Ok(()) } - fn serialize_some<T>(&mut self, value: T) -> Result<(), Error> - where T: Serialize - { + fn serialize_some<T: ?Sized + ser::Serialize>(self, + value: &T) + -> Result<(), Error> { value.serialize(self) } - fn serialize_seq(&mut self, _len: Option<usize>) - -> Result<(), Error> { + fn serialize_seq(self, + _len: Option<usize>) + -> Result<VoidSerializer<()>, Error> { Err(Error::unsupported_pair()) } - fn serialize_seq_elt<T>(&mut self, _state: &mut (), _value: T) - -> Result<(), Error> - where T: Serialize - { + fn serialize_seq_fixed_size(self, + _len: usize) + -> Result<VoidSerializer<()>, Error> { Err(Error::unsupported_pair()) } - fn serialize_seq_end(&mut self, _state: ()) -> Result<(), Error> { - Err(Error::unsupported_pair()) - } - - fn serialize_seq_fixed_size(&mut self, _size: usize) - -> Result<(), Error> { - Err(Error::unsupported_pair()) - } - - fn serialize_tuple(&mut self, len: usize) -> Result<TupleState, Error> { + fn serialize_tuple(self, len: usize) -> Result<Self, Error> { if len == 2 { - Ok(TupleState(None)) + Ok(self) } else { Err(Error::unsupported_pair()) } } - fn serialize_tuple_elt<T>( - &mut self, state: &mut TupleState, value: T) - -> Result<(), Error> - where T: Serialize - { - match state.0.take() { - None => { - let mut key = None; - { - let mut key_serializer = - key::MapKeySerializer::new(&mut key); - try!(value.serialize(&mut key_serializer)); - } - state.0 = Some(key); - Ok(()) - }, - Some(ref mut key) => { - { - let mut value_serializer = - value::ValueSerializer::new(key, &mut self.0).unwrap(); - try!(value.serialize(&mut value_serializer)); - } - state.0 = Some(None); - Ok(()) - } - } - } - - fn serialize_tuple_end(&mut self, _state: TupleState) -> Result<(), Error> { - Ok(()) - } - - fn serialize_tuple_struct( - &mut self, _name: &'static str, len: usize) - -> Result<TupleStructState, Error> { - self.serialize_tuple(len).map(TupleStructState) - } - - fn serialize_tuple_struct_elt<T>( - &mut self, state: &mut TupleStructState, value: T) - -> Result<(), Error> - where T: Serialize - { - self.serialize_tuple_elt(&mut state.0, value) - } - - fn serialize_tuple_struct_end( - &mut self, _state: TupleStructState) - -> Result<(), Error> { - Ok(()) - } - - fn serialize_tuple_variant( - &mut self, - _name: &'static str, - _variant_index: usize, - _variant: &'static str, - _len: usize) - -> Result<(), Error> { - Err(Error::unsupported_pair()) - } - - fn serialize_tuple_variant_elt<T>( - &mut self, _state: &mut (), _value: T) - -> Result<(), Error> - where T: Serialize - { + fn serialize_tuple_struct(self, + _name: &'static str, + _len: usize) + -> Result<VoidSerializer<()>, Error> { Err(Error::unsupported_pair()) } - fn serialize_tuple_variant_end( - &mut self, _state: ()) - -> Result<(), Error> { + fn serialize_tuple_variant(self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _len: usize) + -> Result<VoidSerializer<()>, Error> { Err(Error::unsupported_pair()) } - fn serialize_map( - &mut self, _len: Option<usize>) - -> Result<(), Error> { + fn serialize_map(self, + _len: Option<usize>) + -> Result<VoidSerializer<()>, Error> { Err(Error::unsupported_pair()) } - fn serialize_map_key<T>( - &mut self, _state: &mut (), _key: T) - -> Result<(), Error> - where T: Serialize - { + fn serialize_struct(self, + _name: &'static str, + _len: usize) + -> Result<VoidSerializer<()>, Error> { Err(Error::unsupported_pair()) } - fn serialize_map_value<T>( - &mut self, _state: &mut (), _value: T) - -> Result<(), Error> - where T: Serialize - { + fn serialize_struct_variant(self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _len: usize) + -> Result<VoidSerializer<()>, Error> { Err(Error::unsupported_pair()) } +} - fn serialize_map_end(&mut self, _state: ()) -> Result<(), Error> { - Err(Error::unsupported_pair()) - } +impl<'target, Target> ser::SerializeTuple for PairSerializer<'target, Target> + where Target: 'target + UrlEncodedTarget, +{ + type Ok = (); + type Error = Error; - fn serialize_struct(&mut self, _name: &'static str, _len: usize) - -> Result<(), Error> { - Err(Error::unsupported_pair()) + fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, + value: &T) + -> Result<(), Error> { + match mem::replace(&mut self.state, PairState::Done) { + PairState::WaitingForKey => { + let key_sink = KeySink::new(|key| Ok(key.into())); + let key_serializer = PartSerializer::new(key_sink); + self.state = PairState::WaitingForValue { + key: value.serialize(key_serializer)?, + }; + Ok(()) + }, + PairState::WaitingForValue { key } => { + let result = { + let value_sink = ValueSink::new(self.urlencoder, &key); + let value_serializer = PartSerializer::new(value_sink); + value.serialize(value_serializer) + }; + if result.is_ok() { + self.state = PairState::Done; + } else { + self.state = PairState::WaitingForValue { key: key }; + } + result + }, + PairState::Done => Err(Error::done()), + } } - fn serialize_struct_elt<T>( - &mut self, - _state: &mut (), - _key: &'static str, - _value: T) - -> Result<(), Error> - where T: Serialize - { - Err(Error::unsupported_pair()) + fn end(self) -> Result<(), Error> { + if let PairState::Done = self.state { + Ok(()) + } else { + Err(Error::not_done()) + } } +} - fn serialize_struct_end( - &mut self, _state: ()) - -> Result<(), Error> { - Err(Error::unsupported_pair()) - } +enum PairState { + WaitingForKey, + WaitingForValue { key: Cow<'static, str> }, + Done, +} - fn serialize_struct_variant( - &mut self, - _name: &'static str, - _variant_index: usize, - _variant: &'static str, - _len: usize) - -> Result<(), Error> { - Err(Error::unsupported_pair()) - } - fn serialize_struct_variant_elt<T>( - &mut self, - _state: &mut (), - _key: &'static str, - _value: T) - -> Result<(), Error> { - Err(Error::unsupported_pair()) +impl Error { + fn done() -> Self { + Error::Custom("this pair has already been serialized".into()) } - fn serialize_struct_variant_end( - &mut self, _state: ()) - -> Result<(), Error> { - Err(Error::unsupported_pair()) + fn not_done() -> Self { + Error::Custom("this pair has not yet been serialized".into()) } -} -impl Error { fn unsupported_pair() -> Self { Error::Custom("unsupported pair".into()) } diff --git a/src/ser/part.rs b/src/ser/part.rs new file mode 100644 index 0000000..e720314 --- /dev/null +++ b/src/ser/part.rs @@ -0,0 +1,229 @@ +use dtoa; +use itoa; +use ser::Error; +use ser::void::VoidSerializer; +use serde::ser; +use std::str; + +pub struct PartSerializer<S> { + sink: S, +} + +impl<S: Sink> PartSerializer<S> { + pub fn new(sink: S) -> Self { + PartSerializer { sink: sink } + } +} + +pub trait Sink: Sized { + type Ok; + + fn serialize_static_str(self, + value: &'static str) + -> Result<Self::Ok, Error>; + + fn serialize_str(self, value: &str) -> Result<Self::Ok, Error>; + fn serialize_string(self, value: String) -> Result<Self::Ok, Error>; + fn serialize_none(self) -> Result<Self::Ok, Error>; + + fn serialize_some<T: ?Sized + ser::Serialize> + (self, + value: &T) + -> Result<Self::Ok, Error>; + + fn unsupported(self) -> Error; +} + +impl<S: Sink> ser::Serializer for PartSerializer<S> { + type Ok = S::Ok; + type Error = Error; + type SerializeSeq = VoidSerializer<S::Ok>; + type SerializeTuple = VoidSerializer<S::Ok>; + type SerializeTupleStruct = VoidSerializer<S::Ok>; + type SerializeTupleVariant = VoidSerializer<S::Ok>; + type SerializeMap = VoidSerializer<S::Ok>; + type SerializeStruct = VoidSerializer<S::Ok>; + type SerializeStructVariant = VoidSerializer<S::Ok>; + + fn serialize_bool(self, v: bool) -> Result<S::Ok, Error> { + self.sink.serialize_static_str(if v { "true" } else { "false" }) + } + + fn serialize_i8(self, v: i8) -> Result<S::Ok, Error> { + self.serialize_integer(v) + } + + fn serialize_i16(self, v: i16) -> Result<S::Ok, Error> { + self.serialize_integer(v) + } + + fn serialize_i32(self, v: i32) -> Result<S::Ok, Error> { + self.serialize_integer(v) + } + + fn serialize_i64(self, v: i64) -> Result<S::Ok, Error> { + self.serialize_integer(v) + } + + fn serialize_u8(self, v: u8) -> Result<S::Ok, Error> { + self.serialize_integer(v) + } + + fn serialize_u16(self, v: u16) -> Result<S::Ok, Error> { + self.serialize_integer(v) + } + + fn serialize_u32(self, v: u32) -> Result<S::Ok, Error> { + self.serialize_integer(v) + } + + fn serialize_u64(self, v: u64) -> Result<S::Ok, Error> { + self.serialize_integer(v) + } + + fn serialize_f32(self, v: f32) -> Result<S::Ok, Error> { + self.serialize_floating(v) + } + + fn serialize_f64(self, v: f64) -> Result<S::Ok, Error> { + self.serialize_floating(v) + } + + fn serialize_char(self, v: char) -> Result<S::Ok, Error> { + self.sink.serialize_string(v.to_string()) + } + + fn serialize_str(self, value: &str) -> Result<S::Ok, Error> { + self.sink.serialize_str(value) + } + + fn serialize_bytes(self, value: &[u8]) -> Result<S::Ok, Error> { + match str::from_utf8(value) { + Ok(value) => self.sink.serialize_str(value), + Err(err) => Err(Error::Utf8(err)), + } + } + + fn serialize_unit(self) -> Result<S::Ok, Error> { + Err(self.sink.unsupported()) + } + + fn serialize_unit_struct(self, name: &'static str) -> Result<S::Ok, Error> { + self.sink.serialize_static_str(name.into()) + } + + fn serialize_unit_variant(self, + _name: &'static str, + _variant_index: usize, + variant: &'static str) + -> Result<S::Ok, Error> { + self.sink.serialize_static_str(variant.into()) + } + + fn serialize_newtype_struct<T: ?Sized + ser::Serialize> + (self, + _name: &'static str, + value: &T) + -> Result<S::Ok, Error> { + value.serialize(self) + } + + fn serialize_newtype_variant<T: ?Sized + ser::Serialize> + (self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _value: &T) + -> Result<S::Ok, Error> { + Err(self.sink.unsupported()) + } + + fn serialize_none(self) -> Result<S::Ok, Error> { + self.sink.serialize_none() + } + + fn serialize_some<T: ?Sized + ser::Serialize>(self, + value: &T) + -> Result<S::Ok, Error> { + self.sink.serialize_some(value) + } + + fn serialize_seq(self, + _len: Option<usize>) + -> Result<Self::SerializeSeq, Error> { + Err(self.sink.unsupported()) + } + + + fn serialize_seq_fixed_size(self, + _len: usize) + -> Result<Self::SerializeSeq, Error> { + Err(self.sink.unsupported()) + } + + fn serialize_tuple(self, + _len: usize) + -> Result<Self::SerializeTuple, Error> { + Err(self.sink.unsupported()) + } + + fn serialize_tuple_struct(self, + _name: &'static str, + _len: usize) + -> Result<Self::SerializeTuple, Error> { + Err(self.sink.unsupported()) + } + + fn serialize_tuple_variant + (self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _len: usize) + -> Result<Self::SerializeTupleVariant, Error> { + Err(self.sink.unsupported()) + } + + fn serialize_map(self, + _len: Option<usize>) + -> Result<Self::SerializeMap, Error> { + Err(self.sink.unsupported()) + } + + fn serialize_struct(self, + _name: &'static str, + _len: usize) + -> Result<Self::SerializeStruct, Error> { + Err(self.sink.unsupported()) + } + + fn serialize_struct_variant + (self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _len: usize) + -> Result<Self::SerializeStructVariant, Error> { + Err(self.sink.unsupported()) + } +} + +impl<S: Sink> PartSerializer<S> { + fn serialize_integer<I>(self, value: I) -> Result<S::Ok, Error> + 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<F>(self, value: F) -> Result<S::Ok, Error> + 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) + } +} diff --git a/src/ser/value.rs b/src/ser/value.rs index d017242..e4e1f2c 100644 --- a/src/ser/value.rs +++ b/src/ser/value.rs @@ -1,326 +1,59 @@ use ser::Error; -use serde::{Serialize, Serializer}; -use std::borrow::Cow; +use ser::part::{PartSerializer, Sink}; +use serde::ser::Serialize; use std::str; -use url::form_urlencoded; +use url::form_urlencoded::Serializer as UrlEncodedSerializer; +use url::form_urlencoded::Target as UrlEncodedTarget; -pub struct ValueSerializer<'key, 'target, Target> - where Target: 'target + form_urlencoded::Target +pub struct ValueSink<'key, 'target, Target> + where Target: 'target + UrlEncodedTarget, { - key: &'key mut Option<Cow<'static, str>>, - serializer: &'target mut form_urlencoded::Serializer<Target> + urlencoder: &'target mut UrlEncodedSerializer<Target>, + key: &'key str, } -impl<'key, 'target, Target> ValueSerializer<'key, 'target, Target> - where Target: 'target + form_urlencoded::Target +impl<'key, 'target, Target> ValueSink<'key, 'target, Target> + where Target: 'target + UrlEncodedTarget, { - pub fn new( - key: &'key mut Option<Cow<'static, str>>, - serializer: &'target mut form_urlencoded::Serializer<Target>) - -> Result<Self, Error> { - if key.is_some() { - Ok(ValueSerializer { - key: key, - serializer: serializer, - }) - } else { - Err(Error::no_key()) - } - } - - fn append_pair(&mut self, value: &str) -> Result<(), Error> { - if let Some(key) = self.key.take() { - self.serializer.append_pair(&key, value); - Ok(()) - } else { - Err(Error::no_key()) + pub fn new(urlencoder: &'target mut UrlEncodedSerializer<Target>, + key: &'key str) + -> Self { + ValueSink { + urlencoder: urlencoder, + key: key, } } } -impl<'key, 'target, Target> Serializer - for ValueSerializer<'key, 'target, Target> - where Target: 'target + form_urlencoded::Target +impl<'key, 'target, Target> Sink for ValueSink<'key, 'target, Target> + where Target: 'target + UrlEncodedTarget, { - type Error = Error; - type SeqState = (); - type TupleState = (); - type TupleStructState = (); - type TupleVariantState = (); - type MapState = (); - type StructState = (); - type StructVariantState = (); - - fn serialize_bool(&mut self, v: bool) -> Result<(), Error> { - self.append_pair(if v { "true" } else { "false" }) - } - - fn serialize_isize(&mut self, v: isize) -> Result<(), Error> { - self.append_pair(&v.to_string()) - } - - fn serialize_i8(&mut self, v: i8) -> Result<(), Error> { - self.append_pair(&v.to_string()) - } - - fn serialize_i16(&mut self, v: i16) -> Result<(), Error> { - self.append_pair(&v.to_string()) - } - - fn serialize_i32(&mut self, v: i32) -> Result<(), Error> { - self.append_pair(&v.to_string()) - } - - fn serialize_i64(&mut self, v: i64) -> Result<(), Error> { - self.append_pair(&v.to_string()) - } - - fn serialize_usize(&mut self, v: usize) -> Result<(), Error> { - self.append_pair(&v.to_string()) - } - - fn serialize_u8(&mut self, v: u8) -> Result<(), Error> { - self.append_pair(&v.to_string()) - } - - fn serialize_u16(&mut self, v: u16) -> Result<(), Error> { - self.append_pair(&v.to_string()) - } - - fn serialize_u32(&mut self, v: u32) -> Result<(), Error> { - self.append_pair(&v.to_string()) - } + type Ok = (); - fn serialize_u64(&mut self, v: u64) -> Result<(), Error> { - self.append_pair(&v.to_string()) + fn serialize_str(self, value: &str) -> Result<(), Error> { + self.urlencoder.append_pair(self.key, value); + Ok(()) } - fn serialize_f32(&mut self, v: f32) -> Result<(), Error> { - self.append_pair(&v.to_string()) - } - - fn serialize_f64(&mut self, v: f64) -> Result<(), Error> { - self.append_pair(&v.to_string()) - } - - fn serialize_char(&mut self, v: char) -> Result<(), Error> { - self.append_pair(&v.to_string()) - } - - fn serialize_str(&mut self, value: &str) -> Result<(), Error> { - self.append_pair(value) - } - - fn serialize_bytes(&mut self, value: &[u8]) -> Result<(), Error> { - match str::from_utf8(value) { - Ok(value) => self.append_pair(value), - Err(err) => Err(Error::Utf8(err)), - } - } - - fn serialize_unit(&mut self) -> Result<(), Error> { - Err(Error::unsupported_value()) - } - - fn serialize_unit_struct( - &mut self, name: &'static str) - -> Result<(), Error> { - self.append_pair(name) - } - - fn serialize_unit_variant( - &mut self, - _name: &'static str, - _variant_index: usize, - variant: &'static str) - -> Result<(), Error> { - self.append_pair(variant) - } - - fn serialize_newtype_struct<T>( - &mut self, _name: &'static str, value: T) - -> Result<(), Error> - where T: Serialize - { - value.serialize(self) - } - - fn serialize_newtype_variant<T>( - &mut self, - _name: &'static str, - _variant_index: usize, - _variant: &'static str, - _value: T) - -> Result<(), Error> - where T: Serialize - { - Err(Error::unsupported_value()) - } - - fn serialize_none(&mut self) -> Result<(), Error> { - if let Some(_) = self.key.take() { - Ok(()) - } else { - Err(Error::no_key()) - } + fn serialize_static_str(self, value: &'static str) -> Result<(), Error> { + self.serialize_str(value) } - fn serialize_some<T>(&mut self, value: T) -> Result<(), Error> - where T: Serialize - { - value.serialize(self) + fn serialize_string(self, value: String) -> Result<(), Error> { + self.serialize_str(&value) } - fn serialize_seq(&mut self, _len: Option<usize>) -> Result<(), Error> { - Err(Error::unsupported_value()) + fn serialize_none(self) -> Result<Self::Ok, Error> { + Ok(()) } - fn serialize_seq_elt<T>( - &mut self, _state: &mut (), _value: T) - -> Result<(), Error> - where T: Serialize - { - Err(Error::unsupported_value()) - } - - fn serialize_seq_end(&mut self, _state: ()) -> Result<(), Error> { - Err(Error::unsupported_value()) - } - - fn serialize_seq_fixed_size(&mut self, _size: usize) -> Result<(), Error> { - Err(Error::unsupported_value()) - } - - fn serialize_tuple(&mut self, _len: usize) -> Result<(), Error> { - Err(Error::unsupported_value()) - } - - fn serialize_tuple_elt<T>( - &mut self, _state: &mut (), _value: T) - -> Result<(), Error> - where T: Serialize - { - Err(Error::unsupported_value()) - } - - fn serialize_tuple_end(&mut self, _state: ()) -> Result<(), Error> { - Err(Error::unsupported_value()) - } - - fn serialize_tuple_struct( - &mut self, _name: &'static str, _len: usize) - -> Result<(), Error> { - Err(Error::unsupported_value()) - } - - fn serialize_tuple_struct_elt<T>( - &mut self, _state: &mut (), _value: T) - -> Result<(), Error> - where T: Serialize - { - Err(Error::unsupported_value()) - } - - fn serialize_tuple_struct_end(&mut self, _state: ()) -> Result<(), Error> { - Err(Error::unsupported_value()) - } - - fn serialize_tuple_variant( - &mut self, - _name: &'static str, - _variant_index: usize, - _variant: &'static str, - _len: usize) - -> Result<(), Error> { - Err(Error::unsupported_value()) - } - - fn serialize_tuple_variant_elt<T>( - &mut self, _state: &mut (), _value: T) - -> Result<(), Error> - where T: Serialize - { - Err(Error::unsupported_value()) - } - - fn serialize_tuple_variant_end(&mut self, _state: ()) -> Result<(), Error> { - Err(Error::unsupported_value()) - } - - fn serialize_map(&mut self, _len: Option<usize>) -> Result<(), Error> { - Err(Error::unsupported_value()) - } - - fn serialize_map_key<T>( - &mut self, _state: &mut (), _key: T) - -> Result<(), Error> - where T: Serialize - { - Err(Error::unsupported_value()) - } - - fn serialize_map_value<T>( - &mut self, _state: &mut (), _value: T) - -> Result<(), Error> - where T: Serialize - { - Err(Error::unsupported_value()) - } - - fn serialize_map_end(&mut self, _state: ()) -> Result<(), Error> { - Err(Error::unsupported_value()) - } - - fn serialize_struct( - &mut self, _name: &'static str, _len: usize) - -> Result<(), Error> { - Err(Error::unsupported_value()) - } - - fn serialize_struct_elt<T>( - &mut self, - _state: &mut (), - _key: &'static str, - _value: T) - -> Result<(), Error> - where T: Serialize - { - Err(Error::unsupported_value()) - } - - fn serialize_struct_end(&mut self, _state: ()) -> Result<(), Error> { - Err(Error::unsupported_value()) - } - - fn serialize_struct_variant( - &mut self, - _name: &'static str, - _variant_index: usize, - _variant: &'static str, - _len: usize) - -> Result<(), Error> { - Err(Error::unsupported_value()) - } - fn serialize_struct_variant_elt<T>( - &mut self, _state: &mut (), _key: &'static str, _value: T) - -> Result<(), Error> { - Err(Error::unsupported_value()) - } - - fn serialize_struct_variant_end( - &mut self, _state: ()) - -> Result<(), Error> { - Err(Error::unsupported_value()) - } -} - -impl Error { - fn no_key() -> Self { - Error::Custom( - "tried to serialize a value before serializing key".into()) + fn serialize_some<T: ?Sized + Serialize>(self, + value: &T) + -> Result<Self::Ok, Error> { + value.serialize(PartSerializer::new(self)) } - fn unsupported_value() -> Self { + fn unsupported(self) -> Error { Error::Custom("unsupported value".into()) } } diff --git a/src/ser/void.rs b/src/ser/void.rs new file mode 100644 index 0000000..4765bb7 --- /dev/null +++ b/src/ser/void.rs @@ -0,0 +1,122 @@ +use ser::Error; +use serde::ser; +use std::marker::PhantomData; +use void; + +pub struct VoidSerializer<Ok> { + void: void::Void, + _marker: PhantomData<Ok>, +} + +impl<Ok> ser::SerializeSeq for VoidSerializer<Ok> { + type Ok = Ok; + type Error = Error; + + fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result<Ok, Error> { + void::unreachable(self.void) + } +} + +impl<Ok> ser::SerializeTuple for VoidSerializer<Ok> { + type Ok = Ok; + type Error = Error; + + fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result<Ok, Error> { + void::unreachable(self.void) + } +} + +impl<Ok> ser::SerializeTupleStruct for VoidSerializer<Ok> { + type Ok = Ok; + type Error = Error; + + fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result<Ok, Error> { + void::unreachable(self.void) + } +} + +impl<Ok> ser::SerializeTupleVariant for VoidSerializer<Ok> { + type Ok = Ok; + type Error = Error; + + fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result<Ok, Error> { + void::unreachable(self.void) + } +} + +impl<Ok> ser::SerializeMap for VoidSerializer<Ok> { + type Ok = Ok; + type Error = Error; + + fn serialize_key<T: ?Sized + ser::Serialize>(&mut self, + _key: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn serialize_value<T: ?Sized + ser::Serialize>(&mut self, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result<Ok, Error> { + void::unreachable(self.void) + } +} + +impl<Ok> ser::SerializeStruct for VoidSerializer<Ok> { + type Ok = Ok; + type Error = Error; + + fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, + _key: &'static str, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result<Ok, Error> { + void::unreachable(self.void) + } +} + +impl<Ok> ser::SerializeStructVariant for VoidSerializer<Ok> { + type Ok = Ok; + type Error = Error; + + fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, + _key: &'static str, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result<Ok, Error> { + void::unreachable(self.void) + } +} |