25: Deserialize () from input without key/value pairs. r=nox
Currently there's no input that can be deserialized to `()`, this changes the crate to support deserializing strings without any key/value pair to a `()` (such as the empty string, or just `"&"`).

Would be great if you could release a patch for this, I have a project that needs this behavior.

EDIT: To expand on my use case, I have a trait which parses the body of an HTTP Request into a type (e.g. for a `POST` request). However, this is abstract over HTTP methods, and its expected to be able to parse an empty body (e.g. from a `GET` request) into `()`.
This commit is contained in:
bors[bot] 2017-05-21 07:34:36 +00:00
commit bdb7468bcb
4 changed files with 17 additions and 3 deletions

View File

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

View File

@ -18,7 +18,7 @@ This crate works with Cargo and can be found on
```toml ```toml
[dependencies] [dependencies]
serde_urlencoded = "0.4.2" serde_urlencoded = "0.5.1"
``` ```
[crates.io]: https://crates.io/crates/serde_urlencoded [crates.io]: https://crates.io/crates/serde_urlencoded

View File

@ -110,6 +110,13 @@ impl<'de> de::Deserializer<'de> for Deserializer<'de> {
visitor.visit_seq(self.inner) visitor.visit_seq(self.inner)
} }
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor<'de>,
{
self.inner.end()?;
visitor.visit_unit()
}
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool bool
u8 u8
@ -125,7 +132,6 @@ impl<'de> de::Deserializer<'de> for Deserializer<'de> {
char char
str str
string string
unit
option option
bytes bytes
byte_buf byte_buf

View File

@ -32,3 +32,11 @@ fn deserialize_option() {
]; ];
assert_eq!(serde_urlencoded::from_str("first=23&last=42"), Ok(result)); assert_eq!(serde_urlencoded::from_str("first=23&last=42"), Ok(result));
} }
#[test]
fn deserialize_unit() {
assert_eq!(serde_urlencoded::from_str(""), Ok(()));
assert_eq!(serde_urlencoded::from_str("&"), Ok(()));
assert_eq!(serde_urlencoded::from_str("&&"), Ok(()));
assert!(serde_urlencoded::from_str::<()>("first=23").is_err());
}