From 6ec689b3762a1f8090d97045a76d8d7282dd1fe3 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Sun, 11 Sep 2016 13:26:21 +0200 Subject: x-www-form-urlencoded meets Serde --- src/de.rs | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/de.rs (limited to 'src/de.rs') diff --git a/src/de.rs b/src/de.rs new file mode 100644 index 0000000..ec84ff2 --- /dev/null +++ b/src/de.rs @@ -0,0 +1,90 @@ +//! Deserialization support for the `application/x-www-form-urlencoded` format. + +use serde::de; +use serde::de::value::MapDeserializer; +use std::borrow::Cow; +use std::marker::PhantomData; +use url::form_urlencoded::Parse; + +pub use serde::de::value::Error; + +/// A deserializer for the `application/x-www-form-urlencoded` format. +/// +/// * Supported top-level outputs are structs, maps and sequences of pairs, +/// with or without a given length. +/// +/// * Main `deserialize` methods defers to `deserialize_map`. +/// +/// * Everything else but `deserialize_seq` and `deserialize_seq_fixed_size` +/// defers to `deserialize`. +pub struct Deserializer<'a, T> { + deserializer: MapDeserializer, Cow<'a, str>, Cow<'a, str>, Error>, + marker: PhantomData, +} + +impl<'a, T> de::Deserializer for Deserializer<'a, T> +{ + type Error = Error; + + fn deserialize( + &mut self, visitor: V) + -> Result + where V: de::Visitor, + { + self.deserialize_map(visitor) + } + + fn deserialize_map( + &mut self, mut visitor: V) + -> Result + where V: de::Visitor, + { + visitor.visit_map(&mut self.deserializer) + } + + fn deserialize_seq( + &mut self, mut visitor: V) + -> Result + where V: de::Visitor, + { + visitor.visit_seq(&mut self.deserializer) + } + + fn deserialize_seq_fixed_size( + &mut self, _len: usize, mut visitor: V) + -> Result + where V: de::Visitor + { + visitor.visit_seq(&mut self.deserializer) + } + + forward_to_deserialize! { + bool + usize + u8 + u16 + u32 + u64 + isize + i8 + i16 + i32 + i64 + f32 + f64 + char + str + string + unit + option + bytes + unit_struct + newtype_struct + tuple_struct + struct + struct_field + tuple + enum + ignored_any + } +} -- cgit v1.2.3