1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-19 22:56:39 +00: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 /// Create new `StaticFiles` instance for specified base directory and
/// `CpuPool`. /// `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()?; let dir = dir.into().canonicalize()?;
if !dir.is_dir() { if !dir.is_dir() {
return Err(StaticFileError::IsNotDirectory.into()) return Err(StaticFileError::IsNotDirectory.into());
} }
Ok(StaticFiles { Ok(StaticFiles {
@ -640,7 +642,9 @@ impl<S: 'static> StaticFiles<S> {
self 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 tail: String = req.match_info().query("tail")?;
let relpath = PathBuf::from_param(tail.trim_left_matches('/'))?; let relpath = PathBuf::from_param(tail.trim_left_matches('/'))?;
@ -971,7 +975,10 @@ mod tests {
#[test] #[test]
fn test_named_file_ranges_status_code() { fn test_named_file_ranges_status_code() {
let mut srv = test::TestServer::with_factory(|| { 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 // Valid range header
@ -1001,7 +1008,9 @@ mod tests {
let mut srv = test::TestServer::with_factory(|| { let mut srv = test::TestServer::with_factory(|| {
App::new().handler( App::new().handler(
"test", "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(|| { let mut srv = test::TestServer::with_factory(|| {
App::new().handler( App::new().handler(
"test", "test",
StaticFiles::new(".").unwrap().index_file("tests/test.binary"), StaticFiles::new(".")
.unwrap()
.index_file("tests/test.binary"),
) )
}); });
@ -1167,7 +1178,9 @@ mod tests {
#[test] #[test]
fn test_static_files() { fn test_static_files() {
let mut st = StaticFiles::new(".").unwrap().show_files_listing(); 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 = st.handle(&req).respond_to(&req).unwrap();
let resp = resp.as_msg(); let resp = resp.as_msg();
assert_eq!(resp.status(), StatusCode::NOT_FOUND); assert_eq!(resp.status(), StatusCode::NOT_FOUND);
@ -1202,14 +1215,20 @@ mod tests {
#[test] #[test]
fn test_default_handler_file_missing() { fn test_default_handler_file_missing() {
let st = StaticFiles::new(".").unwrap() let st = StaticFiles::new(".")
.unwrap()
.default_handler(|_: &_| "default content"); .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 = st.handle(&req).respond_to(&req).unwrap();
let resp = resp.as_msg(); let resp = resp.as_msg();
assert_eq!(resp.status(), StatusCode::OK); 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] #[test]
@ -1282,7 +1301,10 @@ mod tests {
#[test] #[test]
fn integration_redirect_to_index() { fn integration_redirect_to_index() {
let mut srv = test::TestServer::with_factory(|| { 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(); let request = srv.get().uri(srv.url("/test")).finish().unwrap();
@ -1311,7 +1333,10 @@ mod tests {
#[test] #[test]
fn integration_percent_encoded() { fn integration_percent_encoded() {
let mut srv = test::TestServer::with_factory(|| { 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 let request = srv

View file

@ -151,7 +151,6 @@ extern crate openssl;
#[cfg(feature = "openssl")] #[cfg(feature = "openssl")]
extern crate tokio_openssl; extern crate tokio_openssl;
mod serde_urlencoded;
mod application; mod application;
mod body; mod body;
mod context; mod context;
@ -174,6 +173,7 @@ mod resource;
mod route; mod route;
mod router; mod router;
mod scope; mod scope;
mod serde_urlencoded;
mod uri; mod uri;
mod with; 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 /// Return predicate that matches if request contains specified header and
/// value. /// value.
pub fn Header<S: 'static>( pub fn Header<S: 'static>(
name: &'static str, name: &'static str, value: &'static str,
value: &'static str,
) -> HeaderPredicate<S> { ) -> HeaderPredicate<S> {
HeaderPredicate( HeaderPredicate(
header::HeaderName::try_from(name).unwrap(), header::HeaderName::try_from(name).unwrap(),

View file

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

View file

@ -1,15 +1,15 @@
//! `x-www-form-urlencoded` meets Serde //! `x-www-form-urlencoded` meets Serde
extern crate itoa;
extern crate dtoa; extern crate dtoa;
extern crate itoa;
pub mod de; pub mod de;
pub mod ser; pub mod ser;
#[doc(inline)] #[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)] #[doc(inline)]
pub use self::ser::{Serializer, to_string}; pub use self::ser::{to_string, Serializer};
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -17,24 +17,21 @@ mod tests {
fn deserialize_bytes() { fn deserialize_bytes() {
let result = vec![("first".to_owned(), 23), ("last".to_owned(), 42)]; let result = vec![("first".to_owned(), 23), ("last".to_owned(), 42)];
assert_eq!(super::from_bytes(b"first=23&last=42"), assert_eq!(super::from_bytes(b"first=23&last=42"), Ok(result));
Ok(result));
} }
#[test] #[test]
fn deserialize_str() { fn deserialize_str() {
let result = vec![("first".to_owned(), 23), ("last".to_owned(), 42)]; let result = vec![("first".to_owned(), 23), ("last".to_owned(), 42)];
assert_eq!(super::from_str("first=23&last=42"), assert_eq!(super::from_str("first=23&last=42"), Ok(result));
Ok(result));
} }
#[test] #[test]
fn deserialize_reader() { fn deserialize_reader() {
let result = vec![("first".to_owned(), 23), ("last".to_owned(), 42)]; let result = vec![("first".to_owned(), 23), ("last".to_owned(), 42)];
assert_eq!(super::from_reader(b"first=23&last=42" as &[_]), assert_eq!(super::from_reader(b"first=23&last=42" as &[_]), Ok(result));
Ok(result));
} }
#[test] #[test]
@ -66,7 +63,7 @@ mod tests {
let result = vec![ let result = vec![
("one".to_owned(), X::A), ("one".to_owned(), X::A),
("two".to_owned(), X::B), ("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)); assert_eq!(super::from_str("one=A&two=B&three=C"), Ok(result));
@ -76,39 +73,49 @@ mod tests {
fn serialize_option_map_int() { fn serialize_option_map_int() {
let params = &[("first", Some(23)), ("middle", None), ("last", Some(42))]; let params = &[("first", Some(23)), ("middle", None), ("last", Some(42))];
assert_eq!(super::to_string(params), assert_eq!(super::to_string(params), Ok("first=23&last=42".to_owned()));
Ok("first=23&last=42".to_owned()));
} }
#[test] #[test]
fn serialize_option_map_string() { fn serialize_option_map_string() {
let params = let params = &[
&[("first", Some("hello")), ("middle", None), ("last", Some("world"))]; ("first", Some("hello")),
("middle", None),
("last", Some("world")),
];
assert_eq!(super::to_string(params), assert_eq!(
Ok("first=hello&last=world".to_owned())); super::to_string(params),
Ok("first=hello&last=world".to_owned())
);
} }
#[test] #[test]
fn serialize_option_map_bool() { fn serialize_option_map_bool() {
let params = &[("one", Some(true)), ("two", Some(false))]; let params = &[("one", Some(true)), ("two", Some(false))];
assert_eq!(super::to_string(params), assert_eq!(
Ok("one=true&two=false".to_owned())); super::to_string(params),
Ok("one=true&two=false".to_owned())
);
} }
#[test] #[test]
fn serialize_map_bool() { fn serialize_map_bool() {
let params = &[("one", true), ("two", false)]; let params = &[("one", true), ("two", false)];
assert_eq!(super::to_string(params), assert_eq!(
Ok("one=true&two=false".to_owned())); super::to_string(params),
Ok("one=true&two=false".to_owned())
);
} }
#[test] #[test]
fn serialize_unit_enum() { fn serialize_unit_enum() {
let params = &[("one", X::A), ("two", X::B), ("three", X::C)]; let params = &[("one", X::A), ("two", X::B), ("three", X::C)];
assert_eq!(super::to_string(params), assert_eq!(
Ok("one=A&two=B&three=C".to_owned())); 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::part::Sink;
use super::super::ser::Error;
use serde::Serialize; use serde::Serialize;
use std::borrow::Cow; use std::borrow::Cow;
use std::ops::Deref; use std::ops::Deref;
@ -34,21 +34,21 @@ pub struct KeySink<End> {
} }
impl<End, Ok> 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 { pub fn new(end: End) -> Self {
KeySink { end: end } KeySink { end }
} }
} }
impl<End, Ok> Sink for 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; type Ok = Ok;
fn serialize_static_str(self, fn serialize_static_str(self, value: &'static str) -> Result<Ok, Error> {
value: &'static str)
-> Result<Ok, Error> {
(self.end)(Key::Static(value)) (self.end)(Key::Static(value))
} }
@ -64,9 +64,7 @@ impl<End, Ok> Sink for KeySink<End>
Err(self.unsupported()) Err(self.unsupported())
} }
fn serialize_some<T: ?Sized + Serialize>(self, fn serialize_some<T: ?Sized + Serialize>(self, _value: &T) -> Result<Ok, Error> {
_value: &T)
-> Result<Ok, Error> {
Err(self.unsupported()) Err(self.unsupported())
} }

View file

@ -49,7 +49,7 @@ pub struct Serializer<'output, Target: 'output + UrlEncodedTarget> {
impl<'output, Target: 'output + UrlEncodedTarget> Serializer<'output, Target> { impl<'output, Target: 'output + UrlEncodedTarget> Serializer<'output, Target> {
/// Returns a new `Serializer`. /// Returns a new `Serializer`.
pub fn new(urlencoder: &'output mut UrlEncodedSerializer<Target>) -> Self { 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> impl<'output, Target> ser::Serializer for Serializer<'output, Target>
where Target: 'output + UrlEncodedTarget, where
Target: 'output + UrlEncodedTarget,
{ {
type Ok = &'output mut UrlEncodedSerializer<Target>; type Ok = &'output mut UrlEncodedSerializer<Target>;
type Error = Error; type Error = Error;
@ -225,38 +226,29 @@ impl<'output, Target> ser::Serializer for Serializer<'output, Target>
} }
/// Returns an error. /// Returns an error.
fn serialize_unit_struct(self, fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Error> {
_name: &'static str)
-> Result<Self::Ok, Error> {
Err(Error::top_level()) Err(Error::top_level())
} }
/// Returns an error. /// Returns an error.
fn serialize_unit_variant(self, fn serialize_unit_variant(
_name: &'static str, self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_variant_index: u32, ) -> Result<Self::Ok, Error> {
_variant: &'static str)
-> Result<Self::Ok, Error> {
Err(Error::top_level()) Err(Error::top_level())
} }
/// Serializes the inner value, ignoring the newtype name. /// Serializes the inner value, ignoring the newtype name.
fn serialize_newtype_struct<T: ?Sized + ser::Serialize> fn serialize_newtype_struct<T: ?Sized + ser::Serialize>(
(self, self, _name: &'static str, value: &T,
_name: &'static str, ) -> Result<Self::Ok, Error> {
value: &T)
-> Result<Self::Ok, Error> {
value.serialize(self) value.serialize(self)
} }
/// Returns an error. /// Returns an error.
fn serialize_newtype_variant<T: ?Sized + ser::Serialize> fn serialize_newtype_variant<T: ?Sized + ser::Serialize>(
(self, self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_name: &'static str, _value: &T,
_variant_index: u32, ) -> Result<Self::Ok, Error> {
_variant: &'static str,
_value: &T)
-> Result<Self::Ok, Error> {
Err(Error::top_level()) Err(Error::top_level())
} }
@ -266,50 +258,43 @@ impl<'output, Target> ser::Serializer for Serializer<'output, Target>
} }
/// Serializes the given value. /// Serializes the given value.
fn serialize_some<T: ?Sized + ser::Serialize> fn serialize_some<T: ?Sized + ser::Serialize>(
(self, self, value: &T,
value: &T) ) -> Result<Self::Ok, Error> {
-> Result<Self::Ok, Error> {
value.serialize(self) value.serialize(self)
} }
/// Serialize a sequence, given length (if any) is ignored. /// Serialize a sequence, given length (if any) is ignored.
fn serialize_seq(self, fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Error> {
_len: Option<usize>) Ok(SeqSerializer {
-> Result<Self::SerializeSeq, Error> { urlencoder: self.urlencoder,
Ok(SeqSerializer { urlencoder: self.urlencoder }) })
} }
/// Returns an error. /// Returns an error.
fn serialize_tuple(self, fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Error> {
_len: usize) Ok(TupleSerializer {
-> Result<Self::SerializeTuple, Error> { urlencoder: self.urlencoder,
Ok(TupleSerializer { urlencoder: self.urlencoder }) })
} }
/// Returns an error. /// Returns an error.
fn serialize_tuple_struct(self, fn serialize_tuple_struct(
_name: &'static str, self, _name: &'static str, _len: usize,
_len: usize) ) -> Result<Self::SerializeTupleStruct, Error> {
-> Result<Self::SerializeTupleStruct, Error> {
Err(Error::top_level()) Err(Error::top_level())
} }
/// Returns an error. /// Returns an error.
fn serialize_tuple_variant fn serialize_tuple_variant(
(self, self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_name: &'static str, _len: usize,
_variant_index: u32, ) -> Result<Self::SerializeTupleVariant, Error> {
_variant: &'static str,
_len: usize)
-> Result<Self::SerializeTupleVariant, Error> {
Err(Error::top_level()) Err(Error::top_level())
} }
/// Serializes a map, given length is ignored. /// Serializes a map, given length is ignored.
fn serialize_map(self, fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Error> {
_len: Option<usize>)
-> Result<Self::SerializeMap, Error> {
Ok(MapSerializer { Ok(MapSerializer {
urlencoder: self.urlencoder, urlencoder: self.urlencoder,
key: None, key: None,
@ -317,34 +302,33 @@ impl<'output, Target> ser::Serializer for Serializer<'output, Target>
} }
/// Serializes a struct, given length is ignored. /// Serializes a struct, given length is ignored.
fn serialize_struct(self, fn serialize_struct(
_name: &'static str, self, _name: &'static str, _len: usize,
_len: usize) ) -> Result<Self::SerializeStruct, Error> {
-> Result<Self::SerializeStruct, Error> { Ok(StructSerializer {
Ok(StructSerializer { urlencoder: self.urlencoder }) urlencoder: self.urlencoder,
})
} }
/// Returns an error. /// Returns an error.
fn serialize_struct_variant fn serialize_struct_variant(
(self, self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_name: &'static str, _len: usize,
_variant_index: u32, ) -> Result<Self::SerializeStructVariant, Error> {
_variant: &'static str,
_len: usize)
-> Result<Self::SerializeStructVariant, Error> {
Err(Error::top_level()) Err(Error::top_level())
} }
} }
impl<'output, Target> ser::SerializeSeq for SeqSerializer<'output, Target> impl<'output, Target> ser::SerializeSeq for SeqSerializer<'output, Target>
where Target: 'output + UrlEncodedTarget, where
Target: 'output + UrlEncodedTarget,
{ {
type Ok = &'output mut UrlEncodedSerializer<Target>; type Ok = &'output mut UrlEncodedSerializer<Target>;
type Error = Error; type Error = Error;
fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, fn serialize_element<T: ?Sized + ser::Serialize>(
value: &T) &mut self, value: &T,
-> Result<(), Error> { ) -> Result<(), Error> {
value.serialize(pair::PairSerializer::new(self.urlencoder)) 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> impl<'output, Target> ser::SerializeTuple for TupleSerializer<'output, Target>
where Target: 'output + UrlEncodedTarget, where
Target: 'output + UrlEncodedTarget,
{ {
type Ok = &'output mut UrlEncodedSerializer<Target>; type Ok = &'output mut UrlEncodedSerializer<Target>;
type Error = Error; type Error = Error;
fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, fn serialize_element<T: ?Sized + ser::Serialize>(
value: &T) &mut self, value: &T,
-> Result<(), Error> { ) -> Result<(), Error> {
value.serialize(pair::PairSerializer::new(self.urlencoder)) 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 impl<'output, Target> ser::SerializeTupleStruct
for for TupleStructSerializer<'output, Target>
TupleStructSerializer<'output, Target> where
where Target: 'output + UrlEncodedTarget, Target: 'output + UrlEncodedTarget,
{ {
type Ok = &'output mut UrlEncodedSerializer<Target>; type Ok = &'output mut UrlEncodedSerializer<Target>;
type Error = Error; type Error = Error;
fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, fn serialize_field<T: ?Sized + ser::Serialize>(
value: &T) &mut self, value: &T,
-> Result<(), Error> { ) -> Result<(), Error> {
self.inner.serialize_field(value) self.inner.serialize_field(value)
} }
@ -390,16 +375,16 @@ impl<'output, Target> ser::SerializeTupleStruct
} }
impl<'output, Target> ser::SerializeTupleVariant impl<'output, Target> ser::SerializeTupleVariant
for for TupleVariantSerializer<'output, Target>
TupleVariantSerializer<'output, Target> where
where Target: 'output + UrlEncodedTarget, Target: 'output + UrlEncodedTarget,
{ {
type Ok = &'output mut UrlEncodedSerializer<Target>; type Ok = &'output mut UrlEncodedSerializer<Target>;
type Error = Error; type Error = Error;
fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, fn serialize_field<T: ?Sized + ser::Serialize>(
value: &T) &mut self, value: &T,
-> Result<(), Error> { ) -> Result<(), Error> {
self.inner.serialize_field(value) self.inner.serialize_field(value)
} }
@ -409,16 +394,15 @@ impl<'output, Target> ser::SerializeTupleVariant
} }
impl<'output, Target> ser::SerializeMap for MapSerializer<'output, Target> impl<'output, Target> ser::SerializeMap for MapSerializer<'output, Target>
where Target: 'output + UrlEncodedTarget, where
Target: 'output + UrlEncodedTarget,
{ {
type Ok = &'output mut UrlEncodedSerializer<Target>; type Ok = &'output mut UrlEncodedSerializer<Target>;
type Error = Error; type Error = Error;
fn serialize_entry<K: ?Sized + ser::Serialize, V: ?Sized + ser::Serialize> fn serialize_entry<K: ?Sized + ser::Serialize, V: ?Sized + ser::Serialize>(
(&mut self, &mut self, key: &K, value: &V,
key: &K, ) -> Result<(), Error> {
value: &V)
-> Result<(), Error> {
let key_sink = key::KeySink::new(|key| { let key_sink = key::KeySink::new(|key| {
let value_sink = value::ValueSink::new(self.urlencoder, &key); let value_sink = value::ValueSink::new(self.urlencoder, &key);
value.serialize(part::PartSerializer::new(value_sink))?; value.serialize(part::PartSerializer::new(value_sink))?;
@ -429,20 +413,20 @@ impl<'output, Target> ser::SerializeMap for MapSerializer<'output, Target>
key.serialize(entry_serializer) key.serialize(entry_serializer)
} }
fn serialize_key<T: ?Sized + ser::Serialize>(&mut self, fn serialize_key<T: ?Sized + ser::Serialize>(
key: &T) &mut self, key: &T,
-> Result<(), Error> { ) -> Result<(), Error> {
let key_sink = key::KeySink::new(|key| Ok(key.into())); let key_sink = key::KeySink::new(|key| Ok(key.into()));
let key_serializer = part::PartSerializer::new(key_sink); let key_serializer = part::PartSerializer::new(key_sink);
self.key = Some(key.serialize(key_serializer)?); self.key = Some(key.serialize(key_serializer)?);
Ok(()) Ok(())
} }
fn serialize_value<T: ?Sized + ser::Serialize>(&mut self, fn serialize_value<T: ?Sized + ser::Serialize>(
value: &T) &mut self, value: &T,
-> Result<(), Error> { ) -> 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); let value_sink = value::ValueSink::new(self.urlencoder, &key);
value.serialize(part::PartSerializer::new(value_sink))?; 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> impl<'output, Target> ser::SerializeStruct for StructSerializer<'output, Target>
where Target: 'output + UrlEncodedTarget, where
Target: 'output + UrlEncodedTarget,
{ {
type Ok = &'output mut UrlEncodedSerializer<Target>; type Ok = &'output mut UrlEncodedSerializer<Target>;
type Error = Error; type Error = Error;
fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, fn serialize_field<T: ?Sized + ser::Serialize>(
key: &'static str, &mut self, key: &'static str, value: &T,
value: &T) ) -> Result<(), Error> {
-> Result<(), Error> {
let value_sink = value::ValueSink::new(self.urlencoder, key); let value_sink = value::ValueSink::new(self.urlencoder, key);
value.serialize(part::PartSerializer::new(value_sink)) 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 impl<'output, Target> ser::SerializeStructVariant
for for StructVariantSerializer<'output, Target>
StructVariantSerializer<'output, Target> where
where Target: 'output + UrlEncodedTarget, Target: 'output + UrlEncodedTarget,
{ {
type Ok = &'output mut UrlEncodedSerializer<Target>; type Ok = &'output mut UrlEncodedSerializer<Target>;
type Error = Error; type Error = Error;
fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, fn serialize_field<T: ?Sized + ser::Serialize>(
key: &'static str, &mut self, key: &'static str, value: &T,
value: &T) ) -> Result<(), Error> {
-> Result<(), Error> {
self.inner.serialize_field(key, value) 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::key::KeySink;
use super::super::ser::part::PartSerializer; use super::super::ser::part::PartSerializer;
use super::super::ser::value::ValueSink; use super::super::ser::value::ValueSink;
use super::super::ser::Error;
use serde::ser; use serde::ser;
use std::borrow::Cow; use std::borrow::Cow;
use std::mem; use std::mem;
@ -14,18 +14,20 @@ pub struct PairSerializer<'target, Target: 'target + UrlEncodedTarget> {
} }
impl<'target, Target> PairSerializer<'target, Target> impl<'target, Target> PairSerializer<'target, Target>
where Target: 'target + UrlEncodedTarget, where
Target: 'target + UrlEncodedTarget,
{ {
pub fn new(urlencoder: &'target mut UrlEncodedSerializer<Target>) -> Self { pub fn new(urlencoder: &'target mut UrlEncodedSerializer<Target>) -> Self {
PairSerializer { PairSerializer {
urlencoder: urlencoder, urlencoder,
state: PairState::WaitingForKey, state: PairState::WaitingForKey,
} }
} }
} }
impl<'target, Target> ser::Serializer for PairSerializer<'target, Target> impl<'target, Target> ser::Serializer for PairSerializer<'target, Target>
where Target: 'target + UrlEncodedTarget, where
Target: 'target + UrlEncodedTarget,
{ {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
@ -101,29 +103,22 @@ impl<'target, Target> ser::Serializer for PairSerializer<'target, Target>
Err(Error::unsupported_pair()) Err(Error::unsupported_pair())
} }
fn serialize_unit_variant(self, fn serialize_unit_variant(
_name: &'static str, self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_variant_index: u32, ) -> Result<(), Error> {
_variant: &'static str)
-> Result<(), Error> {
Err(Error::unsupported_pair()) Err(Error::unsupported_pair())
} }
fn serialize_newtype_struct<T: ?Sized + ser::Serialize> fn serialize_newtype_struct<T: ?Sized + ser::Serialize>(
(self, self, _name: &'static str, value: &T,
_name: &'static str, ) -> Result<(), Error> {
value: &T)
-> Result<(), Error> {
value.serialize(self) value.serialize(self)
} }
fn serialize_newtype_variant<T: ?Sized + ser::Serialize> fn serialize_newtype_variant<T: ?Sized + ser::Serialize>(
(self, self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_name: &'static str, _value: &T,
_variant_index: u32, ) -> Result<(), Error> {
_variant: &'static str,
_value: &T)
-> Result<(), Error> {
Err(Error::unsupported_pair()) Err(Error::unsupported_pair())
} }
@ -131,15 +126,11 @@ impl<'target, Target> ser::Serializer for PairSerializer<'target, Target>
Ok(()) Ok(())
} }
fn serialize_some<T: ?Sized + ser::Serialize>(self, fn serialize_some<T: ?Sized + ser::Serialize>(self, value: &T) -> Result<(), Error> {
value: &T)
-> Result<(), Error> {
value.serialize(self) value.serialize(self)
} }
fn serialize_seq(self, fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Error> {
_len: Option<usize>)
-> Result<Self::SerializeSeq, Error> {
Err(Error::unsupported_pair()) Err(Error::unsupported_pair())
} }
@ -151,56 +142,47 @@ impl<'target, Target> ser::Serializer for PairSerializer<'target, Target>
} }
} }
fn serialize_tuple_struct(self, fn serialize_tuple_struct(
_name: &'static str, self, _name: &'static str, _len: usize,
_len: usize) ) -> Result<Self::SerializeTupleStruct, Error> {
-> Result<Self::SerializeTupleStruct, Error> {
Err(Error::unsupported_pair()) Err(Error::unsupported_pair())
} }
fn serialize_tuple_variant fn serialize_tuple_variant(
(self, self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_name: &'static str, _len: usize,
_variant_index: u32, ) -> Result<Self::SerializeTupleVariant, Error> {
_variant: &'static str,
_len: usize)
-> Result<Self::SerializeTupleVariant, Error> {
Err(Error::unsupported_pair()) Err(Error::unsupported_pair())
} }
fn serialize_map(self, fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Error> {
_len: Option<usize>)
-> Result<Self::SerializeMap, Error> {
Err(Error::unsupported_pair()) Err(Error::unsupported_pair())
} }
fn serialize_struct(self, fn serialize_struct(
_name: &'static str, self, _name: &'static str, _len: usize,
_len: usize) ) -> Result<Self::SerializeStruct, Error> {
-> Result<Self::SerializeStruct, Error> {
Err(Error::unsupported_pair()) Err(Error::unsupported_pair())
} }
fn serialize_struct_variant fn serialize_struct_variant(
(self, self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_name: &'static str, _len: usize,
_variant_index: u32, ) -> Result<Self::SerializeStructVariant, Error> {
_variant: &'static str,
_len: usize)
-> Result<Self::SerializeStructVariant, Error> {
Err(Error::unsupported_pair()) Err(Error::unsupported_pair())
} }
} }
impl<'target, Target> ser::SerializeTuple for PairSerializer<'target, Target> impl<'target, Target> ser::SerializeTuple for PairSerializer<'target, Target>
where Target: 'target + UrlEncodedTarget, where
Target: 'target + UrlEncodedTarget,
{ {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, fn serialize_element<T: ?Sized + ser::Serialize>(
value: &T) &mut self, value: &T,
-> Result<(), Error> { ) -> Result<(), Error> {
match mem::replace(&mut self.state, PairState::Done) { match mem::replace(&mut self.state, PairState::Done) {
PairState::WaitingForKey => { PairState::WaitingForKey => {
let key_sink = KeySink::new(|key| Ok(key.into())); 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)?, key: value.serialize(key_serializer)?,
}; };
Ok(()) Ok(())
}, }
PairState::WaitingForValue { key } => { PairState::WaitingForValue { key } => {
let result = { let result = {
let value_sink = ValueSink::new(self.urlencoder, &key); 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() { if result.is_ok() {
self.state = PairState::Done; self.state = PairState::Done;
} else { } else {
self.state = PairState::WaitingForValue { key: key }; self.state = PairState::WaitingForValue { key };
} }
result result
}, }
PairState::Done => Err(Error::done()), PairState::Done => Err(Error::done()),
} }
} }

View file

@ -1,4 +1,4 @@
use ::serde; use serde;
use super::super::dtoa; use super::super::dtoa;
use super::super::itoa; use super::super::itoa;
@ -11,25 +11,22 @@ pub struct PartSerializer<S> {
impl<S: Sink> PartSerializer<S> { impl<S: Sink> PartSerializer<S> {
pub fn new(sink: S) -> Self { pub fn new(sink: S) -> Self {
PartSerializer { sink: sink } PartSerializer { sink }
} }
} }
pub trait Sink: Sized { pub trait Sink: Sized {
type Ok; type Ok;
fn serialize_static_str(self, fn serialize_static_str(self, value: &'static str) -> Result<Self::Ok, Error>;
value: &'static str)
-> Result<Self::Ok, Error>;
fn serialize_str(self, value: &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_string(self, value: String) -> Result<Self::Ok, Error>;
fn serialize_none(self) -> Result<Self::Ok, Error>; fn serialize_none(self) -> Result<Self::Ok, Error>;
fn serialize_some<T: ?Sized + serde::ser::Serialize> fn serialize_some<T: ?Sized + serde::ser::Serialize>(
(self, self, value: &T,
value: &T) ) -> Result<Self::Ok, Error>;
-> Result<Self::Ok, Error>;
fn unsupported(self) -> 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>; type SerializeStructVariant = serde::ser::Impossible<S::Ok, Error>;
fn serialize_bool(self, v: bool) -> Result<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> { 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> { 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, fn serialize_unit_variant(
_name: &'static str, self, _name: &'static str, _variant_index: u32, variant: &'static str,
_variant_index: u32, ) -> Result<S::Ok, Error> {
variant: &'static str) self.sink.serialize_static_str(variant)
-> Result<S::Ok, Error> {
self.sink.serialize_static_str(variant.into())
} }
fn serialize_newtype_struct<T: ?Sized + serde::ser::Serialize> fn serialize_newtype_struct<T: ?Sized + serde::ser::Serialize>(
(self, self, _name: &'static str, value: &T,
_name: &'static str, ) -> Result<S::Ok, Error> {
value: &T)
-> Result<S::Ok, Error> {
value.serialize(self) value.serialize(self)
} }
fn serialize_newtype_variant<T: ?Sized + serde::ser::Serialize> fn serialize_newtype_variant<T: ?Sized + serde::ser::Serialize>(
(self, self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_name: &'static str, _value: &T,
_variant_index: u32, ) -> Result<S::Ok, Error> {
_variant: &'static str,
_value: &T)
-> Result<S::Ok, Error> {
Err(self.sink.unsupported()) Err(self.sink.unsupported())
} }
@ -142,68 +133,55 @@ impl<S: Sink> serde::ser::Serializer for PartSerializer<S> {
self.sink.serialize_none() self.sink.serialize_none()
} }
fn serialize_some<T: ?Sized + serde::ser::Serialize>(self, fn serialize_some<T: ?Sized + serde::ser::Serialize>(
value: &T) self, value: &T,
-> Result<S::Ok, Error> { ) -> Result<S::Ok, Error> {
self.sink.serialize_some(value) self.sink.serialize_some(value)
} }
fn serialize_seq(self, fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Error> {
_len: Option<usize>)
-> Result<Self::SerializeSeq, Error> {
Err(self.sink.unsupported()) Err(self.sink.unsupported())
} }
fn serialize_tuple(self, fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Error> {
_len: usize)
-> Result<Self::SerializeTuple, Error> {
Err(self.sink.unsupported()) Err(self.sink.unsupported())
} }
fn serialize_tuple_struct(self, fn serialize_tuple_struct(
_name: &'static str, self, _name: &'static str, _len: usize,
_len: usize) ) -> Result<Self::SerializeTuple, Error> {
-> Result<Self::SerializeTuple, Error> {
Err(self.sink.unsupported()) Err(self.sink.unsupported())
} }
fn serialize_tuple_variant fn serialize_tuple_variant(
(self, self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_name: &'static str, _len: usize,
_variant_index: u32, ) -> Result<Self::SerializeTupleVariant, Error> {
_variant: &'static str,
_len: usize)
-> Result<Self::SerializeTupleVariant, Error> {
Err(self.sink.unsupported()) Err(self.sink.unsupported())
} }
fn serialize_map(self, fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Error> {
_len: Option<usize>)
-> Result<Self::SerializeMap, Error> {
Err(self.sink.unsupported()) Err(self.sink.unsupported())
} }
fn serialize_struct(self, fn serialize_struct(
_name: &'static str, self, _name: &'static str, _len: usize,
_len: usize) ) -> Result<Self::SerializeStruct, Error> {
-> Result<Self::SerializeStruct, Error> {
Err(self.sink.unsupported()) Err(self.sink.unsupported())
} }
fn serialize_struct_variant fn serialize_struct_variant(
(self, self, _name: &'static str, _variant_index: u32, _variant: &'static str,
_name: &'static str, _len: usize,
_variant_index: u32, ) -> Result<Self::SerializeStructVariant, Error> {
_variant: &'static str,
_len: usize)
-> Result<Self::SerializeStructVariant, Error> {
Err(self.sink.unsupported()) Err(self.sink.unsupported())
} }
} }
impl<S: Sink> PartSerializer<S> { impl<S: Sink> PartSerializer<S> {
fn serialize_integer<I>(self, value: I) -> Result<S::Ok, Error> 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 mut buf = [b'\0'; 20];
let len = itoa::write(&mut buf[..], value).unwrap(); 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> 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 mut buf = [b'\0'; 24];
let len = dtoa::write(&mut buf[..], value).unwrap(); 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::part::{PartSerializer, Sink};
use super::super::ser::Error;
use serde::ser::Serialize; use serde::ser::Serialize;
use std::str; use std::str;
use url::form_urlencoded::Serializer as UrlEncodedSerializer; use url::form_urlencoded::Serializer as UrlEncodedSerializer;
use url::form_urlencoded::Target as UrlEncodedTarget; use url::form_urlencoded::Target as UrlEncodedTarget;
pub struct ValueSink<'key, 'target, Target> pub struct ValueSink<'key, 'target, Target>
where Target: 'target + UrlEncodedTarget, where
Target: 'target + UrlEncodedTarget,
{ {
urlencoder: &'target mut UrlEncodedSerializer<Target>, urlencoder: &'target mut UrlEncodedSerializer<Target>,
key: &'key str, key: &'key str,
} }
impl<'key, 'target, Target> ValueSink<'key, 'target, Target> impl<'key, 'target, Target> ValueSink<'key, 'target, Target>
where Target: 'target + UrlEncodedTarget, where
Target: 'target + UrlEncodedTarget,
{ {
pub fn new(urlencoder: &'target mut UrlEncodedSerializer<Target>, pub fn new(
key: &'key str) urlencoder: &'target mut UrlEncodedSerializer<Target>, key: &'key str,
-> Self { ) -> Self {
ValueSink { ValueSink { urlencoder, key }
urlencoder: urlencoder,
key: key,
}
} }
} }
impl<'key, 'target, Target> Sink for ValueSink<'key, 'target, Target> impl<'key, 'target, Target> Sink for ValueSink<'key, 'target, Target>
where Target: 'target + UrlEncodedTarget, where
Target: 'target + UrlEncodedTarget,
{ {
type Ok = (); type Ok = ();
@ -47,9 +47,9 @@ impl<'key, 'target, Target> Sink for ValueSink<'key, 'target, Target>
Ok(()) Ok(())
} }
fn serialize_some<T: ?Sized + Serialize>(self, fn serialize_some<T: ?Sized + Serialize>(
value: &T) self, value: &T,
-> Result<Self::Ok, Error> { ) -> Result<Self::Ok, Error> {
value.serialize(PartSerializer::new(self)) 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); let repr_override = format!("{:?}", request_override);
assert!(repr_override.contains("\"user-agent\": \"test\"")); assert!(repr_override.contains("\"user-agent\": \"test\""));
assert!(repr_override.contains("\"accept-encoding\": \"over_test\"")); assert!(repr_override.contains("\"accept-encoding\": \"over_test\""));

View file

@ -94,7 +94,7 @@ fn test_query_extractor() {
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub enum ResponseType { pub enum ResponseType {
Token, Token,
Code Code,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -122,13 +122,24 @@ fn test_query_enum_extractor() {
// read response // read response
let bytes = srv.execute(response.body()).unwrap(); 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(); let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST); 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(); let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST); assert_eq!(response.status(), StatusCode::BAD_REQUEST);
} }