summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSam Scott <sam.scott89@gmail.com>2017-03-12 15:33:01 -0400
committerSam Scott <sam.scott89@gmail.com>2017-03-12 15:33:01 -0400
commit4dfe0be52666a922ee8aa97e2a1d677c6463fcf2 (patch)
tree8ce5f56cef9981e5e2e00a9b09b60082b8de0482 /src
parent57bfc2e1061da6bf55a1d04ac35e6dbbeebabb69 (diff)
Address some clippy comments.
Add docs for `Config`.
Diffstat (limited to 'src')
-rw-r--r--src/de.rs61
-rw-r--r--src/lib.rs6
-rw-r--r--src/ser/mod.rs4
3 files changed, 45 insertions, 26 deletions
diff --git a/src/de.rs b/src/de.rs
index b33e07b..0e08bf1 100644
--- a/src/de.rs
+++ b/src/de.rs
@@ -9,14 +9,39 @@ use std::collections::btree_map::{BTreeMap, Entry, IntoIter};
use std::io::Read;
use url::percent_encoding;
+/// To override the default serialization parameters, first construct a new
+/// Config.
+///
+/// A `max_depth` of 0 implies no nesting: the result will be a flat map.
+/// This is mostly useful when the maximum nested depth is known beforehand,
+/// to prevent denial of service attacks by providing incredibly deeply nested
+/// inputs.
+///
+/// ```
+/// use serde_qs::de::Config;
+/// use std::collections::HashMap;
+///
+/// let mut config = Config::default();
+/// config.max_depth(0);
+/// let map: HashMap<String, String> = config.deserialize_str("a[b][c]=1")
+/// .unwrap();
+/// assert_eq!(map.get("a[b][c]").unwrap(), "1");
+///
+/// config.max_depth(10);
+/// let map: HashMap<String, HashMap<String, HashMap<String, String>>> =
+/// config.deserialize_str("a[b][c]=1").unwrap();
+/// assert_eq!(map.get("a").unwrap().get("b").unwrap().get("c").unwrap(), "1");
+/// ```
///
pub struct Config {
+ /// Specifies the maximum depth key that `serde_qs` will attempt to
+ /// deserialize. Default is 5.
max_depth: usize,
}
impl Default for Config {
fn default() -> Self {
- Config { max_depth: 6 }
+ Config { max_depth: 5 }
}
}
@@ -27,19 +52,19 @@ impl Config {
}
impl Config {
- pub fn from_bytes<T: de::Deserialize>(&self,
- input: &[u8])
- -> Result<T, Error> {
+ pub fn deserialize_bytes<T: de::Deserialize>(&self,
+ input: &[u8])
+ -> Result<T, Error> {
T::deserialize(Deserializer::with_config(self, input))
}
- pub fn from_str<T: de::Deserialize>(&self,
- input: &str)
- -> Result<T, Error> {
- self.from_bytes(input.as_bytes())
+ pub fn deserialize_str<T: de::Deserialize>(&self,
+ input: &str)
+ -> Result<T, Error> {
+ self.deserialize_bytes(input.as_bytes())
}
- pub fn from_reader<T, R>(&self, mut reader: R) -> Result<T, Error>
+ pub fn deserialize_reader<T, R>(&self, mut reader: R) -> Result<T, Error>
where T: de::Deserialize,
R: Read,
{
@@ -48,7 +73,7 @@ impl Config {
.map_err(|e| {
de::Error::custom(format_args!("could not read input: {}", e))
})?;
- self.from_bytes(&buf)
+ self.deserialize_bytes(&buf)
// from_bytes(&buf)
// T::deserialize(Deserializer::with_config(self, input.as_bytes()))
}
@@ -81,7 +106,7 @@ impl Config {
/// # }
/// ```
pub fn from_bytes<T: de::Deserialize>(input: &[u8]) -> Result<T, Error> {
- Config::default().from_bytes(input)
+ Config::default().deserialize_bytes(input)
}
/// Deserializes a querystring from a `&str`.
@@ -194,14 +219,12 @@ impl<I: Iterator<Item = u8>> Parser<I> {
fn peek(&mut self) -> Option<<Self as Iterator>::Item> {
if !self.acc.is_empty() {
self.peeked
+ } else if let Some(x) = self.inner.next() {
+ self.acc.push(x);
+ self.peeked = Some(x);
+ Some(x)
} else {
- if let Some(x) = self.inner.next() {
- self.acc.push(x);
- self.peeked = Some(x);
- Some(x)
- } else {
- None
- }
+ None
}
}
@@ -421,7 +444,7 @@ impl Deserializer {
let map = BTreeMap::default();
let mut root = Level::Nested(map);
- let decoded = percent_encoding::percent_decode(&input);
+ let decoded = percent_encoding::percent_decode(input);
let mut parser = Parser {
inner: decoded,
acc: Vec::new(),
diff --git a/src/lib.rs b/src/lib.rs
index b14df1b..d11e49f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -5,7 +5,7 @@
//!
//! This library aims for compatability with the syntax of
//! [qs](https://github.com/ljharb/qs) and also of the
-//! [Rack::Utils::parse_nested_query](http://www.rubydoc.info/github/rack/rack/Rack/Utils#parse_nested_query-class_method)
+//! [`Rack::Utils::parse_nested_query`](http://www.rubydoc.info/github/rack/rack/Rack/Utils#parse_nested_query-class_method)
//! implementation.
//!
//! For users who do *not* require nested URL parameters, it is highly
@@ -66,10 +66,6 @@ extern crate dtoa;
extern crate serde;
extern crate url;
-#[macro_use]
-extern crate serde_derive;
-
-
pub mod de;
pub mod ser;
diff --git a/src/ser/mod.rs b/src/ser/mod.rs
index 26845b3..8b3e41c 100644
--- a/src/ser/mod.rs
+++ b/src/ser/mod.rs
@@ -460,8 +460,8 @@ impl<'output, Target> ser::SerializeMap for MapSerializer<'output, Target>
value: &T)
-> Result<(), Error> {
{
- let key = self.key.as_ref().ok_or_else(|| Error::no_key())?;
- let value_sink = value::ValueSink::new(self.urlencoder, &key);
+ let key = self.key.as_ref().ok_or_else(Error::no_key)?;
+ let value_sink = value::ValueSink::new(self.urlencoder, key);
value.serialize(part::PartSerializer::new(value_sink))?;
}
self.key = None;