Merge rustfmt settings, re-run cargo fmt

This commit is contained in:
Jonas Platte 2020-06-05 02:01:09 +02:00
parent f8b1aef906
commit 8ddff0d108
No known key found for this signature in database
GPG key ID: 7D261D771D915378
19 changed files with 121 additions and 437 deletions

View file

@ -1,3 +1,4 @@
edition = "2018"
merge_imports = true
newline_style = "Unix"
use_small_heuristics = "Max"

View file

@ -2,10 +2,6 @@
//! endpoint in the [Matrix](https://matrix.org/) application service API specification. These
//! types can be shared by application service and server code.
#![deny(
missing_copy_implementations,
missing_debug_implementations,
missing_docs
)]
#![deny(missing_copy_implementations, missing_debug_implementations, missing_docs)]
pub mod v1;

View file

@ -83,10 +83,7 @@ impl<'de> Deserialize<'de> for Action {
"notify" => Ok(Action::Notify),
"dont_notify" => Ok(Action::DontNotify),
"coalesce" => Ok(Action::Coalesce),
s => Err(E::unknown_variant(
&s,
&["notify", "dont_notify", "coalesce"],
)),
s => Err(E::unknown_variant(&s, &["notify", "dont_notify", "coalesce"])),
}
}
@ -153,10 +150,7 @@ mod tests {
#[test]
fn deserialize_string_action() {
assert_matches!(
from_json_value::<Action>(json!("notify")).unwrap(),
Action::Notify
);
assert_matches!(from_json_value::<Action>(json!("notify")).unwrap(), Action::Notify);
}
#[test]

View file

@ -23,10 +23,7 @@ pub struct SoundTweak {
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(tag = "set_tweak", rename = "highlight")]
pub struct HighlightTweak {
#[serde(
default = "ruma_serde::default_true",
skip_serializing_if = "ruma_serde::is_true"
)]
#[serde(default = "ruma_serde::default_true", skip_serializing_if = "ruma_serde::is_true")]
value: bool,
}

View file

@ -1,4 +0,0 @@
max_width = 80
newline_style = "Unix"
reorder_imports = true
use_try_shorthand = true

View file

@ -13,10 +13,7 @@ use serde::{
///
/// Will fail if integer is greater than the maximum integer that can be
/// unambiguously represented by an f64.
pub fn serialize<S>(
opt_duration: &Option<Duration>,
serializer: S,
) -> Result<S::Ok, S::Error>
pub fn serialize<S>(opt_duration: &Option<Duration>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
@ -33,9 +30,7 @@ where
///
/// Will fail if integer is greater than the maximum integer that can be
/// unambiguously represented by an f64.
pub fn deserialize<'de, D>(
deserializer: D,
) -> Result<Option<Duration>, D::Error>
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Duration>, D::Error>
where
D: Deserializer<'de>,
{
@ -52,11 +47,7 @@ mod tests {
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
struct DurationTest {
#[serde(
with = "super",
default,
skip_serializing_if = "Option::is_none"
)]
#[serde(with = "super", default, skip_serializing_if = "Option::is_none")]
timeout: Option<Duration>,
}
@ -66,9 +57,7 @@ mod tests {
assert_eq!(
serde_json::from_value::<DurationTest>(json).unwrap(),
DurationTest {
timeout: Some(Duration::from_millis(3000))
},
DurationTest { timeout: Some(Duration::from_millis(3000)) },
);
}
@ -94,13 +83,8 @@ mod tests {
#[test]
fn test_serialize_some() {
let request = DurationTest {
timeout: Some(Duration::new(2, 0)),
};
assert_eq!(
serde_json::to_value(&request).unwrap(),
json!({ "timeout": 2000 })
);
let request = DurationTest { timeout: Some(Duration::new(2, 0)) };
assert_eq!(serde_json::to_value(&request).unwrap(), json!({ "timeout": 2000 }));
}
#[test]

View file

@ -13,10 +13,7 @@ use serde::{
///
/// Will fail if integer is greater than the maximum integer that can be
/// unambiguously represented by an f64.
pub fn serialize<S>(
duration: &Duration,
serializer: S,
) -> Result<S::Ok, S::Error>
pub fn serialize<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
@ -56,20 +53,13 @@ mod tests {
assert_eq!(
serde_json::from_value::<DurationTest>(json).unwrap(),
DurationTest {
timeout: Duration::from_secs(3)
},
DurationTest { timeout: Duration::from_secs(3) },
);
}
#[test]
fn test_serialize() {
let test = DurationTest {
timeout: Duration::from_millis(7000),
};
assert_eq!(
serde_json::to_value(test).unwrap(),
json!({ "timeout": 7 }),
);
let test = DurationTest { timeout: Duration::from_millis(7000) };
assert_eq!(serde_json::to_value(test).unwrap(), json!({ "timeout": 7 }),);
}
}

View file

@ -64,18 +64,12 @@ pub mod vec_as_map_of_empty {
use super::Empty;
#[allow(clippy::ptr_arg)]
pub fn serialize<S, T>(
vec: &Vec<T>,
serializer: S,
) -> Result<S::Ok, S::Error>
pub fn serialize<S, T>(vec: &Vec<T>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: Serialize + Eq + Ord,
{
vec.iter()
.map(|v| (v, Empty))
.collect::<BTreeMap<_, _>>()
.serialize(serializer)
vec.iter().map(|v| (v, Empty)).collect::<BTreeMap<_, _>>().serialize(serializer)
}
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error>

View file

@ -59,20 +59,13 @@ mod tests {
assert_eq!(
serde_json::from_value::<SystemTimeTest>(json).unwrap(),
SystemTimeTest {
timestamp: UNIX_EPOCH + Duration::from_millis(3000),
},
SystemTimeTest { timestamp: UNIX_EPOCH + Duration::from_millis(3000) },
);
}
#[test]
fn test_serialize() {
let request = SystemTimeTest {
timestamp: UNIX_EPOCH + Duration::new(2, 0),
};
assert_eq!(
serde_json::to_value(&request).unwrap(),
json!({ "timestamp": 2000 })
);
let request = SystemTimeTest { timestamp: UNIX_EPOCH + Duration::new(2, 0) };
assert_eq!(serde_json::to_value(&request).unwrap(), json!({ "timestamp": 2000 }));
}
}

View file

@ -14,10 +14,7 @@ use serde::{
///
/// Will fail if integer is greater than the maximum integer that can be unambiguously represented
/// by an f64.
pub fn serialize<S>(
opt_time: &Option<SystemTime>,
serializer: S,
) -> Result<S::Ok, S::Error>
pub fn serialize<S>(opt_time: &Option<SystemTime>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
@ -31,9 +28,7 @@ where
///
/// Will fail if integer is greater than the maximum integer that can be unambiguously represented
/// by an f64.
pub fn deserialize<'de, D>(
deserializer: D,
) -> Result<Option<SystemTime>, D::Error>
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<SystemTime>, D::Error>
where
D: Deserializer<'de>,
{
@ -50,11 +45,7 @@ mod tests {
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
struct SystemTimeTest {
#[serde(
with = "super",
default,
skip_serializing_if = "Option::is_none"
)]
#[serde(with = "super", default, skip_serializing_if = "Option::is_none")]
timestamp: Option<SystemTime>,
}
@ -64,9 +55,7 @@ mod tests {
assert_eq!(
serde_json::from_value::<SystemTimeTest>(json).unwrap(),
SystemTimeTest {
timestamp: Some(UNIX_EPOCH + Duration::from_millis(3000))
},
SystemTimeTest { timestamp: Some(UNIX_EPOCH + Duration::from_millis(3000)) },
);
}
@ -92,13 +81,8 @@ mod tests {
#[test]
fn test_serialize_some() {
let request = SystemTimeTest {
timestamp: Some(UNIX_EPOCH + Duration::new(2, 0)),
};
assert_eq!(
serde_json::to_value(&request).unwrap(),
json!({ "timestamp": 2000 })
);
let request = SystemTimeTest { timestamp: Some(UNIX_EPOCH + Duration::new(2, 0)) };
assert_eq!(serde_json::to_value(&request).unwrap(), json!({ "timestamp": 2000 }));
}
#[test]

View file

@ -71,9 +71,9 @@ where
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)
}
@ -93,9 +93,7 @@ pub struct Deserializer<'de> {
impl<'de> Deserializer<'de> {
/// Returns a new `Deserializer`.
pub fn new(parse: UrlEncodedParse<'de>) -> Self {
Deserializer {
inner: MapDeserializer::new(group_entries(parse).into_iter()),
}
Deserializer { inner: MapDeserializer::new(group_entries(parse).into_iter()) }
}
}
@ -160,9 +158,7 @@ impl<'de> de::Deserializer<'de> for Deserializer<'de> {
}
}
fn group_entries<'de>(
parse: UrlEncodedParse<'de>,
) -> BTreeMap<Part<'de>, ValOrVec<Part<'de>>> {
fn group_entries<'de>(parse: UrlEncodedParse<'de>) -> BTreeMap<Part<'de>, ValOrVec<Part<'de>>> {
use btree_map::Entry::*;
let mut res = BTreeMap::new();
@ -313,10 +309,7 @@ impl<'de> de::EnumAccess<'de> for ValueEnumAccess<'de> {
type Error = Error;
type Variant = UnitOnlyVariantAccess;
fn variant_seed<V>(
self,
seed: V,
) -> Result<(V::Value, Self::Variant), Self::Error>
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
where
V: de::DeserializeSeed<'de>,
{
@ -341,11 +334,7 @@ impl<'de> de::VariantAccess<'de> for UnitOnlyVariantAccess {
Err(Error::custom("expected unit variant"))
}
fn tuple_variant<V>(
self,
_len: usize,
_visitor: V,
) -> Result<V::Value, Self::Error>
fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{

View file

@ -136,11 +136,7 @@ where
}
}
fn deserialize_tuple<V>(
self,
len: usize,
visitor: V,
) -> Result<V::Value, Self::Error>
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
@ -189,9 +185,7 @@ where
V: de::Visitor<'de>,
{
match self {
ValOrVec::Val(val) => {
val.deserialize_tuple_struct(name, len, visitor)
}
ValOrVec::Val(val) => val.deserialize_tuple_struct(name, len, visitor),
ValOrVec::Vec(_) => Err(de::Error::custom("unsupported value")),
}
}

View file

@ -8,9 +8,7 @@ mod value;
use std::{borrow::Cow, error, fmt, str};
use serde::ser;
use url::form_urlencoded::{
Serializer as UrlEncodedSerializer, Target as UrlEncodedTarget,
};
use url::form_urlencoded::{Serializer as UrlEncodedSerializer, Target as UrlEncodedTarget};
/// Serializes a value into a `application/x-www-form-urlencoded` `String` buffer.
///
@ -45,13 +43,9 @@ pub struct Serializer<'input, 'output, Target: 'output + UrlEncodedTarget> {
urlencoder: &'output mut UrlEncodedSerializer<'input, Target>,
}
impl<'input, 'output, Target: 'output + UrlEncodedTarget>
Serializer<'input, 'output, Target>
{
impl<'input, 'output, Target: 'output + UrlEncodedTarget> Serializer<'input, 'output, Target> {
/// Returns a new `Serializer`.
pub fn new(
urlencoder: &'output mut UrlEncodedSerializer<'input, Target>,
) -> Self {
pub fn new(urlencoder: &'output mut UrlEncodedSerializer<'input, Target>) -> Self {
Serializer { urlencoder }
}
}
@ -104,27 +98,21 @@ pub struct SeqSerializer<'input, 'output, Target: 'output + UrlEncodedTarget> {
/// Tuple serializer.
///
/// Mostly used for arrays.
pub struct TupleSerializer<'input, 'output, Target: 'output + UrlEncodedTarget>
{
pub struct TupleSerializer<'input, 'output, Target: 'output + UrlEncodedTarget> {
urlencoder: &'output mut UrlEncodedSerializer<'input, Target>,
}
/// Tuple struct serializer.
///
/// Never instantiated, tuple structs are not supported.
pub struct TupleStructSerializer<'input, 'output, T: 'output + UrlEncodedTarget>
{
pub struct TupleStructSerializer<'input, 'output, T: 'output + UrlEncodedTarget> {
inner: ser::Impossible<&'output mut UrlEncodedSerializer<'input, T>, Error>,
}
/// Tuple variant serializer.
///
/// Never instantiated, tuple variants are not supported.
pub struct TupleVariantSerializer<
'input,
'output,
T: 'output + UrlEncodedTarget,
> {
pub struct TupleVariantSerializer<'input, 'output, T: 'output + UrlEncodedTarget> {
inner: ser::Impossible<&'output mut UrlEncodedSerializer<'input, T>, Error>,
}
@ -135,24 +123,18 @@ pub struct MapSerializer<'input, 'output, Target: 'output + UrlEncodedTarget> {
}
/// Struct serializer.
pub struct StructSerializer<'input, 'output, Target: 'output + UrlEncodedTarget>
{
pub struct StructSerializer<'input, 'output, Target: 'output + UrlEncodedTarget> {
urlencoder: &'output mut UrlEncodedSerializer<'input, Target>,
}
/// Struct variant serializer.
///
/// Never instantiated, struct variants are not supported.
pub struct StructVariantSerializer<
'input,
'output,
T: 'output + UrlEncodedTarget,
> {
pub struct StructVariantSerializer<'input, 'output, T: 'output + UrlEncodedTarget> {
inner: ser::Impossible<&'output mut UrlEncodedSerializer<'input, T>, Error>,
}
impl<'input, 'output, Target> ser::Serializer
for Serializer<'input, 'output, Target>
impl<'input, 'output, Target> ser::Serializer for Serializer<'input, 'output, Target>
where
Target: 'output + UrlEncodedTarget,
{
@ -161,12 +143,10 @@ where
type SerializeSeq = SeqSerializer<'input, 'output, Target>;
type SerializeTuple = TupleSerializer<'input, 'output, Target>;
type SerializeTupleStruct = TupleStructSerializer<'input, 'output, Target>;
type SerializeTupleVariant =
TupleVariantSerializer<'input, 'output, Target>;
type SerializeTupleVariant = TupleVariantSerializer<'input, 'output, Target>;
type SerializeMap = MapSerializer<'input, 'output, Target>;
type SerializeStruct = StructSerializer<'input, 'output, Target>;
type SerializeStructVariant =
StructVariantSerializer<'input, 'output, Target>;
type SerializeStructVariant = StructVariantSerializer<'input, 'output, Target>;
/// Returns an error.
fn serialize_bool(self, _v: bool) -> Result<Self::Ok, Error> {
@ -244,10 +224,7 @@ where
}
/// Returns `Ok`.
fn serialize_unit_struct(
self,
_name: &'static str,
) -> Result<Self::Ok, Error> {
fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Error> {
Ok(self.urlencoder)
}
@ -287,31 +264,18 @@ where
}
/// Serializes the given value.
fn serialize_some<T: ?Sized + ser::Serialize>(
self,
value: &T,
) -> Result<Self::Ok, Error> {
fn serialize_some<T: ?Sized + ser::Serialize>(self, value: &T) -> Result<Self::Ok, Error> {
value.serialize(self)
}
/// Serialize a sequence, given length (if any) is ignored.
fn serialize_seq(
self,
_len: Option<usize>,
) -> Result<Self::SerializeSeq, Error> {
Ok(SeqSerializer {
urlencoder: self.urlencoder,
})
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Error> {
Ok(SeqSerializer { urlencoder: self.urlencoder })
}
/// Returns an error.
fn serialize_tuple(
self,
_len: usize,
) -> Result<Self::SerializeTuple, Error> {
Ok(TupleSerializer {
urlencoder: self.urlencoder,
})
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Error> {
Ok(TupleSerializer { urlencoder: self.urlencoder })
}
/// Returns an error.
@ -335,14 +299,8 @@ where
}
/// Serializes a map, given length is ignored.
fn serialize_map(
self,
_len: Option<usize>,
) -> Result<Self::SerializeMap, Error> {
Ok(MapSerializer {
urlencoder: self.urlencoder,
key: None,
})
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Error> {
Ok(MapSerializer { urlencoder: self.urlencoder, key: None })
}
/// Serializes a struct, given length is ignored.
@ -351,9 +309,7 @@ where
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeStruct, Error> {
Ok(StructSerializer {
urlencoder: self.urlencoder,
})
Ok(StructSerializer { urlencoder: self.urlencoder })
}
/// Returns an error.
@ -368,18 +324,14 @@ where
}
}
impl<'input, 'output, Target> ser::SerializeSeq
for SeqSerializer<'input, 'output, Target>
impl<'input, 'output, Target> ser::SerializeSeq for SeqSerializer<'input, 'output, Target>
where
Target: 'output + UrlEncodedTarget,
{
type Ok = &'output mut UrlEncodedSerializer<'input, Target>;
type Error = Error;
fn serialize_element<T: ?Sized + ser::Serialize>(
&mut self,
value: &T,
) -> Result<(), Error> {
fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, value: &T) -> Result<(), Error> {
value.serialize(pair::PairSerializer::new(self.urlencoder))
}
@ -388,18 +340,14 @@ where
}
}
impl<'input, 'output, Target> ser::SerializeTuple
for TupleSerializer<'input, 'output, Target>
impl<'input, 'output, Target> ser::SerializeTuple for TupleSerializer<'input, 'output, Target>
where
Target: 'output + UrlEncodedTarget,
{
type Ok = &'output mut UrlEncodedSerializer<'input, Target>;
type Error = Error;
fn serialize_element<T: ?Sized + ser::Serialize>(
&mut self,
value: &T,
) -> Result<(), Error> {
fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, value: &T) -> Result<(), Error> {
value.serialize(pair::PairSerializer::new(self.urlencoder))
}
@ -416,10 +364,7 @@ where
type Ok = &'output mut UrlEncodedSerializer<'input, Target>;
type Error = Error;
fn serialize_field<T: ?Sized + ser::Serialize>(
&mut self,
value: &T,
) -> Result<(), Error> {
fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, value: &T) -> Result<(), Error> {
self.inner.serialize_field(value)
}
@ -436,10 +381,7 @@ where
type Ok = &'output mut UrlEncodedSerializer<'input, Target>;
type Error = Error;
fn serialize_field<T: ?Sized + ser::Serialize>(
&mut self,
value: &T,
) -> Result<(), Error> {
fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, value: &T) -> Result<(), Error> {
self.inner.serialize_field(value)
}
@ -448,18 +390,14 @@ where
}
}
impl<'input, 'output, Target> ser::SerializeMap
for MapSerializer<'input, 'output, Target>
impl<'input, 'output, Target> ser::SerializeMap for MapSerializer<'input, 'output, Target>
where
Target: 'output + UrlEncodedTarget,
{
type Ok = &'output mut UrlEncodedSerializer<'input, Target>;
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,
key: &K,
value: &V,
@ -474,20 +412,14 @@ where
key.serialize(entry_serializer)
}
fn serialize_key<T: ?Sized + ser::Serialize>(
&mut self,
key: &T,
) -> Result<(), Error> {
fn serialize_key<T: ?Sized + ser::Serialize>(&mut self, key: &T) -> Result<(), Error> {
let key_sink = key::KeySink::new(|key| Ok(key.into()));
let key_serializer = part::PartSerializer::new(key_sink);
self.key = Some(key.serialize(key_serializer)?);
Ok(())
}
fn serialize_value<T: ?Sized + ser::Serialize>(
&mut self,
value: &T,
) -> Result<(), Error> {
fn serialize_value<T: ?Sized + ser::Serialize>(&mut self, value: &T) -> Result<(), Error> {
{
let key = self.key.as_ref().ok_or_else(Error::no_key)?;
let value_sink = value::ValueSink::new(self.urlencoder, &key);
@ -502,8 +434,7 @@ where
}
}
impl<'input, 'output, Target> ser::SerializeStruct
for StructSerializer<'input, 'output, Target>
impl<'input, 'output, Target> ser::SerializeStruct for StructSerializer<'input, 'output, Target>
where
Target: 'output + UrlEncodedTarget,
{

View file

@ -65,10 +65,7 @@ where
Err(self.unsupported())
}
fn serialize_some<T: ?Sized + ser::Serialize>(
self,
_value: &T,
) -> Result<Ok, Error> {
fn serialize_some<T: ?Sized + ser::Serialize>(self, _value: &T) -> Result<Ok, Error> {
Err(self.unsupported())
}

View file

@ -1,9 +1,7 @@
use std::{borrow::Cow, mem};
use serde::ser;
use url::form_urlencoded::{
Serializer as UrlEncodedSerializer, Target as UrlEncodedTarget,
};
use url::form_urlencoded::{Serializer as UrlEncodedSerializer, Target as UrlEncodedTarget};
use super::{key::KeySink, part::PartSerializer, value::ValueSink, Error};
@ -16,18 +14,12 @@ impl<'input, 'target, Target> PairSerializer<'input, 'target, Target>
where
Target: 'target + UrlEncodedTarget,
{
pub fn new(
urlencoder: &'target mut UrlEncodedSerializer<'input, Target>,
) -> Self {
PairSerializer {
urlencoder,
state: PairState::WaitingForKey,
}
pub fn new(urlencoder: &'target mut UrlEncodedSerializer<'input, Target>) -> Self {
PairSerializer { urlencoder, state: PairState::WaitingForKey }
}
}
impl<'input, 'target, Target> ser::Serializer
for PairSerializer<'input, 'target, Target>
impl<'input, 'target, Target> ser::Serializer for PairSerializer<'input, 'target, Target>
where
Target: 'target + UrlEncodedTarget,
{
@ -136,17 +128,11 @@ where
Ok(())
}
fn serialize_some<T: ?Sized + ser::Serialize>(
self,
value: &T,
) -> Result<(), Error> {
fn serialize_some<T: ?Sized + ser::Serialize>(self, value: &T) -> Result<(), Error> {
value.serialize(self)
}
fn serialize_seq(
self,
_len: Option<usize>,
) -> Result<Self::SerializeSeq, Error> {
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Error> {
Err(Error::unsupported_pair())
}
@ -176,10 +162,7 @@ where
Err(Error::unsupported_pair())
}
fn serialize_map(
self,
_len: Option<usize>,
) -> Result<Self::SerializeMap, Error> {
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Error> {
Err(Error::unsupported_pair())
}
@ -202,25 +185,19 @@ where
}
}
impl<'input, 'target, Target> ser::SerializeTuple
for PairSerializer<'input, 'target, Target>
impl<'input, 'target, Target> ser::SerializeTuple for PairSerializer<'input, 'target, Target>
where
Target: 'target + UrlEncodedTarget,
{
type Ok = ();
type Error = Error;
fn serialize_element<T: ?Sized + ser::Serialize>(
&mut self,
value: &T,
) -> Result<(), Error> {
fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, value: &T) -> Result<(), Error> {
match mem::replace(&mut self.state, PairState::Done) {
PairState::WaitingForKey => {
let key_sink = KeySink::new(|key| Ok(key.into()));
let key_serializer = PartSerializer::new(key_sink);
self.state = PairState::WaitingForValue {
key: value.serialize(key_serializer)?,
};
self.state = PairState::WaitingForValue { key: value.serialize(key_serializer)? };
Ok(())
}
PairState::WaitingForValue { key } => {

View file

@ -18,19 +18,13 @@ pub trait Sink: Sized {
type Ok;
type SerializeSeq: ser::SerializeSeq<Ok = Self::Ok, Error = Error>;
fn serialize_static_str(
self,
value: &'static str,
) -> Result<Self::Ok, Error>;
fn serialize_static_str(self, value: &'static str) -> Result<Self::Ok, Error>;
fn serialize_str(self, value: &str) -> Result<Self::Ok, Error>;
fn serialize_string(self, value: String) -> Result<Self::Ok, Error>;
fn serialize_none(self) -> Result<Self::Ok, Error>;
fn serialize_some<T: ?Sized + ser::Serialize>(
self,
value: &T,
) -> Result<Self::Ok, Error>;
fn serialize_some<T: ?Sized + ser::Serialize>(self, value: &T) -> Result<Self::Ok, Error>;
fn serialize_seq(self) -> Result<Self::SerializeSeq, Error>;
@ -49,8 +43,7 @@ impl<S: Sink> ser::Serializer for PartSerializer<S> {
type SerializeStructVariant = ser::Impossible<S::Ok, Error>;
fn serialize_bool(self, v: bool) -> Result<S::Ok, Error> {
self.sink
.serialize_static_str(if v { "true" } else { "false" })
self.sink.serialize_static_str(if v { "true" } else { "false" })
}
fn serialize_i8(self, v: i8) -> Result<S::Ok, Error> {
@ -147,24 +140,15 @@ impl<S: Sink> ser::Serializer for PartSerializer<S> {
self.sink.serialize_none()
}
fn serialize_some<T: ?Sized + ser::Serialize>(
self,
value: &T,
) -> Result<S::Ok, Error> {
fn serialize_some<T: ?Sized + ser::Serialize>(self, value: &T) -> Result<S::Ok, Error> {
self.sink.serialize_some(value)
}
fn serialize_seq(
self,
_len: Option<usize>,
) -> Result<Self::SerializeSeq, Error> {
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Error> {
self.sink.serialize_seq()
}
fn serialize_tuple(
self,
_len: usize,
) -> Result<Self::SerializeTuple, Error> {
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Error> {
Err(self.sink.unsupported())
}
@ -186,10 +170,7 @@ impl<S: Sink> ser::Serializer for PartSerializer<S> {
Err(self.sink.unsupported())
}
fn serialize_map(
self,
_len: Option<usize>,
) -> Result<Self::SerializeMap, Error> {
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Error> {
Err(self.sink.unsupported())
}

View file

@ -1,9 +1,7 @@
use std::str;
use serde::ser;
use url::form_urlencoded::{
Serializer as UrlEncodedSerializer, Target as UrlEncodedTarget,
};
use url::form_urlencoded::{Serializer as UrlEncodedSerializer, Target as UrlEncodedTarget};
use super::{
part::{PartSerializer, Sink},
@ -27,16 +25,11 @@ where
urlencoder: &'target mut UrlEncodedSerializer<'input, Target>,
key: &'key str,
) -> Self {
ValueSink {
urlencoder,
key,
nested: false,
}
ValueSink { urlencoder, key, nested: false }
}
}
impl<'input, 'key, 'target, Target> Sink
for ValueSink<'input, 'key, 'target, Target>
impl<'input, 'key, 'target, Target> Sink for ValueSink<'input, 'key, 'target, Target>
where
Target: 'target + UrlEncodedTarget,
{
@ -60,10 +53,7 @@ where
Ok(())
}
fn serialize_some<T: ?Sized + ser::Serialize>(
self,
value: &T,
) -> Result<Self::Ok, Error> {
fn serialize_some<T: ?Sized + ser::Serialize>(self, value: &T) -> Result<Self::Ok, Error> {
value.serialize(PartSerializer::new(self))
}
@ -80,18 +70,14 @@ where
}
}
impl<'input, 'key, 'target, Target> ser::SerializeSeq
for ValueSink<'input, 'key, 'target, Target>
impl<'input, 'key, 'target, Target> ser::SerializeSeq for ValueSink<'input, 'key, 'target, Target>
where
Target: 'target + UrlEncodedTarget,
{
type Ok = ();
type Error = Error;
fn serialize_element<T: ?Sized>(
&mut self,
value: &T,
) -> Result<(), Self::Error>
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
where
T: ser::Serialize,
{

View file

@ -38,18 +38,12 @@ fn deserialize_borrowed_str() {
fn deserialize_reader() {
let result = vec![("first".to_owned(), 23), ("last".to_owned(), 42)];
assert_eq!(
urlencoded::from_reader(b"first=23&last=42" as &[_]),
Ok(result)
);
assert_eq!(urlencoded::from_reader(b"first=23&last=42" as &[_]), Ok(result));
}
#[test]
fn deserialize_option() {
let result = vec![
("first".to_owned(), Some(23)),
("last".to_owned(), Some(42)),
];
let result = vec![("first".to_owned(), Some(23)), ("last".to_owned(), Some(42))];
assert_eq!(urlencoded::from_str("first=23&last=42"), Ok(result));
}
@ -70,8 +64,7 @@ enum X {
#[test]
fn deserialize_unit_enum() {
let result: Vec<(String, X)> =
urlencoded::from_str("one=A&two=B&three=C").unwrap();
let result: Vec<(String, X)> = urlencoded::from_str("one=A&two=B&three=C").unwrap();
assert_eq!(result.len(), 3);
assert!(result.contains(&("one".to_owned(), X::A)));
@ -93,11 +86,7 @@ struct Params<'a> {
#[test]
fn deserialize_struct() {
let de = Params {
a: 10,
b: "Hello",
c: None,
};
let de = Params { a: 10, b: "Hello", c: None };
assert_eq!(urlencoded::from_str("a=10&b=Hello"), Ok(de));
assert_eq!(urlencoded::from_str("b=Hello&a=10"), Ok(de));
}
@ -110,10 +99,7 @@ fn deserialize_list_of_str() {
Err(error) if error.to_string().contains("unsupported")
);
assert_eq!(
urlencoded::from_str("a=a&a=b"),
Ok(vec![("a", vec!["a", "b"])])
)
assert_eq!(urlencoded::from_str("a=a&a=b"), Ok(vec![("a", vec!["a", "b"])]))
}
#[test]
@ -126,18 +112,12 @@ fn deserialize_multiple_lists() {
assert_eq!(
urlencoded::from_str("xs=true&xs=false&ys=3&ys=2&ys=1"),
Ok(Lists {
xs: vec![true, false],
ys: vec![3, 2, 1],
})
Ok(Lists { xs: vec![true, false], ys: vec![3, 2, 1] })
);
assert_eq!(
urlencoded::from_str("ys=3&xs=true&ys=2&xs=false&ys=1"),
Ok(Lists {
xs: vec![true, false],
ys: vec![3, 2, 1],
})
Ok(Lists { xs: vec![true, false], ys: vec![3, 2, 1] })
);
}
@ -172,12 +152,7 @@ fn deserialize_with_serde_attributes() {
assert_eq!(
urlencoded::from_str(""),
Ok(FieldsWithAttributes {
xs: vec![],
def: None,
time: None,
flag: false,
})
Ok(FieldsWithAttributes { xs: vec![], def: None, time: None, flag: false })
);
}
@ -196,10 +171,7 @@ fn deserialize_list_of_option() {
#[test]
fn deserialize_list_of_newtype() {
assert_eq!(
urlencoded::from_str("list=test"),
Ok(vec![("list", vec![NewType("test")])])
);
assert_eq!(urlencoded::from_str("list=test"), Ok(vec![("list", vec![NewType("test")])]));
}
#[test]
@ -239,17 +211,13 @@ struct ListStruct {
#[test]
fn deserialize_newstruct() {
let de = NewStruct {
list: vec!["hello", "world"],
};
let de = NewStruct { list: vec!["hello", "world"] };
assert_eq!(urlencoded::from_str("list=hello&list=world"), Ok(de));
}
#[test]
fn deserialize_numlist() {
let de = NumList {
list: vec![1, 2, 3, 4],
};
let de = NumList { list: vec![1, 2, 3, 4] };
assert_eq!(urlencoded::from_str("list=1&list=2&list=3&list=4"), Ok(de));
}
@ -275,18 +243,10 @@ struct InnerList<T> {
fn deserialize_nested_struct() {
let mut encoder = Encoder::new(String::new());
let nested = Nested {
item: Inner {
c: "hello",
a: 10,
b: "bye",
},
};
let nested = Nested { item: Inner { c: "hello", a: 10, b: "bye" } };
assert_eq!(
urlencoded::from_str(
&encoder
.append_pair("item", r#"{"c":"hello","a":10,"b":"bye"}"#)
.finish(),
&encoder.append_pair("item", r#"{"c":"hello","a":10,"b":"bye"}"#).finish(),
),
Ok(nested)
);
@ -297,16 +257,10 @@ fn deserialize_nested_struct() {
fn deserialize_nested_struct_with_list() {
let mut encoder = Encoder::new(String::new());
let nested = Nested {
item: InnerList {
list: vec![1, 2, 3],
},
};
let nested = Nested { item: InnerList { list: vec![1, 2, 3] } };
assert_eq!(
urlencoded::from_str(
&encoder.append_pair("item", r#"{"list":[1,2,3]}"#).finish(),
),
urlencoded::from_str(&encoder.append_pair("item", r#"{"list":[1,2,3]}"#).finish(),),
Ok(nested)
);
}
@ -316,17 +270,9 @@ fn deserialize_nested_struct_with_list() {
fn deserialize_nested_list_option() {
let mut encoder = Encoder::new(String::new());
let nested = Nested {
item: InnerList {
list: vec![Some(1), Some(2), None],
},
};
let nested = Nested { item: InnerList { list: vec![Some(1), Some(2), None] } };
assert_eq!(
urlencoded::from_str(
&encoder
.append_pair("item", r#"{"list":[1,2,null]}"#)
.finish(),
),
urlencoded::from_str(&encoder.append_pair("item", r#"{"list":[1,2,null]}"#).finish(),),
Ok(nested)
);
}

View file

@ -16,44 +16,28 @@ fn serialize_newtype_i32() {
fn serialize_option_map_int() {
let params = &[("first", Some(23)), ("middle", None), ("last", Some(42))];
assert_eq!(
urlencoded::to_string(params),
Ok("first=23&last=42".to_owned())
);
assert_eq!(urlencoded::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!(
urlencoded::to_string(params),
Ok("first=hello&last=world".to_owned())
);
assert_eq!(urlencoded::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!(
urlencoded::to_string(params),
Ok("one=true&two=false".to_owned())
);
assert_eq!(urlencoded::to_string(params), Ok("one=true&two=false".to_owned()));
}
#[test]
fn serialize_map_bool() {
let params = &[("one", true), ("two", false)];
assert_eq!(
urlencoded::to_string(params),
Ok("one=true&two=false".to_owned())
);
assert_eq!(urlencoded::to_string(params), Ok("one=true&two=false".to_owned()));
}
#[derive(Serialize)]
@ -66,10 +50,7 @@ enum X {
#[test]
fn serialize_unit_enum() {
let params = &[("one", X::A), ("two", X::B), ("three", X::C)];
assert_eq!(
urlencoded::to_string(params),
Ok("one=A&two=B&three=C".to_owned())
);
assert_eq!(urlencoded::to_string(params), Ok("one=A&two=B&three=C".to_owned()));
}
#[derive(Serialize)]
@ -89,10 +70,7 @@ fn serialize_unit_type() {
fn serialize_list_of_str() {
let params = &[("list", vec!["hello", "world"])];
assert_eq!(
urlencoded::to_string(params),
Ok("list=hello&list=world".to_owned())
);
assert_eq!(urlencoded::to_string(params), Ok("list=hello&list=world".to_owned()));
}
#[test]
@ -103,15 +81,9 @@ fn serialize_multiple_lists() {
ys: Vec<u32>,
}
let params = Lists {
xs: vec![true, false],
ys: vec![3, 2, 1],
};
let params = Lists { xs: vec![true, false], ys: vec![3, 2, 1] };
assert_eq!(
urlencoded::to_string(params),
Ok("xs=true&xs=false&ys=3&ys=2&ys=1".to_owned())
);
assert_eq!(urlencoded::to_string(params), Ok("xs=true&xs=false&ys=3&ys=2&ys=1".to_owned()));
}
#[test]
@ -126,10 +98,7 @@ fn serialize_nested_list() {
#[test]
fn serialize_list_of_option() {
let params = &[("list", vec![Some(10), Some(100)])];
assert_eq!(
urlencoded::to_string(params),
Ok("list=10&list=100".to_owned())
);
assert_eq!(urlencoded::to_string(params), Ok("list=10&list=100".to_owned()));
}
#[test]
@ -141,10 +110,7 @@ fn serialize_list_of_newtype() {
#[test]
fn serialize_list_of_enum() {
let params = &[("item", vec![X::A, X::B, X::C])];
assert_eq!(
urlencoded::to_string(params),
Ok("item=A&item=B&item=C".to_owned())
);
assert_eq!(urlencoded::to_string(params), Ok("item=A&item=B&item=C".to_owned()));
}
#[test]
@ -180,17 +146,9 @@ struct InnerList<T> {
fn serialize_nested_struct() {
let mut encoder = Encoder::new(String::new());
let s = Nested {
item: Inner {
c: "hello".into(),
a: 10,
b: "bye".into(),
},
};
let s = Nested { item: Inner { c: "hello".into(), a: 10, b: "bye".into() } };
assert_eq!(
encoder
.append_pair("item", r#"{"c":"hello","a":10,"b":"bye"}"#)
.finish(),
encoder.append_pair("item", r#"{"c":"hello","a":10,"b":"bye"}"#).finish(),
urlencoded::to_string(s).unwrap()
);
}
@ -200,11 +158,7 @@ fn serialize_nested_struct() {
fn serialize_nested_struct_with_list() {
let mut encoder = Encoder::new(String::new());
let s = Nested {
item: InnerList {
list: vec![1, 2, 3],
},
};
let s = Nested { item: InnerList { list: vec![1, 2, 3] } };
assert_eq!(
encoder.append_pair("item", r#"{"list":[1,2,3]}"#).finish(),
urlencoded::to_string(s).unwrap()