1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

clippy warnings and fmt

This commit is contained in:
Nikolay Kim 2018-07-16 11:17:45 +06:00
parent 5888f01317
commit 22385505a3
12 changed files with 327 additions and 331 deletions

View File

@ -585,11 +585,13 @@ impl<S: 'static> StaticFiles<S> {
/// Create new `StaticFiles` instance for specified base directory and
/// `CpuPool`.
pub fn with_pool<T: Into<PathBuf>>(dir: T, pool: CpuPool) -> Result<StaticFiles<S>, Error> {
pub fn with_pool<T: Into<PathBuf>>(
dir: T, pool: CpuPool,
) -> Result<StaticFiles<S>, Error> {
let dir = dir.into().canonicalize()?;
if !dir.is_dir() {
return Err(StaticFileError::IsNotDirectory.into())
return Err(StaticFileError::IsNotDirectory.into());
}
Ok(StaticFiles {
@ -640,7 +642,9 @@ impl<S: 'static> StaticFiles<S> {
self
}
fn try_handle(&self, req: &HttpRequest<S>) -> Result<AsyncResult<HttpResponse>, Error> {
fn try_handle(
&self, req: &HttpRequest<S>,
) -> Result<AsyncResult<HttpResponse>, Error> {
let tail: String = req.match_info().query("tail")?;
let relpath = PathBuf::from_param(tail.trim_left_matches('/'))?;
@ -971,7 +975,10 @@ mod tests {
#[test]
fn test_named_file_ranges_status_code() {
let mut srv = test::TestServer::with_factory(|| {
App::new().handler("test", StaticFiles::new(".").unwrap().index_file("Cargo.toml"))
App::new().handler(
"test",
StaticFiles::new(".").unwrap().index_file("Cargo.toml"),
)
});
// Valid range header
@ -1001,7 +1008,9 @@ mod tests {
let mut srv = test::TestServer::with_factory(|| {
App::new().handler(
"test",
StaticFiles::new(".").unwrap().index_file("tests/test.binary"),
StaticFiles::new(".")
.unwrap()
.index_file("tests/test.binary"),
)
});
@ -1049,7 +1058,9 @@ mod tests {
let mut srv = test::TestServer::with_factory(|| {
App::new().handler(
"test",
StaticFiles::new(".").unwrap().index_file("tests/test.binary"),
StaticFiles::new(".")
.unwrap()
.index_file("tests/test.binary"),
)
});
@ -1167,7 +1178,9 @@ mod tests {
#[test]
fn test_static_files() {
let mut st = StaticFiles::new(".").unwrap().show_files_listing();
let req = TestRequest::with_uri("/missing").param("tail", "missing").finish();
let req = TestRequest::with_uri("/missing")
.param("tail", "missing")
.finish();
let resp = st.handle(&req).respond_to(&req).unwrap();
let resp = resp.as_msg();
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
@ -1202,14 +1215,20 @@ mod tests {
#[test]
fn test_default_handler_file_missing() {
let st = StaticFiles::new(".").unwrap()
let st = StaticFiles::new(".")
.unwrap()
.default_handler(|_: &_| "default content");
let req = TestRequest::with_uri("/missing").param("tail", "missing").finish();
let req = TestRequest::with_uri("/missing")
.param("tail", "missing")
.finish();
let resp = st.handle(&req).respond_to(&req).unwrap();
let resp = resp.as_msg();
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.body(), &Body::Binary(Binary::Slice(b"default content")));
assert_eq!(
resp.body(),
&Body::Binary(Binary::Slice(b"default content"))
);
}
#[test]
@ -1282,7 +1301,10 @@ mod tests {
#[test]
fn integration_redirect_to_index() {
let mut srv = test::TestServer::with_factory(|| {
App::new().handler("test", StaticFiles::new(".").unwrap().index_file("Cargo.toml"))
App::new().handler(
"test",
StaticFiles::new(".").unwrap().index_file("Cargo.toml"),
)
});
let request = srv.get().uri(srv.url("/test")).finish().unwrap();
@ -1311,7 +1333,10 @@ mod tests {
#[test]
fn integration_percent_encoded() {
let mut srv = test::TestServer::with_factory(|| {
App::new().handler("test", StaticFiles::new(".").unwrap().index_file("Cargo.toml"))
App::new().handler(
"test",
StaticFiles::new(".").unwrap().index_file("Cargo.toml"),
)
});
let request = srv

View File

@ -151,7 +151,6 @@ extern crate openssl;
#[cfg(feature = "openssl")]
extern crate tokio_openssl;
mod serde_urlencoded;
mod application;
mod body;
mod context;
@ -174,6 +173,7 @@ mod resource;
mod route;
mod router;
mod scope;
mod serde_urlencoded;
mod uri;
mod with;

View File

@ -175,8 +175,7 @@ pub fn Method<S: 'static>(method: http::Method) -> MethodPredicate<S> {
/// Return predicate that matches if request contains specified header and
/// value.
pub fn Header<S: 'static>(
name: &'static str,
value: &'static str,
name: &'static str, value: &'static str,
) -> HeaderPredicate<S> {
HeaderPredicate(
header::HeaderName::try_from(name).unwrap(),

View File

@ -1,13 +1,15 @@
//! Deserialization support for the `application/x-www-form-urlencoded` format.
use serde::de::{self, DeserializeSeed, EnumAccess, IntoDeserializer, VariantAccess, Visitor};
use serde::de::Error as de_Error;
use serde::de::{
self, DeserializeSeed, EnumAccess, IntoDeserializer, VariantAccess, Visitor,
};
use serde::de::value::MapDeserializer;
use std::borrow::Cow;
use std::io::Read;
use url::form_urlencoded::Parse as UrlEncodedParse;
use url::form_urlencoded::parse;
use url::form_urlencoded::Parse as UrlEncodedParse;
#[doc(inline)]
pub use serde::de::value::Error;
@ -28,7 +30,8 @@ pub use serde::de::value::Error;
/// Ok(meal));
/// ```
pub fn from_bytes<'de, T>(input: &'de [u8]) -> Result<T, Error>
where T: de::Deserialize<'de>,
where
T: de::Deserialize<'de>,
{
T::deserialize(Deserializer::new(parse(input)))
}
@ -49,7 +52,8 @@ pub fn from_bytes<'de, T>(input: &'de [u8]) -> Result<T, Error>
/// Ok(meal));
/// ```
pub fn from_str<'de, T>(input: &'de str) -> Result<T, Error>
where T: de::Deserialize<'de>,
where
T: de::Deserialize<'de>,
{
from_bytes(input.as_bytes())
}
@ -58,14 +62,14 @@ pub fn from_str<'de, T>(input: &'de str) -> Result<T, Error>
/// 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::DeserializeOwned,
where
T: de::DeserializeOwned,
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))
})?;
reader
.read_to_end(&mut buf)
.map_err(|e| de::Error::custom(format_args!("could not read input: {}", e)))?;
from_bytes(&buf)
}
@ -95,25 +99,29 @@ impl<'de> de::Deserializer<'de> for Deserializer<'de> {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor<'de>,
where
V: de::Visitor<'de>,
{
self.deserialize_map(visitor)
}
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor<'de>,
where
V: de::Visitor<'de>,
{
visitor.visit_map(self.inner)
}
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor<'de>,
where
V: de::Visitor<'de>,
{
visitor.visit_seq(self.inner)
}
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor<'de>,
where
V: de::Visitor<'de>,
{
self.inner.end()?;
visitor.visit_unit()
@ -160,8 +168,7 @@ impl<'de> Iterator for PartIterator<'de> {
struct Part<'de>(Cow<'de, str>);
impl<'de> IntoDeserializer<'de> for Part<'de>
{
impl<'de> IntoDeserializer<'de> for Part<'de> {
type Deserializer = Self;
fn into_deserializer(self) -> Self::Deserializer {
@ -188,19 +195,24 @@ impl<'de> de::Deserializer<'de> for Part<'de> {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor<'de>,
where
V: de::Visitor<'de>,
{
self.0.into_deserializer().deserialize_any(visitor)
}
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor<'de>,
where
V: de::Visitor<'de>,
{
visitor.visit_some(self)
}
fn deserialize_enum<V>(self, _name: &'static str, _variants: &'static [&'static str], visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor<'de>,
fn deserialize_enum<V>(
self, _name: &'static str, _variants: &'static [&'static str], visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
visitor.visit_enum(ValueEnumAccess { value: self.0 })
}
@ -248,11 +260,9 @@ impl<'de> EnumAccess<'de> for ValueEnumAccess<'de> {
type Error = Error;
type Variant = UnitOnlyVariantAccess;
fn variant_seed<V>(
self,
seed: V,
) -> Result<(V::Value, Self::Variant), Self::Error>
where V: DeserializeSeed<'de>,
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
where
V: DeserializeSeed<'de>,
{
let variant = seed.deserialize(self.value.into_deserializer())?;
Ok((variant, UnitOnlyVariantAccess))
@ -271,27 +281,24 @@ impl<'de> VariantAccess<'de> for UnitOnlyVariantAccess {
}
fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value, Self::Error>
where T: DeserializeSeed<'de>,
where
T: DeserializeSeed<'de>,
{
Err(Error::custom("expected unit variant"))
}
fn tuple_variant<V>(
self,
_len: usize,
_visitor: V,
) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,
fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(Error::custom("expected unit variant"))
}
fn struct_variant<V>(
self,
_fields: &'static [&'static str],
_visitor: V,
self, _fields: &'static [&'static str], _visitor: V,
) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,
where
V: Visitor<'de>,
{
Err(Error::custom("expected unit variant"))
}

View File

@ -1,15 +1,15 @@
//! `x-www-form-urlencoded` meets Serde
extern crate itoa;
extern crate dtoa;
extern crate itoa;
pub mod de;
pub mod ser;
#[doc(inline)]
pub use self::de::{Deserializer, from_bytes, from_reader, from_str};
pub use self::de::{from_bytes, from_reader, from_str, Deserializer};
#[doc(inline)]
pub use self::ser::{Serializer, to_string};
pub use self::ser::{to_string, Serializer};
#[cfg(test)]
mod tests {
@ -17,24 +17,21 @@ mod tests {
fn deserialize_bytes() {
let result = vec![("first".to_owned(), 23), ("last".to_owned(), 42)];
assert_eq!(super::from_bytes(b"first=23&last=42"),
Ok(result));
assert_eq!(super::from_bytes(b"first=23&last=42"), Ok(result));
}
#[test]
fn deserialize_str() {
let result = vec![("first".to_owned(), 23), ("last".to_owned(), 42)];
assert_eq!(super::from_str("first=23&last=42"),
Ok(result));
assert_eq!(super::from_str("first=23&last=42"), Ok(result));
}
#[test]
fn deserialize_reader() {
let result = vec![("first".to_owned(), 23), ("last".to_owned(), 42)];
assert_eq!(super::from_reader(b"first=23&last=42" as &[_]),
Ok(result));
assert_eq!(super::from_reader(b"first=23&last=42" as &[_]), Ok(result));
}
#[test]
@ -66,7 +63,7 @@ mod tests {
let result = vec![
("one".to_owned(), X::A),
("two".to_owned(), X::B),
("three".to_owned(), X::C)
("three".to_owned(), X::C),
];
assert_eq!(super::from_str("one=A&two=B&three=C"), Ok(result));
@ -76,39 +73,49 @@ mod tests {
fn serialize_option_map_int() {
let params = &[("first", Some(23)), ("middle", None), ("last", Some(42))];
assert_eq!(super::to_string(params),
Ok("first=23&last=42".to_owned()));
assert_eq!(super::to_string(params), Ok("first=23&last=42".to_owned()));
}
#[test]
fn serialize_option_map_string() {
let params =
&[("first", Some("hello")), ("middle", None), ("last", Some("world"))];
let params = &[
("first", Some("hello")),
("middle", None),
("last", Some("world")),
];
assert_eq!(super::to_string(params),
Ok("first=hello&last=world".to_owned()));
assert_eq!(
super::to_string(params),
Ok("first=hello&last=world".to_owned())
);
}
#[test]
fn serialize_option_map_bool() {
let params = &[("one", Some(true)), ("two", Some(false))];
assert_eq!(super::to_string(params),
Ok("one=true&two=false".to_owned()));
assert_eq!(
super::to_string(params),
Ok("one=true&two=false".to_owned())
);
}
#[test]
fn serialize_map_bool() {
let params = &[("one", true), ("two", false)];
assert_eq!(super::to_string(params),
Ok("one=true&two=false".to_owned()));
assert_eq!(
super::to_string(params),
Ok("one=true&two=false".to_owned())
);
}
#[test]
fn serialize_unit_enum() {
let params = &[("one", X::A), ("two", X::B), ("three", X::C)];
assert_eq!(super::to_string(params),
Ok("one=A&two=B&three=C".to_owned()));
assert_eq!(
super::to_string(params),
Ok("one=A&two=B&three=C".to_owned())
);
}
}

View File

@ -1,5 +1,5 @@
use super::super::ser::Error;
use super::super::ser::part::Sink;
use super::super::ser::Error;
use serde::Serialize;
use std::borrow::Cow;
use std::ops::Deref;
@ -34,21 +34,21 @@ pub struct KeySink<End> {
}
impl<End, Ok> KeySink<End>
where End: for<'key> FnOnce(Key<'key>) -> Result<Ok, Error>
where
End: for<'key> FnOnce(Key<'key>) -> Result<Ok, Error>,
{
pub fn new(end: End) -> Self {
KeySink { end: end }
KeySink { end }
}
}
impl<End, Ok> Sink for KeySink<End>
where End: for<'key> FnOnce(Key<'key>) -> Result<Ok, Error>
where
End: for<'key> FnOnce(Key<'key>) -> Result<Ok, Error>,
{
type Ok = Ok;
fn serialize_static_str(self,
value: &'static str)
-> Result<Ok, Error> {
fn serialize_static_str(self, value: &'static str) -> Result<Ok, Error> {
(self.end)(Key::Static(value))
}
@ -64,9 +64,7 @@ impl<End, Ok> Sink for KeySink<End>
Err(self.unsupported())
}
fn serialize_some<T: ?Sized + Serialize>(self,
_value: &T)
-> Result<Ok, Error> {
fn serialize_some<T: ?Sized + Serialize>(self, _value: &T) -> Result<Ok, Error> {
Err(self.unsupported())
}

View File

@ -49,7 +49,7 @@ pub struct Serializer<'output, Target: 'output + UrlEncodedTarget> {
impl<'output, Target: 'output + UrlEncodedTarget> Serializer<'output, Target> {
/// Returns a new `Serializer`.
pub fn new(urlencoder: &'output mut UrlEncodedSerializer<Target>) -> Self {
Serializer { urlencoder: urlencoder }
Serializer { urlencoder }
}
}
@ -137,7 +137,8 @@ pub struct StructVariantSerializer<'output, T: 'output + UrlEncodedTarget> {
}
impl<'output, Target> ser::Serializer for Serializer<'output, Target>
where Target: 'output + UrlEncodedTarget,
where
Target: 'output + UrlEncodedTarget,
{
type Ok = &'output mut UrlEncodedSerializer<Target>;
type Error = Error;
@ -225,38 +226,29 @@ impl<'output, Target> ser::Serializer for Serializer<'output, Target>
}
/// Returns an error.
fn serialize_unit_struct(self,
_name: &'static str)
-> Result<Self::Ok, Error> {
fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Error> {
Err(Error::top_level())
}
/// Returns an error.
fn serialize_unit_variant(self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str)
-> Result<Self::Ok, Error> {
fn serialize_unit_variant(
self, _name: &'static str, _variant_index: u32, _variant: &'static str,
) -> Result<Self::Ok, Error> {
Err(Error::top_level())
}
/// Serializes the inner value, ignoring the newtype name.
fn serialize_newtype_struct<T: ?Sized + ser::Serialize>
(self,
_name: &'static str,
value: &T)
-> Result<Self::Ok, Error> {
fn serialize_newtype_struct<T: ?Sized + ser::Serialize>(
self, _name: &'static str, value: &T,
) -> Result<Self::Ok, Error> {
value.serialize(self)
}
/// Returns an error.
fn serialize_newtype_variant<T: ?Sized + ser::Serialize>
(self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T)
-> Result<Self::Ok, Error> {
fn serialize_newtype_variant<T: ?Sized + ser::Serialize>(
self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_value: &T,
) -> Result<Self::Ok, Error> {
Err(Error::top_level())
}
@ -266,50 +258,43 @@ impl<'output, Target> ser::Serializer for Serializer<'output, Target>
}
/// Serializes the given value.
fn serialize_some<T: ?Sized + ser::Serialize>
(self,
value: &T)
-> Result<Self::Ok, Error> {
fn serialize_some<T: ?Sized + ser::Serialize>(
self, value: &T,
) -> Result<Self::Ok, Error> {
value.serialize(self)
}
/// Serialize a sequence, given length (if any) is ignored.
fn serialize_seq(self,
_len: Option<usize>)
-> Result<Self::SerializeSeq, Error> {
Ok(SeqSerializer { urlencoder: self.urlencoder })
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Error> {
Ok(SeqSerializer {
urlencoder: self.urlencoder,
})
}
/// Returns an error.
fn serialize_tuple(self,
_len: usize)
-> Result<Self::SerializeTuple, Error> {
Ok(TupleSerializer { urlencoder: self.urlencoder })
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Error> {
Ok(TupleSerializer {
urlencoder: self.urlencoder,
})
}
/// Returns an error.
fn serialize_tuple_struct(self,
_name: &'static str,
_len: usize)
-> Result<Self::SerializeTupleStruct, Error> {
fn serialize_tuple_struct(
self, _name: &'static str, _len: usize,
) -> Result<Self::SerializeTupleStruct, Error> {
Err(Error::top_level())
}
/// Returns an error.
fn serialize_tuple_variant
(self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize)
-> Result<Self::SerializeTupleVariant, Error> {
fn serialize_tuple_variant(
self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleVariant, Error> {
Err(Error::top_level())
}
/// Serializes a map, given length is ignored.
fn serialize_map(self,
_len: Option<usize>)
-> Result<Self::SerializeMap, Error> {
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Error> {
Ok(MapSerializer {
urlencoder: self.urlencoder,
key: None,
@ -317,34 +302,33 @@ impl<'output, Target> ser::Serializer for Serializer<'output, Target>
}
/// Serializes a struct, given length is ignored.
fn serialize_struct(self,
_name: &'static str,
_len: usize)
-> Result<Self::SerializeStruct, Error> {
Ok(StructSerializer { urlencoder: self.urlencoder })
fn serialize_struct(
self, _name: &'static str, _len: usize,
) -> Result<Self::SerializeStruct, Error> {
Ok(StructSerializer {
urlencoder: self.urlencoder,
})
}
/// Returns an error.
fn serialize_struct_variant
(self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize)
-> Result<Self::SerializeStructVariant, Error> {
fn serialize_struct_variant(
self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant, Error> {
Err(Error::top_level())
}
}
impl<'output, Target> ser::SerializeSeq for SeqSerializer<'output, Target>
where Target: 'output + UrlEncodedTarget,
where
Target: 'output + UrlEncodedTarget,
{
type Ok = &'output mut UrlEncodedSerializer<Target>;
type Error = Error;
fn serialize_element<T: ?Sized + ser::Serialize>(&mut self,
value: &T)
-> Result<(), Error> {
fn serialize_element<T: ?Sized + ser::Serialize>(
&mut self, value: &T,
) -> Result<(), Error> {
value.serialize(pair::PairSerializer::new(self.urlencoder))
}
@ -354,14 +338,15 @@ impl<'output, Target> ser::SerializeSeq for SeqSerializer<'output, Target>
}
impl<'output, Target> ser::SerializeTuple for TupleSerializer<'output, Target>
where Target: 'output + UrlEncodedTarget,
where
Target: 'output + UrlEncodedTarget,
{
type Ok = &'output mut UrlEncodedSerializer<Target>;
type Error = Error;
fn serialize_element<T: ?Sized + ser::Serialize>(&mut self,
value: &T)
-> Result<(), Error> {
fn serialize_element<T: ?Sized + ser::Serialize>(
&mut self, value: &T,
) -> Result<(), Error> {
value.serialize(pair::PairSerializer::new(self.urlencoder))
}
@ -371,16 +356,16 @@ impl<'output, Target> ser::SerializeTuple for TupleSerializer<'output, Target>
}
impl<'output, Target> ser::SerializeTupleStruct
for
TupleStructSerializer<'output, Target>
where Target: 'output + UrlEncodedTarget,
for TupleStructSerializer<'output, Target>
where
Target: 'output + UrlEncodedTarget,
{
type Ok = &'output mut UrlEncodedSerializer<Target>;
type Error = Error;
fn serialize_field<T: ?Sized + ser::Serialize>(&mut self,
value: &T)
-> Result<(), Error> {
fn serialize_field<T: ?Sized + ser::Serialize>(
&mut self, value: &T,
) -> Result<(), Error> {
self.inner.serialize_field(value)
}
@ -390,16 +375,16 @@ impl<'output, Target> ser::SerializeTupleStruct
}
impl<'output, Target> ser::SerializeTupleVariant
for
TupleVariantSerializer<'output, Target>
where Target: 'output + UrlEncodedTarget,
for TupleVariantSerializer<'output, Target>
where
Target: 'output + UrlEncodedTarget,
{
type Ok = &'output mut UrlEncodedSerializer<Target>;
type Error = Error;
fn serialize_field<T: ?Sized + ser::Serialize>(&mut self,
value: &T)
-> Result<(), Error> {
fn serialize_field<T: ?Sized + ser::Serialize>(
&mut self, value: &T,
) -> Result<(), Error> {
self.inner.serialize_field(value)
}
@ -409,16 +394,15 @@ impl<'output, Target> ser::SerializeTupleVariant
}
impl<'output, Target> ser::SerializeMap for MapSerializer<'output, Target>
where Target: 'output + UrlEncodedTarget,
where
Target: 'output + UrlEncodedTarget,
{
type Ok = &'output mut UrlEncodedSerializer<Target>;
type Error = Error;
fn serialize_entry<K: ?Sized + ser::Serialize, V: ?Sized + ser::Serialize>
(&mut self,
key: &K,
value: &V)
-> Result<(), Error> {
fn serialize_entry<K: ?Sized + ser::Serialize, V: ?Sized + ser::Serialize>(
&mut self, key: &K, value: &V,
) -> Result<(), Error> {
let key_sink = key::KeySink::new(|key| {
let value_sink = value::ValueSink::new(self.urlencoder, &key);
value.serialize(part::PartSerializer::new(value_sink))?;
@ -429,20 +413,20 @@ impl<'output, Target> ser::SerializeMap for MapSerializer<'output, Target>
key.serialize(entry_serializer)
}
fn serialize_key<T: ?Sized + ser::Serialize>(&mut self,
key: &T)
-> Result<(), Error> {
fn serialize_key<T: ?Sized + ser::Serialize>(
&mut self, key: &T,
) -> Result<(), Error> {
let key_sink = key::KeySink::new(|key| Ok(key.into()));
let key_serializer = part::PartSerializer::new(key_sink);
self.key = Some(key.serialize(key_serializer)?);
Ok(())
}
fn serialize_value<T: ?Sized + ser::Serialize>(&mut self,
value: &T)
-> Result<(), Error> {
fn serialize_value<T: ?Sized + ser::Serialize>(
&mut self, value: &T,
) -> Result<(), Error> {
{
let key = self.key.as_ref().ok_or_else(|| Error::no_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))?;
}
@ -456,15 +440,15 @@ impl<'output, Target> ser::SerializeMap for MapSerializer<'output, Target>
}
impl<'output, Target> ser::SerializeStruct for StructSerializer<'output, Target>
where Target: 'output + UrlEncodedTarget,
where
Target: 'output + UrlEncodedTarget,
{
type Ok = &'output mut UrlEncodedSerializer<Target>;
type Error = Error;
fn serialize_field<T: ?Sized + ser::Serialize>(&mut self,
key: &'static str,
value: &T)
-> Result<(), Error> {
fn serialize_field<T: ?Sized + ser::Serialize>(
&mut self, key: &'static str, value: &T,
) -> Result<(), Error> {
let value_sink = value::ValueSink::new(self.urlencoder, key);
value.serialize(part::PartSerializer::new(value_sink))
}
@ -475,17 +459,16 @@ impl<'output, Target> ser::SerializeStruct for StructSerializer<'output, Target>
}
impl<'output, Target> ser::SerializeStructVariant
for
StructVariantSerializer<'output, Target>
where Target: 'output + UrlEncodedTarget,
for StructVariantSerializer<'output, Target>
where
Target: 'output + UrlEncodedTarget,
{
type Ok = &'output mut UrlEncodedSerializer<Target>;
type Error = Error;
fn serialize_field<T: ?Sized + ser::Serialize>(&mut self,
key: &'static str,
value: &T)
-> Result<(), Error> {
fn serialize_field<T: ?Sized + ser::Serialize>(
&mut self, key: &'static str, value: &T,
) -> Result<(), Error> {
self.inner.serialize_field(key, value)
}

View File

@ -1,7 +1,7 @@
use super::super::ser::Error;
use super::super::ser::key::KeySink;
use super::super::ser::part::PartSerializer;
use super::super::ser::value::ValueSink;
use super::super::ser::Error;
use serde::ser;
use std::borrow::Cow;
use std::mem;
@ -14,18 +14,20 @@ pub struct PairSerializer<'target, Target: 'target + UrlEncodedTarget> {
}
impl<'target, Target> PairSerializer<'target, Target>
where Target: 'target + UrlEncodedTarget,
where
Target: 'target + UrlEncodedTarget,
{
pub fn new(urlencoder: &'target mut UrlEncodedSerializer<Target>) -> Self {
PairSerializer {
urlencoder: urlencoder,
urlencoder,
state: PairState::WaitingForKey,
}
}
}
impl<'target, Target> ser::Serializer for PairSerializer<'target, Target>
where Target: 'target + UrlEncodedTarget,
where
Target: 'target + UrlEncodedTarget,
{
type Ok = ();
type Error = Error;
@ -101,29 +103,22 @@ impl<'target, Target> ser::Serializer for PairSerializer<'target, Target>
Err(Error::unsupported_pair())
}
fn serialize_unit_variant(self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str)
-> Result<(), Error> {
fn serialize_unit_variant(
self, _name: &'static str, _variant_index: u32, _variant: &'static str,
) -> Result<(), Error> {
Err(Error::unsupported_pair())
}
fn serialize_newtype_struct<T: ?Sized + ser::Serialize>
(self,
_name: &'static str,
value: &T)
-> Result<(), Error> {
fn serialize_newtype_struct<T: ?Sized + ser::Serialize>(
self, _name: &'static str, value: &T,
) -> Result<(), Error> {
value.serialize(self)
}
fn serialize_newtype_variant<T: ?Sized + ser::Serialize>
(self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T)
-> Result<(), Error> {
fn serialize_newtype_variant<T: ?Sized + ser::Serialize>(
self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_value: &T,
) -> Result<(), Error> {
Err(Error::unsupported_pair())
}
@ -131,15 +126,11 @@ impl<'target, Target> ser::Serializer for PairSerializer<'target, Target>
Ok(())
}
fn serialize_some<T: ?Sized + ser::Serialize>(self,
value: &T)
-> Result<(), Error> {
fn serialize_some<T: ?Sized + ser::Serialize>(self, value: &T) -> Result<(), Error> {
value.serialize(self)
}
fn serialize_seq(self,
_len: Option<usize>)
-> Result<Self::SerializeSeq, Error> {
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Error> {
Err(Error::unsupported_pair())
}
@ -151,56 +142,47 @@ impl<'target, Target> ser::Serializer for PairSerializer<'target, Target>
}
}
fn serialize_tuple_struct(self,
_name: &'static str,
_len: usize)
-> Result<Self::SerializeTupleStruct, Error> {
fn serialize_tuple_struct(
self, _name: &'static str, _len: usize,
) -> Result<Self::SerializeTupleStruct, Error> {
Err(Error::unsupported_pair())
}
fn serialize_tuple_variant
(self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize)
-> Result<Self::SerializeTupleVariant, Error> {
fn serialize_tuple_variant(
self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleVariant, Error> {
Err(Error::unsupported_pair())
}
fn serialize_map(self,
_len: Option<usize>)
-> Result<Self::SerializeMap, Error> {
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Error> {
Err(Error::unsupported_pair())
}
fn serialize_struct(self,
_name: &'static str,
_len: usize)
-> Result<Self::SerializeStruct, Error> {
fn serialize_struct(
self, _name: &'static str, _len: usize,
) -> Result<Self::SerializeStruct, Error> {
Err(Error::unsupported_pair())
}
fn serialize_struct_variant
(self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize)
-> Result<Self::SerializeStructVariant, Error> {
fn serialize_struct_variant(
self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant, Error> {
Err(Error::unsupported_pair())
}
}
impl<'target, Target> ser::SerializeTuple for PairSerializer<'target, Target>
where Target: 'target + UrlEncodedTarget,
where
Target: 'target + UrlEncodedTarget,
{
type Ok = ();
type Error = Error;
fn serialize_element<T: ?Sized + ser::Serialize>(&mut self,
value: &T)
-> Result<(), Error> {
fn serialize_element<T: ?Sized + ser::Serialize>(
&mut self, value: &T,
) -> Result<(), Error> {
match mem::replace(&mut self.state, PairState::Done) {
PairState::WaitingForKey => {
let key_sink = KeySink::new(|key| Ok(key.into()));
@ -209,7 +191,7 @@ impl<'target, Target> ser::SerializeTuple for PairSerializer<'target, Target>
key: value.serialize(key_serializer)?,
};
Ok(())
},
}
PairState::WaitingForValue { key } => {
let result = {
let value_sink = ValueSink::new(self.urlencoder, &key);
@ -219,10 +201,10 @@ impl<'target, Target> ser::SerializeTuple for PairSerializer<'target, Target>
if result.is_ok() {
self.state = PairState::Done;
} else {
self.state = PairState::WaitingForValue { key: key };
self.state = PairState::WaitingForValue { key };
}
result
},
}
PairState::Done => Err(Error::done()),
}
}

View File

@ -1,4 +1,4 @@
use ::serde;
use serde;
use super::super::dtoa;
use super::super::itoa;
@ -11,25 +11,22 @@ pub struct PartSerializer<S> {
impl<S: Sink> PartSerializer<S> {
pub fn new(sink: S) -> Self {
PartSerializer { sink: sink }
PartSerializer { sink }
}
}
pub trait Sink: Sized {
type Ok;
fn serialize_static_str(self,
value: &'static str)
-> Result<Self::Ok, Error>;
fn serialize_static_str(self, value: &'static str) -> Result<Self::Ok, Error>;
fn serialize_str(self, value: &str) -> Result<Self::Ok, Error>;
fn serialize_string(self, value: String) -> Result<Self::Ok, Error>;
fn serialize_none(self) -> Result<Self::Ok, Error>;
fn serialize_some<T: ?Sized + serde::ser::Serialize>
(self,
value: &T)
-> Result<Self::Ok, Error>;
fn serialize_some<T: ?Sized + serde::ser::Serialize>(
self, value: &T,
) -> Result<Self::Ok, Error>;
fn unsupported(self) -> Error;
}
@ -46,7 +43,8 @@ impl<S: Sink> serde::ser::Serializer for PartSerializer<S> {
type SerializeStructVariant = serde::ser::Impossible<S::Ok, Error>;
fn serialize_bool(self, v: bool) -> Result<S::Ok, Error> {
self.sink.serialize_static_str(if v { "true" } else { "false" })
self.sink
.serialize_static_str(if v { "true" } else { "false" })
}
fn serialize_i8(self, v: i8) -> Result<S::Ok, Error> {
@ -109,32 +107,25 @@ impl<S: Sink> serde::ser::Serializer for PartSerializer<S> {
}
fn serialize_unit_struct(self, name: &'static str) -> Result<S::Ok, Error> {
self.sink.serialize_static_str(name.into())
self.sink.serialize_static_str(name)
}
fn serialize_unit_variant(self,
_name: &'static str,
_variant_index: u32,
variant: &'static str)
-> Result<S::Ok, Error> {
self.sink.serialize_static_str(variant.into())
fn serialize_unit_variant(
self, _name: &'static str, _variant_index: u32, variant: &'static str,
) -> Result<S::Ok, Error> {
self.sink.serialize_static_str(variant)
}
fn serialize_newtype_struct<T: ?Sized + serde::ser::Serialize>
(self,
_name: &'static str,
value: &T)
-> Result<S::Ok, Error> {
fn serialize_newtype_struct<T: ?Sized + serde::ser::Serialize>(
self, _name: &'static str, value: &T,
) -> Result<S::Ok, Error> {
value.serialize(self)
}
fn serialize_newtype_variant<T: ?Sized + serde::ser::Serialize>
(self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T)
-> Result<S::Ok, Error> {
fn serialize_newtype_variant<T: ?Sized + serde::ser::Serialize>(
self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_value: &T,
) -> Result<S::Ok, Error> {
Err(self.sink.unsupported())
}
@ -142,68 +133,55 @@ impl<S: Sink> serde::ser::Serializer for PartSerializer<S> {
self.sink.serialize_none()
}
fn serialize_some<T: ?Sized + serde::ser::Serialize>(self,
value: &T)
-> Result<S::Ok, Error> {
fn serialize_some<T: ?Sized + serde::ser::Serialize>(
self, value: &T,
) -> Result<S::Ok, Error> {
self.sink.serialize_some(value)
}
fn serialize_seq(self,
_len: Option<usize>)
-> Result<Self::SerializeSeq, Error> {
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Error> {
Err(self.sink.unsupported())
}
fn serialize_tuple(self,
_len: usize)
-> Result<Self::SerializeTuple, Error> {
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Error> {
Err(self.sink.unsupported())
}
fn serialize_tuple_struct(self,
_name: &'static str,
_len: usize)
-> Result<Self::SerializeTuple, Error> {
fn serialize_tuple_struct(
self, _name: &'static str, _len: usize,
) -> Result<Self::SerializeTuple, Error> {
Err(self.sink.unsupported())
}
fn serialize_tuple_variant
(self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize)
-> Result<Self::SerializeTupleVariant, Error> {
fn serialize_tuple_variant(
self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleVariant, Error> {
Err(self.sink.unsupported())
}
fn serialize_map(self,
_len: Option<usize>)
-> Result<Self::SerializeMap, Error> {
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Error> {
Err(self.sink.unsupported())
}
fn serialize_struct(self,
_name: &'static str,
_len: usize)
-> Result<Self::SerializeStruct, Error> {
fn serialize_struct(
self, _name: &'static str, _len: usize,
) -> Result<Self::SerializeStruct, Error> {
Err(self.sink.unsupported())
}
fn serialize_struct_variant
(self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize)
-> Result<Self::SerializeStructVariant, Error> {
fn serialize_struct_variant(
self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant, Error> {
Err(self.sink.unsupported())
}
}
impl<S: Sink> PartSerializer<S> {
fn serialize_integer<I>(self, value: I) -> Result<S::Ok, Error>
where I: itoa::Integer,
where
I: itoa::Integer,
{
let mut buf = [b'\0'; 20];
let len = itoa::write(&mut buf[..], value).unwrap();
@ -212,7 +190,8 @@ impl<S: Sink> PartSerializer<S> {
}
fn serialize_floating<F>(self, value: F) -> Result<S::Ok, Error>
where F: dtoa::Floating,
where
F: dtoa::Floating,
{
let mut buf = [b'\0'; 24];
let len = dtoa::write(&mut buf[..], value).unwrap();

View File

@ -1,32 +1,32 @@
use super::super::ser::Error;
use super::super::ser::part::{PartSerializer, Sink};
use super::super::ser::Error;
use serde::ser::Serialize;
use std::str;
use url::form_urlencoded::Serializer as UrlEncodedSerializer;
use url::form_urlencoded::Target as UrlEncodedTarget;
pub struct ValueSink<'key, 'target, Target>
where Target: 'target + UrlEncodedTarget,
where
Target: 'target + UrlEncodedTarget,
{
urlencoder: &'target mut UrlEncodedSerializer<Target>,
key: &'key str,
}
impl<'key, 'target, Target> ValueSink<'key, 'target, Target>
where Target: 'target + UrlEncodedTarget,
where
Target: 'target + UrlEncodedTarget,
{
pub fn new(urlencoder: &'target mut UrlEncodedSerializer<Target>,
key: &'key str)
-> Self {
ValueSink {
urlencoder: urlencoder,
key: key,
}
pub fn new(
urlencoder: &'target mut UrlEncodedSerializer<Target>, key: &'key str,
) -> Self {
ValueSink { urlencoder, key }
}
}
impl<'key, 'target, Target> Sink for ValueSink<'key, 'target, Target>
where Target: 'target + UrlEncodedTarget,
where
Target: 'target + UrlEncodedTarget,
{
type Ok = ();
@ -47,9 +47,9 @@ impl<'key, 'target, Target> Sink for ValueSink<'key, 'target, Target>
Ok(())
}
fn serialize_some<T: ?Sized + Serialize>(self,
value: &T)
-> Result<Self::Ok, Error> {
fn serialize_some<T: ?Sized + Serialize>(
self, value: &T,
) -> Result<Self::Ok, Error> {
value.serialize(PartSerializer::new(self))
}

View File

@ -443,7 +443,12 @@ fn test_default_headers() {
"\""
)));
let request_override = srv.get().header("User-Agent", "test").header("Accept-Encoding", "over_test").finish().unwrap();
let request_override = srv
.get()
.header("User-Agent", "test")
.header("Accept-Encoding", "over_test")
.finish()
.unwrap();
let repr_override = format!("{:?}", request_override);
assert!(repr_override.contains("\"user-agent\": \"test\""));
assert!(repr_override.contains("\"accept-encoding\": \"over_test\""));

View File

@ -94,7 +94,7 @@ fn test_query_extractor() {
#[derive(Deserialize, Debug)]
pub enum ResponseType {
Token,
Code
Code,
}
#[derive(Debug, Deserialize)]
@ -122,13 +122,24 @@ fn test_query_enum_extractor() {
// read response
let bytes = srv.execute(response.body()).unwrap();
assert_eq!(bytes, Bytes::from_static(b"AuthRequest { id: 64, response_type: Code }"));
assert_eq!(
bytes,
Bytes::from_static(b"AuthRequest { id: 64, response_type: Code }")
);
let request = srv.get().uri(srv.url("/index.html?id=64&response_type=Co")).finish().unwrap();
let request = srv
.get()
.uri(srv.url("/index.html?id=64&response_type=Co"))
.finish()
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
let request = srv.get().uri(srv.url("/index.html?response_type=Code")).finish().unwrap();
let request = srv
.get()
.uri(srv.url("/index.html?response_type=Code"))
.finish()
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}