summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorArthur Skobara <skobara.arthur@gmail.com>2017-02-05 10:37:15 +0700
committerAnthony Ramine <n.oxyde@gmail.com>2017-02-05 14:34:14 +0100
commit8839c6a86ce52eb28471ef491b280f9e91b023cd (patch)
treec3176aca5390a289292a90e1c0400efab22113e6 /src
parent4ef7cc6e85a3c17f341c3308b60e227e27e5e305 (diff)
Introduce de::from_reader
Diffstat (limited to 'src')
-rw-r--r--src/de.rs14
-rw-r--r--src/lib.rs2
2 files changed, 15 insertions, 1 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,
diff --git a/src/lib.rs b/src/lib.rs
index 285a091..db5f3a7 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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};