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/value.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'src/ser/value.rs') diff --git a/src/ser/value.rs b/src/ser/value.rs index 56b410e..832cbf3 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, SerializeStruct}; +use serde::ser::{Serialize, SerializeSeq, SerializeStruct}; use std::str; use url::form_urlencoded::Serializer as UrlEncodedSerializer; use url::form_urlencoded::Target as UrlEncodedTarget; @@ -10,6 +10,7 @@ pub struct ValueSink<'key, 'target, Target> { urlencoder: &'target mut UrlEncodedSerializer, key: &'key str, + idx: usize, } impl<'key, 'target, Target> ValueSink<'key, 'target, Target> @@ -21,11 +22,12 @@ impl<'key, 'target, Target> ValueSink<'key, 'target, Target> ValueSink { urlencoder: urlencoder, key: key, + idx: 0, } } } -impl<'key, 'target, Target> Sink for ValueSink<'key, 'target, Target> +impl<'key, 'target, Target> Sink<(), Error> for ValueSink<'key, 'target, Target> where Target: 'target + UrlEncodedTarget, { // type Ok = (); @@ -43,13 +45,13 @@ impl<'key, 'target, Target> Sink for ValueSink<'key, 'target, Target> self.serialize_str(&value) } - fn serialize_none(self) -> Result { + fn serialize_none(self) -> Result<(), Error> { Ok(()) } fn serialize_some(self, value: &T) - -> Result { + -> Result<(), Error> { value.serialize(PartSerializer::new(self)) } @@ -79,3 +81,23 @@ impl<'key, 'target, Target> SerializeStruct for ValueSink<'key, 'target, Target> } } +impl<'key, 'target, Target> SerializeSeq for ValueSink<'key, 'target, Target> + where Target: 'target + UrlEncodedTarget, +{ + type Ok = (); + type Error = Error; + + fn serialize_element(&mut self, + value: &T) + -> Result<(), Error> { + let newk = format!("{}[{}]", self.key, self.idx); + self.idx += 1; + 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