From 4a6b46f9222cad61ceb8e9246b4cc33eb4f61f57 Mon Sep 17 00:00:00 2001 From: Sam Scott Date: Wed, 15 Feb 2017 17:43:33 +0000 Subject: Some _very_ hacky code to get nested url_encoded params working. --- src/ser/key.rs | 24 ++++++++++++++++++++++-- src/ser/part.rs | 14 ++++++++------ src/ser/value.rs | 26 ++++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 10 deletions(-) (limited to 'src/ser') diff --git a/src/ser/key.rs b/src/ser/key.rs index 6cbbff4..a84f311 100644 --- a/src/ser/key.rs +++ b/src/ser/key.rs @@ -1,4 +1,5 @@ -use ser::Error; +use ser::{Error}; +use serde::ser::{SerializeStruct}; use ser::part::Sink; use serde::Serialize; use std::borrow::Cow; @@ -44,7 +45,6 @@ impl KeySink impl Sink for KeySink where End: for<'key> FnOnce(Key<'key>) -> Result { - type Ok = Ok; fn serialize_static_str(self, value: &'static str) @@ -74,3 +74,23 @@ impl Sink for KeySink Error::Custom("unsupported key".into()) } } + +impl SerializeStruct for KeySink +where End: for<'key> FnOnce(Key<'key>) -> Result +{ + type Ok = Ok; + type Error = Error; + + fn serialize_field(&mut self, + _key: &'static str, + _value: &T) + -> Result<(), Error> { + Err(Error::top_level()) + } + + fn end(self) -> Result { + Err(self.unsupported()) + } +} + + diff --git a/src/ser/part.rs b/src/ser/part.rs index 42653c0..d2ade0a 100644 --- a/src/ser/part.rs +++ b/src/ser/part.rs @@ -14,8 +14,8 @@ impl PartSerializer { } } -pub trait Sink: Sized { - type Ok; +pub trait Sink: Sized + ser::SerializeStruct { + // type Ok; fn serialize_static_str(self, value: &'static str) @@ -33,7 +33,7 @@ pub trait Sink: Sized { fn unsupported(self) -> Error; } -impl ser::Serializer for PartSerializer { +impl> ser::Serializer for PartSerializer { type Ok = S::Ok; type Error = Error; type SerializeSeq = ser::Impossible; @@ -41,7 +41,7 @@ impl ser::Serializer for PartSerializer { type SerializeTupleStruct = ser::Impossible; type SerializeTupleVariant = ser::Impossible; type SerializeMap = ser::Impossible; - type SerializeStruct = ser::Impossible; + type SerializeStruct = S; type SerializeStructVariant = ser::Impossible; fn serialize_bool(self, v: bool) -> Result { @@ -193,7 +193,9 @@ impl ser::Serializer for PartSerializer { _name: &'static str, _len: usize) -> Result { - Err(self.sink.unsupported()) + // Err(self.sink.unsupported()) + Ok(self.sink) + } fn serialize_struct_variant @@ -207,7 +209,7 @@ impl ser::Serializer for PartSerializer { } } -impl PartSerializer { +impl> PartSerializer { fn serialize_integer(self, value: I) -> Result where I: itoa::Integer, { diff --git a/src/ser/value.rs b/src/ser/value.rs index e4e1f2c..56b410e 100644 --- a/src/ser/value.rs +++ b/src/ser/value.rs @@ -1,6 +1,6 @@ use ser::Error; use ser::part::{PartSerializer, Sink}; -use serde::ser::Serialize; +use serde::ser::{Serialize, SerializeStruct}; use std::str; use url::form_urlencoded::Serializer as UrlEncodedSerializer; use url::form_urlencoded::Target as UrlEncodedTarget; @@ -28,7 +28,7 @@ impl<'key, 'target, Target> ValueSink<'key, 'target, Target> impl<'key, 'target, Target> Sink for ValueSink<'key, 'target, Target> where Target: 'target + UrlEncodedTarget, { - type Ok = (); + // type Ok = (); fn serialize_str(self, value: &str) -> Result<(), Error> { self.urlencoder.append_pair(self.key, value); @@ -57,3 +57,25 @@ impl<'key, 'target, Target> Sink for ValueSink<'key, 'target, Target> Error::Custom("unsupported value".into()) } } + + +impl<'key, 'target, Target> SerializeStruct for ValueSink<'key, 'target, Target> + where Target: 'target + UrlEncodedTarget, +{ + type Ok = (); + type Error = Error; + + fn serialize_field(&mut self, + key: &'static str, + value: &T) + -> Result<(), Error> { + let newk = format!("{}[{}]", self.key, key); + let value_sink = ValueSink::new(self.urlencoder, &newk); + value.serialize(super::part::PartSerializer::new(value_sink)) + } + + fn end(self) -> Result { + Ok(()) + } +} + -- cgit v1.2.3