summaryrefslogtreecommitdiff
path: root/src/de.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/de.rs')
-rw-r--r--src/de.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/de.rs b/src/de.rs
index 8925f52..695547f 100644
--- a/src/de.rs
+++ b/src/de.rs
@@ -4,9 +4,48 @@ use serde::de;
use serde::de::value::MapDeserializer;
use std::borrow::Cow;
use url::form_urlencoded::Parse as UrlEncodedParse;
+use url::form_urlencoded::parse;
pub use serde::de::value::Error;
+/// Deserializes a `application/x-wwww-url-encoded` value from a `&[u8]`.
+///
+/// ```
+/// let meal = vec![
+/// ("bread".to_owned(), "baguette".to_owned()),
+/// ("cheese".to_owned(), "comté".to_owned()),
+/// ("meat".to_owned(), "ham".to_owned()),
+/// ("fat".to_owned(), "butter".to_owned()),
+/// ];
+///
+/// assert_eq!(
+/// serde_urlencoded::from_bytes::<Vec<(String, String)>>(
+/// b"bread=baguette&cheese=comt%C3%A9&meat=ham&fat=butter"),
+/// Ok(meal));
+/// ```
+pub fn from_bytes<T: de::Deserialize>(input: &[u8]) -> Result<T, Error> {
+ T::deserialize(&mut Deserializer::new(parse(input)))
+}
+
+/// Deserializes a `application/x-wwww-url-encoded` value from a `&str`.
+///
+/// ```
+/// let meal = vec![
+/// ("bread".to_owned(), "baguette".to_owned()),
+/// ("cheese".to_owned(), "comté".to_owned()),
+/// ("meat".to_owned(), "ham".to_owned()),
+/// ("fat".to_owned(), "butter".to_owned()),
+/// ];
+///
+/// assert_eq!(
+/// serde_urlencoded::from_str::<Vec<(String, String)>>(
+/// "bread=baguette&cheese=comt%C3%A9&meat=ham&fat=butter"),
+/// Ok(meal));
+/// ```
+pub fn from_str<T: de::Deserialize>(input: &str) -> Result<T, Error> {
+ from_bytes(input.as_bytes())
+}
+
/// A deserializer for the `application/x-www-form-urlencoded` format.
///
/// * Supported top-level outputs are structs, maps and sequences of pairs,