Skip to content

Commit 56d7623

Browse files
authored
Merge pull request #8 from Michael-F-Bryan/serialize
Basic implementation of Serializer
2 parents 458672b + 603ca08 commit 56d7623

File tree

4 files changed

+559
-35
lines changed

4 files changed

+559
-35
lines changed

src/error.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
use xml::reader;
22
use std::fmt::{self, Display, Debug};
33
use std::error::Error as StdError;
4-
use std::num;
5-
use serde::de::Error as SerdeError;
4+
use std::num::ParseIntError;
5+
use std::io;
6+
use serde::de::Error as DeError;
7+
use serde::ser::Error as SerError;
68

79
pub enum Error {
8-
ParseIntError(num::ParseIntError),
10+
ParseIntError(ParseIntError),
911
Syntax(reader::Error),
1012
Custom(String),
13+
Io(io::Error),
14+
UnsupportedOperation(String),
1115
}
1216

1317
pub type VResult<V> = Result<V, Error>;
@@ -55,6 +59,8 @@ impl Display for Error {
5559
Error::ParseIntError(ref error) => Display::fmt(error, fmt),
5660
Error::Syntax(ref error) => Display::fmt(error, fmt),
5761
Error::Custom(ref display) => Display::fmt(display, fmt),
62+
Error::Io(ref err) => Display::fmt(err, fmt),
63+
Error::UnsupportedOperation(ref msg) => write!(fmt, "Unsupported Operation: {}", msg),
5864
}
5965
}
6066
}
@@ -65,29 +71,46 @@ impl Debug for Error {
6571
Error::ParseIntError(ref error) => Display::fmt(error, fmt),
6672
Error::Syntax(ref error) => Debug::fmt(error, fmt),
6773
Error::Custom(ref display) => Display::fmt(display, fmt),
74+
Error::Io(ref err) => Display::fmt(err, fmt),
75+
Error::UnsupportedOperation(ref msg) => write!(fmt, "Unsupported Operation: {}", msg),
6876
}
6977
}
7078
}
7179

80+
impl From<io::Error> for Error {
81+
fn from(other: io::Error) -> Self {
82+
Error::Io(other)
83+
}
84+
}
85+
7286
impl StdError for Error {
7387
fn description(&self) -> &str {
7488
match *self {
7589
Error::ParseIntError(ref error) => error.description(),
7690
Error::Syntax(ref error) => error.description(),
91+
Error::Io(ref error) => error.description(),
7792
Error::Custom(_) => "other error",
93+
Error::UnsupportedOperation(_) => "Unsupported Operation",
7894
}
7995
}
8096

8197
fn cause(&self) -> Option<&StdError> {
8298
match *self {
8399
Error::ParseIntError(ref error) => Some(error),
84100
Error::Syntax(ref error) => Some(error),
101+
Error::Io(ref error) => Some(error),
85102
_ => None,
86103
}
87104
}
88105
}
89106

90-
impl SerdeError for Error {
107+
impl DeError for Error {
108+
fn custom<T: Display>(msg: T) -> Self {
109+
Error::Custom(msg.to_string())
110+
}
111+
}
112+
113+
impl SerError for Error {
91114
fn custom<T: Display>(msg: T) -> Self {
92115
Error::Custom(msg.to_string())
93116
}

src/lib.rs

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@ extern crate serde;
44
#[macro_use]
55
extern crate log;
66

7+
#[cfg(test)]
8+
#[macro_use]
9+
extern crate serde_derive;
10+
711
#[macro_use]
812
mod error;
913
mod map;
1014
mod seq;
1115
mod var;
16+
mod serialize;
1217

1318
pub use error::Error;
1419
pub use xml::reader::{EventReader, ParserConfig};
20+
pub use serialize::{serialize, Serializer};
1521

1622
use error::VResult;
1723
use xml::reader::XmlEvent;
@@ -72,7 +78,7 @@ impl<'de, R: Read> Deserializer<R> {
7278
XmlEvent::StartDocument { .. } |
7379
XmlEvent::EndDocument { .. } |
7480
XmlEvent::ProcessingInstruction { .. } |
75-
XmlEvent::Comment(_) => { /* skip */ }
81+
XmlEvent::Comment(_) => { /* skip */ },
7682

7783
other => return Ok(other),
7884
}
@@ -88,11 +94,11 @@ impl<'de, R: Read> Deserializer<R> {
8894
match next {
8995
XmlEvent::StartElement { .. } => {
9096
self.depth += 1;
91-
}
97+
},
9298
XmlEvent::EndElement { .. } => {
9399
self.depth -= 1;
94-
}
95-
_ => {}
100+
},
101+
_ => {},
96102
}
97103
debug!("Fetched {:?}", next);
98104
Ok(next)
@@ -106,10 +112,8 @@ impl<'de, R: Read> Deserializer<R> {
106112
::std::mem::replace(&mut self.is_map_value, false)
107113
}
108114

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> {
115+
fn read_inner_value<V: Visitor<'de>, F: FnOnce(&mut Self) -> VResult<V::Value>>(&mut self, f: F)
116+
-> VResult<V::Value> {
113117
if self.unset_map_value() {
114118
debug_expect!(self.next(), Ok(XmlEvent::StartElement { name, .. }) => {
115119
let result = f(self)?;
@@ -162,12 +166,8 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {
162166
newtype_struct identifier
163167
}
164168

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> {
169+
fn deserialize_struct<V: Visitor<'de>>(self, _name: &'static str, fields: &'static [&'static str], visitor: V)
170+
-> VResult<V::Value> {
171171
self.unset_map_value();
172172
expect!(self.next()?, XmlEvent::StartElement { name, attributes, .. } => {
173173
let map_value = visitor.visit_map(MapAccess::new(
@@ -248,33 +248,21 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {
248248
})
249249
}
250250

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

259-
fn deserialize_tuple_struct<V: Visitor<'de>>(
260-
self,
261-
_name: &'static str,
262-
len: usize,
263-
visitor: V,
264-
) -> VResult<V::Value> {
255+
fn deserialize_tuple_struct<V: Visitor<'de>>(self, _name: &'static str, len: usize, visitor: V)
256+
-> VResult<V::Value> {
265257
self.deserialize_tuple(len, visitor)
266258
}
267259

268260
fn deserialize_tuple<V: Visitor<'de>>(self, len: usize, visitor: V) -> VResult<V::Value> {
269261
visitor.visit_seq(SeqAccess::new(self, Some(len)))
270262
}
271263

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> {
264+
fn deserialize_enum<V: Visitor<'de>>(self, _name: &'static str, _variants: &'static [&'static str], visitor: V)
265+
-> VResult<V::Value> {
278266
self.read_inner_value::<V, _>(|this| visitor.visit_enum(EnumAccess::new(this)))
279267
}
280268

0 commit comments

Comments
 (0)