From f27c161dafca1ffae86e104eea4fad8c899f0f21 Mon Sep 17 00:00:00 2001 From: Sam Scott Date: Wed, 23 Jan 2019 11:14:57 -0500 Subject: Run cargo fmt. --- src/ser.rs | 329 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 180 insertions(+), 149 deletions(-) (limited to 'src/ser.rs') diff --git a/src/ser.rs b/src/ser.rs index 13796ce..fb765d7 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -1,8 +1,7 @@ - //! Serialization support for querystrings. use data_encoding::BASE64URL_NOPAD as BASE64; -use percent_encoding::{EncodeSet, percent_encode}; +use percent_encoding::{percent_encode, EncodeSet}; use serde::ser; use error::*; @@ -19,8 +18,14 @@ struct QS_ENCODE_SET; impl EncodeSet for QS_ENCODE_SET { fn contains(&self, byte: u8) -> bool { match byte { - b' ' | b'*' | b'-' | b'.' | b'0'...b'9' | b'A'...b'Z' | b'_' | - b'a'...b'z' => false, + b' ' + | b'*' + | b'-' + | b'.' + | b'0'...b'9' + | b'A'...b'Z' + | b'_' + | b'a'...b'z' => false, _ => true, } } @@ -56,10 +61,10 @@ pub fn to_string(input: &T) -> Result { let mut buffer = Vec::new(); let mut first = true; input.serialize(&mut QsSerializer { - writer: &mut buffer, - key: None, - first: &mut first, - })?; + writer: &mut buffer, + key: None, + first: &mut first, + })?; String::from_utf8(buffer).map_err(Error::from) } @@ -90,13 +95,16 @@ pub fn to_string(input: &T) -> Result { /// "name=Alice&age=24&occupation=Student"); /// # } /// ``` -pub fn to_writer(input: &T, writer: &mut W) -> Result<()> { +pub fn to_writer( + input: &T, + writer: &mut W, +) -> Result<()> { let mut first = true; input.serialize(&mut QsSerializer { - writer, - key: None, - first: &mut first, - }) + writer, + key: None, + first: &mut first, + }) } /// A serializer for the querystring format. @@ -124,17 +132,19 @@ fn replace_space(input: &str) -> Cow { *byte = b'+'; } } - Cow::Owned(String::from_utf8(replaced) - .expect("replacing ' ' with '+' cannot panic")) + Cow::Owned( + String::from_utf8(replaced) + .expect("replacing ' ' with '+' cannot panic"), + ) }, } } impl<'a, W: 'a + Write> QsSerializer<'a, W> { fn extend_key(&mut self, newkey: &str) { - let newkey = percent_encode(replace_space(newkey).as_bytes(), - QS_ENCODE_SET) - .collect::>(); + let newkey = + percent_encode(replace_space(newkey).as_bytes(), QS_ENCODE_SET) + .collect::>(); let key = if let Some(ref key) = self.key { format!("{}[{}]", key, newkey).into() } else { @@ -145,19 +155,21 @@ impl<'a, W: 'a + Write> QsSerializer<'a, W> { fn write_value(&mut self, value: &str) -> Result<()> { if let Some(ref key) = self.key { - write!(self.writer, - "{}{}={}", - if *self.first { - *self.first = false; - "" - } else { - "&" - }, - key, - percent_encode(value.as_bytes(), QS_ENCODE_SET) - .map(replace_space) - .collect::()) - .map_err(Error::from) + write!( + self.writer, + "{}{}={}", + if *self.first { + *self.first = false; + "" + } else { + "&" + }, + key, + percent_encode(value.as_bytes(), QS_ENCODE_SET) + .map(replace_space) + .collect::() + ) + .map_err(Error::from) } else { Err(Error::no_key()) } @@ -165,8 +177,9 @@ impl<'a, W: 'a + Write> QsSerializer<'a, W> { /// Creates a new `QsSerializer` with a distinct key, but `writer` and ///`first` referring to the original data. - fn new_from_ref<'b: 'a>(other: &'a mut QsSerializer<'b, W>) - -> QsSerializer<'a, W> { + fn new_from_ref<'b: 'a>( + other: &'a mut QsSerializer<'b, W>, + ) -> QsSerializer<'a, W> { Self { key: other.key.clone(), writer: other.writer, @@ -210,7 +223,7 @@ impl<'a, W: Write> ser::Serializer for &'a mut QsSerializer<'a, W> { type SerializeStruct = Self; type SerializeStructVariant = Self; - serialize_as_string!{ + serialize_as_string! { Qs bool => serialize_bool, u8 => serialize_u8, @@ -231,7 +244,6 @@ impl<'a, W: Write> ser::Serializer for &'a mut QsSerializer<'a, W> { self.write_value(&BASE64.encode(value)) } - fn serialize_unit(self) -> Result { self.write_value("") } @@ -242,31 +254,32 @@ impl<'a, W: Write> ser::Serializer for &'a mut QsSerializer<'a, W> { } /// 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 { self.write_value(variant) } /// Returns an error. - 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 { self.extend_key(variant); value.serialize(self) } @@ -275,9 +288,10 @@ impl<'a, W: Write> ser::Serializer for &'a mut QsSerializer<'a, W> { Ok(()) } - fn serialize_some(self, - value: &T) - -> Result { + fn serialize_some( + self, + value: &T, + ) -> Result { // Err(ErrorKind::Unsupported.into()) value.serialize(self) } @@ -287,25 +301,26 @@ impl<'a, W: Write> ser::Serializer for &'a mut QsSerializer<'a, W> { Ok(QsSeq(self, 0)) } - fn serialize_tuple(self, _len: usize) -> Result { Ok(QsSeq(self, 0)) } /// 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 { Ok(QsSeq(self, 0)) } - 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 { // self.write(variant)?; self.extend_key(variant); Ok(QsSeq(self, 0)) @@ -315,43 +330,47 @@ impl<'a, W: Write> ser::Serializer for &'a mut QsSerializer<'a, W> { Ok(QsMap(self, None)) } - fn serialize_struct(self, - _name: &'static str, - _len: usize) - -> Result { + fn serialize_struct( + self, + _name: &'static str, + _len: usize, + ) -> Result { Ok(self) } - 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 { self.extend_key(variant); Ok(self) } } - impl ser::Error for Error { fn custom(msg: T) -> Self - where T: Display, + where + T: Display, { ErrorKind::Custom(msg.to_string()).into() } } pub struct QsSeq<'a, W: 'a + Write>(&'a mut QsSerializer<'a, W>, usize); -pub struct QsMap<'a, W: 'a + Write>(&'a mut QsSerializer<'a, W>, - Option>); - +pub struct QsMap<'a, W: 'a + Write>( + &'a mut QsSerializer<'a, W>, + Option>, +); impl<'a, W: Write> ser::SerializeTuple for QsSeq<'a, W> { type Ok = (); type Error = Error; fn serialize_element(&mut self, value: &T) -> Result<()> - where T: ser::Serialize, + where + T: ser::Serialize, { let mut serializer = QsSerializer::new_from_ref(self.0); serializer.extend_key(&self.1.to_string()); @@ -361,7 +380,6 @@ impl<'a, W: Write> ser::SerializeTuple for QsSeq<'a, W> { fn end(self) -> Result { Ok(()) - } } @@ -369,7 +387,8 @@ impl<'a, W: Write> ser::SerializeSeq for QsSeq<'a, W> { type Ok = (); type Error = Error; fn serialize_element(&mut self, value: &T) -> Result<()> - where T: ser::Serialize, + where + T: ser::Serialize, { let mut serializer = QsSerializer::new_from_ref(self.0); serializer.extend_key(&self.1.to_string()); @@ -378,18 +397,19 @@ impl<'a, W: Write> ser::SerializeSeq for QsSeq<'a, W> { } fn end(self) -> Result { Ok(()) - } } impl<'a, W: Write> ser::SerializeStruct for &'a mut QsSerializer<'a, W> { type Ok = (); type Error = Error; - fn serialize_field(&mut self, - key: &'static str, - value: &T) - -> Result<()> - where T: ser::Serialize, + fn serialize_field( + &mut self, + key: &'static str, + value: &T, + ) -> Result<()> + where + T: ser::Serialize, { let mut serializer = QsSerializer::new_from_ref(self); serializer.extend_key(key); @@ -404,11 +424,13 @@ impl<'a, W: Write> ser::SerializeStructVariant for &'a mut QsSerializer<'a, W> { type Ok = (); type Error = Error; - fn serialize_field(&mut self, - key: &'static str, - value: &T) - -> Result<()> - where T: ser::Serialize, + fn serialize_field( + &mut self, + key: &'static str, + value: &T, + ) -> Result<()> + where + T: ser::Serialize, { let mut serializer = QsSerializer::new_from_ref(self); serializer.extend_key(key); @@ -425,7 +447,8 @@ impl<'a, W: Write> ser::SerializeTupleVariant for QsSeq<'a, W> { type Error = Error; fn serialize_field(&mut self, value: &T) -> Result<()> - where T: ser::Serialize, + where + T: ser::Serialize, { let mut serializer = QsSerializer::new_from_ref(self.0); serializer.extend_key(&self.1.to_string()); @@ -443,7 +466,8 @@ impl<'a, W: Write> ser::SerializeTupleStruct for QsSeq<'a, W> { type Error = Error; fn serialize_field(&mut self, value: &T) -> Result<()> - where T: ser::Serialize, + where + T: ser::Serialize, { let mut serializer = QsSerializer::new_from_ref(self.0); serializer.extend_key(&self.1.to_string()); @@ -461,14 +485,16 @@ impl<'a, W: Write> ser::SerializeMap for QsMap<'a, W> { type Error = Error; fn serialize_key(&mut self, key: &T) -> Result<()> - where T: ser::Serialize, + where + T: ser::Serialize, { self.1 = Some(Cow::from(key.serialize(StringSerializer)?)); Ok(()) } fn serialize_value(&mut self, value: &T) -> Result<()> - where T: ser::Serialize, + where + T: ser::Serialize, { let mut serializer = QsSerializer::new_from_ref(self.0); if let Some(ref key) = self.1 { @@ -484,12 +510,14 @@ impl<'a, W: Write> ser::SerializeMap for QsMap<'a, W> { Ok(()) } - fn serialize_entry(&mut self, - key: &K, - value: &V) - -> Result<()> - where K: ser::Serialize, - V: ser::Serialize, + fn serialize_entry( + &mut self, + key: &K, + value: &V, + ) -> Result<()> + where + K: ser::Serialize, + V: ser::Serialize, { let mut serializer = QsSerializer::new_from_ref(self.0); serializer.extend_key(&key.serialize(StringSerializer)?); @@ -510,7 +538,7 @@ impl ser::Serializer for StringSerializer { type SerializeStruct = ser::Impossible; type SerializeStructVariant = ser::Impossible; - serialize_as_string!{ + serialize_as_string! { bool => serialize_bool, u8 => serialize_u8, u16 => serialize_u16, @@ -526,7 +554,6 @@ impl ser::Serializer for StringSerializer { &str => serialize_str, } - fn serialize_bytes(self, value: &[u8]) -> Result { Ok(BASE64.encode(value)) } @@ -542,31 +569,32 @@ impl ser::Serializer for StringSerializer { } /// 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(ErrorKind::Unsupported.into()) } /// Returns an error. - fn serialize_newtype_struct - (self, - _name: &'static str, - _value: &T) - -> Result { + fn serialize_newtype_struct( + self, + _name: &'static str, + _value: &T, + ) -> Result { Err(ErrorKind::Unsupported.into()) } /// 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(ErrorKind::Unsupported.into()) } @@ -576,9 +604,10 @@ impl ser::Serializer for StringSerializer { } /// Returns an error. - fn serialize_some(self, - _value: &T) - -> Result { + fn serialize_some( + self, + _value: &T, + ) -> Result { Err(ErrorKind::Unsupported.into()) } @@ -587,46 +616,48 @@ impl ser::Serializer for StringSerializer { Err(ErrorKind::Unsupported.into()) } - fn serialize_tuple(self, _len: usize) -> Result { Err(ErrorKind::Unsupported.into()) } /// 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(ErrorKind::Unsupported.into()) } - 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(ErrorKind::Unsupported.into()) } fn serialize_map(self, _len: Option) -> Result { Err(ErrorKind::Unsupported.into()) - } - fn serialize_struct(self, - _name: &'static str, - _len: usize) - -> Result { + fn serialize_struct( + self, + _name: &'static str, + _len: usize, + ) -> Result { Err(ErrorKind::Unsupported.into()) } - 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(ErrorKind::Unsupported.into()) } } -- cgit v1.2.3