diff options
author | Arthur Skobara <skobara.arthur@gmail.com> | 2017-02-05 10:37:15 +0700 |
---|---|---|
committer | Anthony Ramine <n.oxyde@gmail.com> | 2017-02-05 14:34:14 +0100 |
commit | 8839c6a86ce52eb28471ef491b280f9e91b023cd (patch) | |
tree | c3176aca5390a289292a90e1c0400efab22113e6 /src | |
parent | 4ef7cc6e85a3c17f341c3308b60e227e27e5e305 (diff) |
Introduce de::from_reader
Diffstat (limited to 'src')
-rw-r--r-- | src/de.rs | 14 | ||||
-rw-r--r-- | src/lib.rs | 2 |
2 files changed, 15 insertions, 1 deletions
@@ -4,6 +4,7 @@ use serde::de; pub use serde::de::value::Error; use serde::de::value::MapDeserializer; +use std::io::Read; use url::form_urlencoded::Parse as UrlEncodedParse; use url::form_urlencoded::parse; @@ -45,6 +46,19 @@ pub fn from_str<T: de::Deserialize>(input: &str) -> Result<T, Error> { from_bytes(input.as_bytes()) } +/// Convenience function that reads all bytes from `reader` and deserializes +/// them with `from_bytes`. +pub fn from_reader<T, R>(mut reader: R) -> Result<T, Error> + where T: de::Deserialize, R: Read +{ + let mut buf = vec![]; + reader.read_to_end(&mut buf) + .map_err(|e| { + de::Error::custom(format_args!("could not read input: {}", e)) + })?; + from_bytes(&buf) +} + /// A deserializer for the `application/x-www-form-urlencoded` format. /// /// * Supported top-level outputs are structs, maps and sequences of pairs, @@ -11,5 +11,5 @@ extern crate url; pub mod de; pub mod ser; -pub use de::{Deserializer, from_bytes, from_str}; +pub use de::{Deserializer, from_bytes, from_str, from_reader}; pub use ser::{Serializer, to_string}; |