45: Fix struct newtype deserialization (and add tests) r=nox a=samsieber

Fixes #41

I only had to fix the deserializer - the serialization already works. So now they work the same way - you can serialize something and then deserialize it losslessly. 

I also added tests for serialization and deserialization. Let me know if there's anything you'd like changed.

Co-authored-by: Sam Sieber <swsieber@gmail.com>
This commit is contained in:
bors[bot] 2018-11-19 16:16:45 +00:00
commit 11cc5bb88f
4 changed files with 34 additions and 2 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "serde_urlencoded"
version = "0.5.3"
version = "0.5.4"
authors = ["Anthony Ramine <n.oxyde@gmail.com>"]
license = "MIT/Apache-2.0"
repository = "https://github.com/nox/serde_urlencoded"

View File

@ -208,6 +208,16 @@ impl<'de> de::Deserializer<'de> for Part<'de> {
visitor.visit_enum(ValueEnumAccess(self.0))
}
fn deserialize_newtype_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, Self::Error>
where V: de::Visitor<'de>,
{
visitor.visit_newtype_struct(self)
}
forward_to_deserialize_any! {
char
str
@ -216,7 +226,6 @@ impl<'de> de::Deserializer<'de> for Part<'de> {
bytes
byte_buf
unit_struct
newtype_struct
tuple_struct
struct
identifier

View File

@ -2,6 +2,17 @@ extern crate serde_urlencoded;
#[macro_use]
extern crate serde_derive;
#[derive(Deserialize, Debug, PartialEq)]
struct NewType<T>(T);
#[test]
fn deserialize_newtype_i32() {
let result = vec![("field".to_owned(), NewType(11))];
assert_eq!(serde_urlencoded::from_str("field=11"),
Ok(result));
}
#[test]
fn deserialize_bytes() {
let result = vec![("first".to_owned(), 23), ("last".to_owned(), 42)];

View File

@ -2,6 +2,18 @@ extern crate serde_urlencoded;
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
struct NewType<T>(T);
#[test]
fn serialize_newtype_i32() {
let params = &[("field", Some(NewType(11))),];
assert_eq!(
serde_urlencoded::to_string(params),
Ok("field=11".to_owned())
);
}
#[test]
fn serialize_option_map_int() {
let params = &[("first", Some(23)), ("middle", None), ("last", Some(42))];