From 22385505a3b868fcac63e6b7f15eb2bc5d22c71b Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 16 Jul 2018 11:17:45 +0600 Subject: [PATCH] clippy warnings and fmt --- src/fs.rs | 49 ++++++-- src/lib.rs | 2 +- src/pred.rs | 3 +- src/serde_urlencoded/de.rs | 79 ++++++------ src/serde_urlencoded/mod.rs | 51 ++++---- src/serde_urlencoded/ser/key.rs | 18 ++- src/serde_urlencoded/ser/mod.rs | 193 ++++++++++++++---------------- src/serde_urlencoded/ser/pair.rs | 100 +++++++--------- src/serde_urlencoded/ser/part.rs | 109 +++++++---------- src/serde_urlencoded/ser/value.rs | 28 ++--- tests/test_client.rs | 7 +- tests/test_handlers.rs | 19 ++- 12 files changed, 327 insertions(+), 331 deletions(-) diff --git a/src/fs.rs b/src/fs.rs index f42ef2e4e..14c3818ba 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -585,11 +585,13 @@ impl StaticFiles { /// Create new `StaticFiles` instance for specified base directory and /// `CpuPool`. - pub fn with_pool>(dir: T, pool: CpuPool) -> Result, Error> { + pub fn with_pool>( + dir: T, pool: CpuPool, + ) -> Result, 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 StaticFiles { self } - fn try_handle(&self, req: &HttpRequest) -> Result, Error> { + fn try_handle( + &self, req: &HttpRequest, + ) -> Result, 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 diff --git a/src/lib.rs b/src/lib.rs index 34dcd7182..da2249c87 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/pred.rs b/src/pred.rs index 432150efe..22f12ac2a 100644 --- a/src/pred.rs +++ b/src/pred.rs @@ -175,8 +175,7 @@ pub fn Method(method: http::Method) -> MethodPredicate { /// Return predicate that matches if request contains specified header and /// value. pub fn Header( - name: &'static str, - value: &'static str, + name: &'static str, value: &'static str, ) -> HeaderPredicate { HeaderPredicate( header::HeaderName::try_from(name).unwrap(), diff --git a/src/serde_urlencoded/de.rs b/src/serde_urlencoded/de.rs index affbfb37e..ae14afbf5 100644 --- a/src/serde_urlencoded/de.rs +++ b/src/serde_urlencoded/de.rs @@ -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 - 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 /// Ok(meal)); /// ``` pub fn from_str<'de, T>(input: &'de str) -> Result - 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 /// Convenience function that reads all bytes from `reader` and deserializes /// them with `from_bytes`. pub fn from_reader(mut reader: R) -> Result - where T: de::DeserializeOwned, - R: Read, +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(self, visitor: V) -> Result - where V: de::Visitor<'de>, + where + V: de::Visitor<'de>, { self.deserialize_map(visitor) } fn deserialize_map(self, visitor: V) -> Result - where V: de::Visitor<'de>, + where + V: de::Visitor<'de>, { visitor.visit_map(self.inner) } fn deserialize_seq(self, visitor: V) -> Result - where V: de::Visitor<'de>, + where + V: de::Visitor<'de>, { visitor.visit_seq(self.inner) } fn deserialize_unit(self, visitor: V) -> Result - 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(self, visitor: V) -> Result - where V: de::Visitor<'de>, + where + V: de::Visitor<'de>, { self.0.into_deserializer().deserialize_any(visitor) } fn deserialize_option(self, visitor: V) -> Result - where V: de::Visitor<'de>, + where + V: de::Visitor<'de>, { visitor.visit_some(self) } - fn deserialize_enum(self, _name: &'static str, _variants: &'static [&'static str], visitor: V) -> Result - where V: de::Visitor<'de>, + fn deserialize_enum( + self, _name: &'static str, _variants: &'static [&'static str], visitor: V, + ) -> Result + 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( - self, - seed: V, - ) -> Result<(V::Value, Self::Variant), Self::Error> - where V: DeserializeSeed<'de>, + fn variant_seed(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(self, _seed: T) -> Result - where T: DeserializeSeed<'de>, + where + T: DeserializeSeed<'de>, { Err(Error::custom("expected unit variant")) } - fn tuple_variant( - self, - _len: usize, - _visitor: V, - ) -> Result - where V: Visitor<'de>, + fn tuple_variant(self, _len: usize, _visitor: V) -> Result + where + V: Visitor<'de>, { Err(Error::custom("expected unit variant")) } fn struct_variant( - self, - _fields: &'static [&'static str], - _visitor: V, + self, _fields: &'static [&'static str], _visitor: V, ) -> Result - where V: Visitor<'de>, + where + V: Visitor<'de>, { Err(Error::custom("expected unit variant")) } diff --git a/src/serde_urlencoded/mod.rs b/src/serde_urlencoded/mod.rs index 6ee62c2a9..7e2cf33ae 100644 --- a/src/serde_urlencoded/mod.rs +++ b/src/serde_urlencoded/mod.rs @@ -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()) + ); } } diff --git a/src/serde_urlencoded/ser/key.rs b/src/serde_urlencoded/ser/key.rs index 2d138f18e..48497a558 100644 --- a/src/serde_urlencoded/ser/key.rs +++ b/src/serde_urlencoded/ser/key.rs @@ -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 { } impl KeySink - where End: for<'key> FnOnce(Key<'key>) -> Result +where + End: for<'key> FnOnce(Key<'key>) -> Result, { pub fn new(end: End) -> Self { - KeySink { end: end } + KeySink { end } } } impl Sink for KeySink - where End: for<'key> FnOnce(Key<'key>) -> Result +where + End: for<'key> FnOnce(Key<'key>) -> Result, { type Ok = Ok; - fn serialize_static_str(self, - value: &'static str) - -> Result { + fn serialize_static_str(self, value: &'static str) -> Result { (self.end)(Key::Static(value)) } @@ -64,9 +64,7 @@ impl Sink for KeySink Err(self.unsupported()) } - fn serialize_some(self, - _value: &T) - -> Result { + fn serialize_some(self, _value: &T) -> Result { Err(self.unsupported()) } diff --git a/src/serde_urlencoded/ser/mod.rs b/src/serde_urlencoded/ser/mod.rs index f8d5e13e6..b4022d563 100644 --- a/src/serde_urlencoded/ser/mod.rs +++ b/src/serde_urlencoded/ser/mod.rs @@ -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) -> 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; 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 { + fn serialize_unit_struct(self, _name: &'static str) -> Result { Err(Error::top_level()) } /// Returns an error. - fn serialize_unit_variant(self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str) - -> Result { + fn serialize_unit_variant( + self, _name: &'static str, _variant_index: u32, _variant: &'static str, + ) -> Result { Err(Error::top_level()) } /// Serializes the inner value, ignoring the newtype name. - fn serialize_newtype_struct - (self, - _name: &'static str, - value: &T) - -> Result { + fn serialize_newtype_struct( + self, _name: &'static str, value: &T, + ) -> Result { value.serialize(self) } /// Returns an error. - fn serialize_newtype_variant - (self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _value: &T) - -> Result { + fn serialize_newtype_variant( + self, _name: &'static str, _variant_index: u32, _variant: &'static str, + _value: &T, + ) -> Result { Err(Error::top_level()) } @@ -266,50 +258,43 @@ impl<'output, Target> ser::Serializer for Serializer<'output, Target> } /// Serializes the given value. - fn serialize_some - (self, - value: &T) - -> Result { + fn serialize_some( + self, value: &T, + ) -> Result { value.serialize(self) } /// Serialize a sequence, given length (if any) is ignored. - fn serialize_seq(self, - _len: Option) - -> Result { - Ok(SeqSerializer { urlencoder: self.urlencoder }) + fn serialize_seq(self, _len: Option) -> Result { + Ok(SeqSerializer { + urlencoder: self.urlencoder, + }) } /// Returns an error. - fn serialize_tuple(self, - _len: usize) - -> Result { - Ok(TupleSerializer { urlencoder: self.urlencoder }) + fn serialize_tuple(self, _len: usize) -> Result { + Ok(TupleSerializer { + urlencoder: self.urlencoder, + }) } /// Returns an error. - fn serialize_tuple_struct(self, - _name: &'static str, - _len: usize) - -> Result { + fn serialize_tuple_struct( + self, _name: &'static str, _len: usize, + ) -> Result { Err(Error::top_level()) } /// Returns an error. - fn serialize_tuple_variant - (self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _len: usize) - -> Result { + fn serialize_tuple_variant( + self, _name: &'static str, _variant_index: u32, _variant: &'static str, + _len: usize, + ) -> Result { Err(Error::top_level()) } /// Serializes a map, given length is ignored. - fn serialize_map(self, - _len: Option) - -> Result { + fn serialize_map(self, _len: Option) -> Result { 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 { - Ok(StructSerializer { urlencoder: self.urlencoder }) + fn serialize_struct( + self, _name: &'static str, _len: usize, + ) -> Result { + 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 { + fn serialize_struct_variant( + self, _name: &'static str, _variant_index: u32, _variant: &'static str, + _len: usize, + ) -> Result { 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; type Error = Error; - fn serialize_element(&mut self, - value: &T) - -> Result<(), Error> { + fn serialize_element( + &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; type Error = Error; - fn serialize_element(&mut self, - value: &T) - -> Result<(), Error> { + fn serialize_element( + &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; type Error = Error; - fn serialize_field(&mut self, - value: &T) - -> Result<(), Error> { + fn serialize_field( + &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; type Error = Error; - fn serialize_field(&mut self, - value: &T) - -> Result<(), Error> { + fn serialize_field( + &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; type Error = Error; - fn serialize_entry - (&mut self, - key: &K, - value: &V) - -> Result<(), Error> { + fn serialize_entry( + &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(&mut self, - key: &T) - -> Result<(), Error> { + fn serialize_key( + &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(&mut self, - value: &T) - -> Result<(), Error> { + fn serialize_value( + &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; type Error = Error; - fn serialize_field(&mut self, - key: &'static str, - value: &T) - -> Result<(), Error> { + fn serialize_field( + &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; type Error = Error; - fn serialize_field(&mut self, - key: &'static str, - value: &T) - -> Result<(), Error> { + fn serialize_field( + &mut self, key: &'static str, value: &T, + ) -> Result<(), Error> { self.inner.serialize_field(key, value) } diff --git a/src/serde_urlencoded/ser/pair.rs b/src/serde_urlencoded/ser/pair.rs index 37f984755..68db144f9 100644 --- a/src/serde_urlencoded/ser/pair.rs +++ b/src/serde_urlencoded/ser/pair.rs @@ -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) -> 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 - (self, - _name: &'static str, - value: &T) - -> Result<(), Error> { + fn serialize_newtype_struct( + self, _name: &'static str, value: &T, + ) -> Result<(), Error> { value.serialize(self) } - fn serialize_newtype_variant - (self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _value: &T) - -> Result<(), Error> { + fn serialize_newtype_variant( + 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(self, - value: &T) - -> Result<(), Error> { + fn serialize_some(self, value: &T) -> Result<(), Error> { value.serialize(self) } - fn serialize_seq(self, - _len: Option) - -> Result { + fn serialize_seq(self, _len: Option) -> Result { 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 { + fn serialize_tuple_struct( + self, _name: &'static str, _len: usize, + ) -> Result { Err(Error::unsupported_pair()) } - fn serialize_tuple_variant - (self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _len: usize) - -> Result { + fn serialize_tuple_variant( + self, _name: &'static str, _variant_index: u32, _variant: &'static str, + _len: usize, + ) -> Result { Err(Error::unsupported_pair()) } - fn serialize_map(self, - _len: Option) - -> Result { + fn serialize_map(self, _len: Option) -> Result { Err(Error::unsupported_pair()) } - fn serialize_struct(self, - _name: &'static str, - _len: usize) - -> Result { + fn serialize_struct( + self, _name: &'static str, _len: usize, + ) -> Result { Err(Error::unsupported_pair()) } - fn serialize_struct_variant - (self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _len: usize) - -> Result { + fn serialize_struct_variant( + self, _name: &'static str, _variant_index: u32, _variant: &'static str, + _len: usize, + ) -> Result { 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(&mut self, - value: &T) - -> Result<(), Error> { + fn serialize_element( + &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()), } } diff --git a/src/serde_urlencoded/ser/part.rs b/src/serde_urlencoded/ser/part.rs index 2ce352d24..4874dd34b 100644 --- a/src/serde_urlencoded/ser/part.rs +++ b/src/serde_urlencoded/ser/part.rs @@ -1,4 +1,4 @@ -use ::serde; +use serde; use super::super::dtoa; use super::super::itoa; @@ -11,25 +11,22 @@ pub struct PartSerializer { impl PartSerializer { 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; + fn serialize_static_str(self, value: &'static str) -> Result; fn serialize_str(self, value: &str) -> Result; fn serialize_string(self, value: String) -> Result; fn serialize_none(self) -> Result; - fn serialize_some - (self, - value: &T) - -> Result; + fn serialize_some( + self, value: &T, + ) -> Result; fn unsupported(self) -> Error; } @@ -46,7 +43,8 @@ impl serde::ser::Serializer for PartSerializer { type SerializeStructVariant = serde::ser::Impossible; fn serialize_bool(self, v: bool) -> Result { - 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 { @@ -109,32 +107,25 @@ impl serde::ser::Serializer for PartSerializer { } fn serialize_unit_struct(self, name: &'static str) -> Result { - 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 { - self.sink.serialize_static_str(variant.into()) + fn serialize_unit_variant( + self, _name: &'static str, _variant_index: u32, variant: &'static str, + ) -> Result { + self.sink.serialize_static_str(variant) } - fn serialize_newtype_struct - (self, - _name: &'static str, - value: &T) - -> Result { + fn serialize_newtype_struct( + self, _name: &'static str, value: &T, + ) -> Result { value.serialize(self) } - fn serialize_newtype_variant - (self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _value: &T) - -> Result { + fn serialize_newtype_variant( + self, _name: &'static str, _variant_index: u32, _variant: &'static str, + _value: &T, + ) -> Result { Err(self.sink.unsupported()) } @@ -142,68 +133,55 @@ impl serde::ser::Serializer for PartSerializer { self.sink.serialize_none() } - fn serialize_some(self, - value: &T) - -> Result { + fn serialize_some( + self, value: &T, + ) -> Result { self.sink.serialize_some(value) } - fn serialize_seq(self, - _len: Option) - -> Result { + fn serialize_seq(self, _len: Option) -> Result { Err(self.sink.unsupported()) } - fn serialize_tuple(self, - _len: usize) - -> Result { + fn serialize_tuple(self, _len: usize) -> Result { Err(self.sink.unsupported()) } - fn serialize_tuple_struct(self, - _name: &'static str, - _len: usize) - -> Result { + fn serialize_tuple_struct( + self, _name: &'static str, _len: usize, + ) -> Result { Err(self.sink.unsupported()) } - fn serialize_tuple_variant - (self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _len: usize) - -> Result { + fn serialize_tuple_variant( + self, _name: &'static str, _variant_index: u32, _variant: &'static str, + _len: usize, + ) -> Result { Err(self.sink.unsupported()) } - fn serialize_map(self, - _len: Option) - -> Result { + fn serialize_map(self, _len: Option) -> Result { Err(self.sink.unsupported()) } - fn serialize_struct(self, - _name: &'static str, - _len: usize) - -> Result { + fn serialize_struct( + self, _name: &'static str, _len: usize, + ) -> Result { Err(self.sink.unsupported()) } - fn serialize_struct_variant - (self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _len: usize) - -> Result { + fn serialize_struct_variant( + self, _name: &'static str, _variant_index: u32, _variant: &'static str, + _len: usize, + ) -> Result { Err(self.sink.unsupported()) } } impl PartSerializer { fn serialize_integer(self, value: I) -> Result - 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 PartSerializer { } fn serialize_floating(self, value: F) -> Result - where F: dtoa::Floating, + where + F: dtoa::Floating, { let mut buf = [b'\0'; 24]; let len = dtoa::write(&mut buf[..], value).unwrap(); diff --git a/src/serde_urlencoded/ser/value.rs b/src/serde_urlencoded/ser/value.rs index ef63b010d..3c47739f3 100644 --- a/src/serde_urlencoded/ser/value.rs +++ b/src/serde_urlencoded/ser/value.rs @@ -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, 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, - key: &'key str) - -> Self { - ValueSink { - urlencoder: urlencoder, - key: key, - } + pub fn new( + urlencoder: &'target mut UrlEncodedSerializer, 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(self, - value: &T) - -> Result { + fn serialize_some( + self, value: &T, + ) -> Result { value.serialize(PartSerializer::new(self)) } diff --git a/tests/test_client.rs b/tests/test_client.rs index d128fa310..cf20fb8b8 100644 --- a/tests/test_client.rs +++ b/tests/test_client.rs @@ -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\"")); diff --git a/tests/test_handlers.rs b/tests/test_handlers.rs index bc65b93f8..c86a3e9c0 100644 --- a/tests/test_handlers.rs +++ b/tests/test_handlers.rs @@ -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); }