From 8839c6a86ce52eb28471ef491b280f9e91b023cd Mon Sep 17 00:00:00 2001 From: Arthur Skobara Date: Sun, 5 Feb 2017 10:37:15 +0700 Subject: Introduce de::from_reader --- src/de.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/de.rs') diff --git a/src/de.rs b/src/de.rs index 95208e9..95999d0 100644 --- a/src/de.rs +++ b/src/de.rs @@ -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(input: &str) -> Result { from_bytes(input.as_bytes()) } +/// Convenience function that reads all bytes from `reader` and deserializes +/// them with `from_bytes`. +pub fn from_reader(mut reader: R) -> Result + 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, -- cgit v1.2.3