Skip to content

Commit 458672b

Browse files
committed
Format with rustfmt-nightly
1 parent f3267b7 commit 458672b

File tree

7 files changed

+622
-536
lines changed

7 files changed

+622
-536
lines changed

src/error.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use serde::de::Error as SerdeError;
77
pub enum Error {
88
ParseIntError(num::ParseIntError),
99
Syntax(reader::Error),
10-
Custom(String)
10+
Custom(String),
1111
}
1212

1313
pub type VResult<V> = Result<V, Error>;
@@ -16,7 +16,11 @@ macro_rules! expect {
1616
($actual: expr, $($expected: pat)|+ => $if_ok: expr) => {
1717
match $actual {
1818
$($expected)|+ => $if_ok,
19-
actual => Err($crate::Error::Custom(format!("Expected token {}, found {:?}", stringify!($($expected)|+), actual)))
19+
actual => Err($crate::Error::Custom(format!(
20+
"Expected token {}, found {:?}",
21+
stringify!($($expected)|+),
22+
actual
23+
)))
2024
}
2125
}
2226
}
@@ -26,7 +30,11 @@ macro_rules! debug_expect {
2630
($actual: expr, $($expected: pat)|+ => $if_ok: expr) => {
2731
match $actual {
2832
$($expected)|+ => $if_ok,
29-
actual => panic!("Internal error: Expected token {}, found {:?}", stringify!($($expected)|+), actual)
33+
actual => panic!(
34+
"Internal error: Expected token {}, found {:?}",
35+
stringify!($($expected)|+),
36+
actual
37+
)
3038
}
3139
}
3240
}
@@ -46,7 +54,7 @@ impl Display for Error {
4654
match *self {
4755
Error::ParseIntError(ref error) => Display::fmt(error, fmt),
4856
Error::Syntax(ref error) => Display::fmt(error, fmt),
49-
Error::Custom(ref display) => Display::fmt(display, fmt)
57+
Error::Custom(ref display) => Display::fmt(display, fmt),
5058
}
5159
}
5260
}
@@ -56,7 +64,7 @@ impl Debug for Error {
5664
match *self {
5765
Error::ParseIntError(ref error) => Display::fmt(error, fmt),
5866
Error::Syntax(ref error) => Debug::fmt(error, fmt),
59-
Error::Custom(ref display) => Display::fmt(display, fmt)
67+
Error::Custom(ref display) => Display::fmt(display, fmt),
6068
}
6169
}
6270
}
@@ -66,15 +74,15 @@ impl StdError for Error {
6674
match *self {
6775
Error::ParseIntError(ref error) => error.description(),
6876
Error::Syntax(ref error) => error.description(),
69-
Error::Custom(_) => "other error"
77+
Error::Custom(_) => "other error",
7078
}
7179
}
7280

7381
fn cause(&self) -> Option<&StdError> {
7482
match *self {
7583
Error::ParseIntError(ref error) => Some(error),
7684
Error::Syntax(ref error) => Some(error),
77-
_ => None
85+
_ => None,
7886
}
7987
}
8088
}

src/lib.rs

Lines changed: 68 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
extern crate xml;
2-
#[macro_use] extern crate serde;
3-
#[macro_use] extern crate log;
2+
#[macro_use]
3+
extern crate serde;
4+
#[macro_use]
5+
extern crate log;
46

5-
#[macro_use] mod error;
7+
#[macro_use]
8+
mod error;
69
mod map;
710
mod seq;
811
mod var;
@@ -23,7 +26,7 @@ pub struct Deserializer<R: Read> {
2326
depth: usize,
2427
reader: EventReader<R>,
2528
peeked: Option<XmlEvent>,
26-
is_map_value: bool
29+
is_map_value: bool,
2730
}
2831

2932
pub fn deserialize<'de, R: Read, T: Deserialize<'de>>(reader: R) -> Result<T, Error> {
@@ -36,18 +39,21 @@ impl<'de, R: Read> Deserializer<R> {
3639
depth: 0,
3740
reader: reader,
3841
peeked: None,
39-
is_map_value: false
42+
is_map_value: false,
4043
}
4144
}
4245

4346
pub fn new_from_reader(reader: R) -> Self {
44-
Self::new(EventReader::new_with_config(reader, ParserConfig {
45-
trim_whitespace: true,
46-
whitespace_to_characters: true,
47-
cdata_to_characters: true,
48-
ignore_comments: true,
49-
coalesce_characters: true
50-
}))
47+
Self::new(EventReader::new_with_config(
48+
reader,
49+
ParserConfig {
50+
trim_whitespace: true,
51+
whitespace_to_characters: true,
52+
cdata_to_characters: true,
53+
ignore_comments: true,
54+
coalesce_characters: true,
55+
},
56+
))
5157
}
5258

5359
fn peek(&mut self) -> Result<&XmlEvent, Error> {
@@ -66,9 +72,9 @@ impl<'de, R: Read> Deserializer<R> {
6672
XmlEvent::StartDocument { .. } |
6773
XmlEvent::EndDocument { .. } |
6874
XmlEvent::ProcessingInstruction { .. } |
69-
XmlEvent::Comment(_) => {/* skip */}
75+
XmlEvent::Comment(_) => { /* skip */ }
7076

71-
other => return Ok(other)
77+
other => return Ok(other),
7278
}
7379
}
7480
}
@@ -82,10 +88,10 @@ impl<'de, R: Read> Deserializer<R> {
8288
match next {
8389
XmlEvent::StartElement { .. } => {
8490
self.depth += 1;
85-
},
91+
}
8692
XmlEvent::EndElement { .. } => {
8793
self.depth -= 1;
88-
},
94+
}
8995
_ => {}
9096
}
9197
debug!("Fetched {:?}", next);
@@ -100,7 +106,10 @@ impl<'de, R: Read> Deserializer<R> {
100106
::std::mem::replace(&mut self.is_map_value, false)
101107
}
102108

103-
fn read_inner_value<V: Visitor<'de>, F: FnOnce(&mut Self) -> VResult<V::Value>>(&mut self, f: F) -> VResult<V::Value> {
109+
fn read_inner_value<V: Visitor<'de>, F: FnOnce(&mut Self) -> VResult<V::Value>>(
110+
&mut self,
111+
f: F,
112+
) -> VResult<V::Value> {
104113
if self.unset_map_value() {
105114
debug_expect!(self.next(), Ok(XmlEvent::StartElement { name, .. }) => {
106115
let result = f(self)?;
@@ -117,16 +126,21 @@ impl<'de, R: Read> Deserializer<R> {
117126
if name == start_name {
118127
Ok(())
119128
} else {
120-
Err(Error::Custom(format!("End tag </{}> didn't match the start tag <{}>", name.local_name, start_name.local_name)))
129+
Err(Error::Custom(format!(
130+
"End tag </{}> didn't match the start tag <{}>",
131+
name.local_name,
132+
start_name.local_name
133+
)))
121134
}
122135
})
123136
}
124137

125138

126139
fn parse_int<N, V, F>(&mut self, visit: F) -> VResult<V::Value>
127-
where N: std::str::FromStr<Err=std::num::ParseIntError>,
128-
V: Visitor<'de>,
129-
F: FnOnce(N) -> VResult<V::Value>,
140+
where
141+
N: std::str::FromStr<Err = std::num::ParseIntError>,
142+
V: Visitor<'de>,
143+
F: FnOnce(N) -> VResult<V::Value>,
130144
{
131145
self.read_inner_value::<V, _>(|this| {
132146
if let XmlEvent::EndElement { .. } = *this.peek()? {
@@ -148,10 +162,19 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {
148162
newtype_struct identifier
149163
}
150164

151-
fn deserialize_struct<V: Visitor<'de>>(self, _name: &'static str, fields: &'static [&'static str], visitor: V) -> VResult<V::Value> {
165+
fn deserialize_struct<V: Visitor<'de>>(
166+
self,
167+
_name: &'static str,
168+
fields: &'static [&'static str],
169+
visitor: V,
170+
) -> VResult<V::Value> {
152171
self.unset_map_value();
153172
expect!(self.next()?, XmlEvent::StartElement { name, attributes, .. } => {
154-
let map_value = visitor.visit_map(MapAccess::new(self, attributes, fields.contains(&"$value")))?;
173+
let map_value = visitor.visit_map(MapAccess::new(
174+
self,
175+
attributes,
176+
fields.contains(&"$value")
177+
))?;
155178
self.expect_end_element(name)?;
156179
Ok(map_value)
157180
})
@@ -225,22 +248,34 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {
225248
})
226249
}
227250

228-
fn deserialize_unit_struct<V: Visitor<'de>>(self, _name: &'static str, visitor: V) -> VResult<V::Value> {
251+
fn deserialize_unit_struct<V: Visitor<'de>>(
252+
self,
253+
_name: &'static str,
254+
visitor: V,
255+
) -> VResult<V::Value> {
229256
self.deserialize_unit(visitor)
230257
}
231258

232-
fn deserialize_tuple_struct<V: Visitor<'de>>(self, _name: &'static str, len: usize, visitor: V) -> VResult<V::Value> {
259+
fn deserialize_tuple_struct<V: Visitor<'de>>(
260+
self,
261+
_name: &'static str,
262+
len: usize,
263+
visitor: V,
264+
) -> VResult<V::Value> {
233265
self.deserialize_tuple(len, visitor)
234266
}
235267

236268
fn deserialize_tuple<V: Visitor<'de>>(self, len: usize, visitor: V) -> VResult<V::Value> {
237269
visitor.visit_seq(SeqAccess::new(self, Some(len)))
238270
}
239271

240-
fn deserialize_enum<V: Visitor<'de>>(self, _name: &'static str, _variants: &'static [&'static str], visitor: V) -> VResult<V::Value> {
241-
self.read_inner_value::<V, _>(|this| {
242-
visitor.visit_enum(EnumAccess::new(this))
243-
})
272+
fn deserialize_enum<V: Visitor<'de>>(
273+
self,
274+
_name: &'static str,
275+
_variants: &'static [&'static str],
276+
visitor: V,
277+
) -> VResult<V::Value> {
278+
self.read_inner_value::<V, _>(|this| visitor.visit_enum(EnumAccess::new(this)))
244279
}
245280

246281
fn deserialize_string<V: Visitor<'de>>(self, visitor: V) -> VResult<V::Value> {
@@ -270,7 +305,7 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {
270305
fn deserialize_option<V: Visitor<'de>>(self, visitor: V) -> VResult<V::Value> {
271306
match *self.peek()? {
272307
XmlEvent::EndElement { .. } => visitor.visit_none(),
273-
_ => visitor.visit_some(self)
308+
_ => visitor.visit_some(self),
274309
}
275310
}
276311

@@ -288,15 +323,9 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {
288323

289324
fn deserialize_any<V: Visitor<'de>>(self, visitor: V) -> VResult<V::Value> {
290325
match *self.peek()? {
291-
XmlEvent::StartElement { .. } => {
292-
self.deserialize_map(visitor)
293-
}
294-
XmlEvent::EndElement { .. } => {
295-
self.deserialize_unit(visitor)
296-
}
297-
_ => {
298-
self.deserialize_string(visitor)
299-
}
326+
XmlEvent::StartElement { .. } => self.deserialize_map(visitor),
327+
XmlEvent::EndElement { .. } => self.deserialize_unit(visitor),
328+
_ => self.deserialize_string(visitor),
300329
}
301330
}
302331
}

src/map.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub struct MapAccess<'a, R: 'a + Read> {
99
attrs: ::std::vec::IntoIter<OwnedAttribute>,
1010
next_value: Option<String>,
1111
de: &'a mut Deserializer<R>,
12-
inner_value: bool
12+
inner_value: bool,
1313
}
1414

1515
impl<'a, R: 'a + Read> MapAccess<'a, R> {
@@ -18,34 +18,40 @@ impl<'a, R: 'a + Read> MapAccess<'a, R> {
1818
attrs: attrs.into_iter(),
1919
next_value: None,
2020
de: de,
21-
inner_value: inner_value
21+
inner_value: inner_value,
2222
}
2323
}
2424
}
2525

2626
impl<'de, 'a, R: 'a + Read> de::MapAccess<'de> for MapAccess<'a, R> {
2727
type Error = Error;
2828

29-
fn next_key_seed<K: DeserializeSeed<'de>>(&mut self, seed: K) -> Result<Option<K::Value>, Error> {
29+
fn next_key_seed<K: DeserializeSeed<'de>>(
30+
&mut self,
31+
seed: K,
32+
) -> Result<Option<K::Value>, Error> {
3033
debug_assert_eq!(self.next_value, None);
3134
match self.attrs.next() {
3235
Some(OwnedAttribute { name, value }) => {
3336
self.next_value = Some(value);
34-
seed.deserialize(name.local_name.into_deserializer()).map(Some)
37+
seed.deserialize(name.local_name.into_deserializer())
38+
.map(Some)
3539
}
3640
None => {
3741
match *self.de.peek()? {
3842
XmlEvent::StartElement { ref name, .. } => {
39-
seed.deserialize(if !self.inner_value {
40-
name.local_name.as_str()
41-
} else {
42-
"$value"
43-
}.into_deserializer()).map(Some)
43+
seed.deserialize(
44+
if !self.inner_value {
45+
name.local_name.as_str()
46+
} else {
47+
"$value"
48+
}.into_deserializer(),
49+
).map(Some)
4450
}
4551
XmlEvent::Characters(_) => {
4652
seed.deserialize("$value".into_deserializer()).map(Some)
4753
}
48-
_ => Ok(None)
54+
_ => Ok(None),
4955
}
5056
}
5157
}
@@ -85,7 +91,12 @@ impl<'de> de::Deserializer<'de> for AttrValueDeserializer {
8591
visitor.visit_u8(u)
8692
}
8793

88-
fn deserialize_enum<V: Visitor<'de>>(self, _name: &str, _variants: &'static [&'static str], visitor: V) -> VResult<V::Value> {
94+
fn deserialize_enum<V: Visitor<'de>>(
95+
self,
96+
_name: &str,
97+
_variants: &'static [&'static str],
98+
visitor: V,
99+
) -> VResult<V::Value> {
89100
visitor.visit_enum(self.0.into_deserializer())
90101
}
91102

src/seq.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::de::{self, DeserializeSeed};
66
pub struct SeqAccess<'a, R: 'a + Read> {
77
de: &'a mut Deserializer<R>,
88
max_size: Option<usize>,
9-
expected_name: Option<String>
9+
expected_name: Option<String>,
1010
}
1111

1212
impl<'a, R: 'a + Read> SeqAccess<'a, R> {
@@ -21,15 +21,18 @@ impl<'a, R: 'a + Read> SeqAccess<'a, R> {
2121
SeqAccess {
2222
de: de,
2323
max_size: max_size,
24-
expected_name: expected_name
24+
expected_name: expected_name,
2525
}
2626
}
2727
}
2828

2929
impl<'de, 'a, R: 'a + Read> de::SeqAccess<'de> for SeqAccess<'a, R> {
3030
type Error = Error;
3131

32-
fn next_element_seed<T: DeserializeSeed<'de>>(&mut self, seed: T) -> Result<Option<T::Value>, Error> {
32+
fn next_element_seed<T: DeserializeSeed<'de>>(
33+
&mut self,
34+
seed: T,
35+
) -> Result<Option<T::Value>, Error> {
3336
match self.max_size.as_mut() {
3437
Some(&mut 0) => {
3538
return Ok(None);
@@ -45,7 +48,7 @@ impl<'de, 'a, R: 'a + Read> de::SeqAccess<'de> for SeqAccess<'a, R> {
4548
}
4649
(&XmlEvent::EndElement { .. }, None) => false,
4750
(_, Some(_)) => false,
48-
(_, None) => true
51+
(_, None) => true,
4952
};
5053
if more {
5154
if self.expected_name.is_some() {

0 commit comments

Comments
 (0)