summaryrefslogtreecommitdiff
path: root/src/de.rs
diff options
context:
space:
mode:
authorbors-ng[bot] <bors-ng[bot]@users.noreply.github.com>2017-02-05 13:36:49 +0000
committerbors-ng[bot] <bors-ng[bot]@users.noreply.github.com>2017-02-05 13:36:49 +0000
commit9931bc5318da6be3d3ea8ca5c298e132cf13ceac (patch)
treec3176aca5390a289292a90e1c0400efab22113e6 /src/de.rs
parent4ef7cc6e85a3c17f341c3308b60e227e27e5e305 (diff)
parent8839c6a86ce52eb28471ef491b280f9e91b023cd (diff)
Merge #12
12: implement deserialization of std::io::Read
Diffstat (limited to 'src/de.rs')
-rw-r--r--src/de.rs14
1 files changed, 14 insertions, 0 deletions
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<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,