Skip to content
This repository has been archived by the owner on Aug 10, 2020. It is now read-only.

Commit

Permalink
Merge ruma#45
Browse files Browse the repository at this point in the history
45: Fix struct newtype deserialization (and add tests) r=nox a=samsieber

Fixes ruma#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>
  • Loading branch information
bors[bot] and samsieber committed Nov 19, 2018
2 parents 199ed02 + 8f829ac commit 11cc5bb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
11 changes: 10 additions & 1 deletion src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -216,7 +226,6 @@ impl<'de> de::Deserializer<'de> for Part<'de> {
bytes
byte_buf
unit_struct
newtype_struct
tuple_struct
struct
identifier
Expand Down
11 changes: 11 additions & 0 deletions tests/test_deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
Expand Down
12 changes: 12 additions & 0 deletions tests/test_serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))];
Expand Down

0 comments on commit 11cc5bb

Please sign in to comment.