Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions src/de/map.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
use std::io::Read;

use serde::de::value::MapDeserializer;
use serde::de::{self, IntoDeserializer};
use xml::attribute::OwnedAttribute;
use xml::namespace::Namespace;
use xml::reader::XmlEvent;

use Deserializer;
use error::{Error, Result};
use Deserializer;

pub struct MapAccess<'a, R: 'a + Read> {
attrs: ::std::vec::IntoIter<OwnedAttribute>,
next_value: Option<String>,
de: &'a mut Deserializer<R>,
ns: Option<Namespace>,
attrs: ::std::vec::IntoIter<OwnedAttribute>,
inner_value: bool,
next_value: Option<String>,
}

impl<'a, R: 'a + Read> MapAccess<'a, R> {
pub fn new(de: &'a mut Deserializer<R>, attrs: Vec<OwnedAttribute>, inner_value: bool) -> Self {
/// ns should only be Some if the map has a "$xmlns" field.
pub fn new(
de: &'a mut Deserializer<R>,
attrs: Vec<OwnedAttribute>,
ns: Option<Namespace>,
inner_value: bool,
) -> Self {
MapAccess {
de,
ns,
attrs: attrs.into_iter(),
inner_value,
next_value: None,
de: de,
inner_value: inner_value,
}
}
}
Expand All @@ -29,28 +39,37 @@ impl<'de, 'a, R: 'a + Read> de::MapAccess<'de> for MapAccess<'a, R> {
type Error = Error;

fn next_key_seed<K: de::DeserializeSeed<'de>>(&mut self, seed: K) -> Result<Option<K::Value>> {
debug_assert_eq!(self.next_value, None);
if self.ns.is_some() {
return seed.deserialize("$xmlns".into_deserializer()).map(Some);
}
match self.attrs.next() {
Some(OwnedAttribute { name, value }) => {
self.next_value = Some(value);
seed.deserialize(name.local_name.into_deserializer())
.map(Some)
},
None => match *self.de.peek()? {
XmlEvent::StartElement { ref name, .. } => seed.deserialize(
if !self.inner_value {
name.local_name.as_str()
} else {
"$value"
}.into_deserializer(),
).map(Some),
XmlEvent::StartElement { ref name, .. } => seed
.deserialize(
if !self.inner_value {
name.local_name.as_str()
} else {
"$value"
}
.into_deserializer(),
)
.map(Some),
XmlEvent::Characters(_) => seed.deserialize("$value".into_deserializer()).map(Some),
_ => Ok(None),
},
}
}

fn next_value_seed<V: de::DeserializeSeed<'de>>(&mut self, seed: V) -> Result<V::Value> {
// Insert the xmlns
if let Some(ns) = self.ns.take() {
return seed.deserialize(MapDeserializer::new(ns.into_iter()));
}
match self.next_value.take() {
Some(value) => seed.deserialize(AttrValueDeserializer(value)),
None => {
Expand Down
29 changes: 17 additions & 12 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::io::Read;

use serde::de;
use xml::reader::{EventReader, ParserConfig, XmlEvent};
use xml::name::OwnedName;
use xml::reader::{EventReader, ParserConfig, XmlEvent};

use error::{Error, ErrorKind, Result};
use self::map::MapAccess;
use self::seq::SeqAccess;
use self::var::EnumAccess;
use error::{Error, ErrorKind, Result};

mod map;
mod seq;
Expand Down Expand Up @@ -36,7 +36,6 @@ pub fn from_str<'de, T: de::Deserialize<'de>>(s: &str) -> Result<T> {
from_reader(s.as_bytes())
}


/// A convenience method for deserialize some object from a reader.
///
/// ```rust
Expand Down Expand Up @@ -71,7 +70,7 @@ impl<'de, R: Read> Deserializer<R> {
pub fn new(reader: EventReader<R>) -> Self {
Deserializer {
depth: 0,
reader: reader,
reader,
peeked: None,
is_map_value: false,
}
Expand Down Expand Up @@ -101,9 +100,9 @@ impl<'de, R: Read> Deserializer<R> {
fn inner_next(&mut self) -> Result<XmlEvent> {
loop {
match self.reader.next().map_err(ErrorKind::Syntax)? {
XmlEvent::StartDocument { .. } |
XmlEvent::ProcessingInstruction { .. } |
XmlEvent::Comment(_) => { /* skip */ },
XmlEvent::StartDocument { .. }
| XmlEvent::ProcessingInstruction { .. }
| XmlEvent::Comment(_) => { /* skip */ },
other => return Ok(other),
}
}
Expand Down Expand Up @@ -177,7 +176,7 @@ impl<'de, R: Read> Deserializer<R> {
}

expect!(this.next()?, XmlEvent::Characters(s) => {
return Ok(s)
Ok(s)
})
})
}
Expand Down Expand Up @@ -206,11 +205,17 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {
visitor: V,
) -> Result<V::Value> {
self.unset_map_value();
expect!(self.next()?, XmlEvent::StartElement { name, attributes, .. } => {
expect!(self.next()?, XmlEvent::StartElement { name, attributes, namespace, .. } => {
let namespace = if fields.contains(&"$xmlns") {
Some(namespace)
} else {
None
};
let map_value = visitor.visit_map(MapAccess::new(
self,
attributes,
fields.contains(&"$value")
namespace,
fields.contains(&"$value"),
))?;
self.expect_end_element(name)?;
Ok(map_value)
Expand Down Expand Up @@ -304,8 +309,8 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {

fn deserialize_map<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
self.unset_map_value();
expect!(self.next()?, XmlEvent::StartElement { name, attributes, .. } => {
let map_value = visitor.visit_map(MapAccess::new(self, attributes, false))?;
expect!(self.next()?, XmlEvent::StartElement { name, attributes, namespace } => {
let map_value = visitor.visit_map(MapAccess::new(self, attributes, None, false))?;
self.expect_end_element(name)?;
Ok(map_value)
})
Expand Down
12 changes: 6 additions & 6 deletions src/de/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ impl<'a, R: 'a + Read> SeqAccess<'a, R> {
None
};
SeqAccess {
de: de,
max_size: max_size,
expected_name: expected_name,
de,
max_size,
expected_name,
}
}
}
Expand All @@ -49,9 +49,9 @@ impl<'de, 'a, R: 'a + Read> de::SeqAccess<'de> for SeqAccess<'a, R> {
(&XmlEvent::StartElement { ref name, .. }, Some(expected_name)) => {
&name.local_name == expected_name
},
(&XmlEvent::EndElement { .. }, None) |
(_, Some(_)) |
(&XmlEvent::EndDocument { .. }, _) => false,
(&XmlEvent::EndElement { .. }, None)
| (_, Some(_))
| (&XmlEvent::EndDocument { .. }, _) => false,
(_, None) => true,
};
if more {
Expand Down
14 changes: 8 additions & 6 deletions src/de/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct EnumAccess<'a, R: 'a + Read> {

impl<'a, R: 'a + Read> EnumAccess<'a, R> {
pub fn new(de: &'a mut Deserializer<R>) -> Self {
EnumAccess { de: de }
EnumAccess { de }
}
}

Expand Down Expand Up @@ -44,7 +44,7 @@ pub struct VariantAccess<'a, R: 'a + Read> {

impl<'a, R: 'a + Read> VariantAccess<'a, R> {
pub fn new(de: &'a mut Deserializer<R>) -> Self {
VariantAccess { de: de }
VariantAccess { de }
}
}

Expand All @@ -56,10 +56,12 @@ impl<'de, 'a, R: 'a + Read> de::VariantAccess<'de> for VariantAccess<'a, R> {
match self.de.next()? {
XmlEvent::StartElement {
name, attributes, ..
} => if attributes.is_empty() {
self.de.expect_end_element(name)
} else {
Err(de::Error::invalid_length(attributes.len(), &"0"))
} => {
if attributes.is_empty() {
self.de.expect_end_element(name)
} else {
Err(de::Error::invalid_length(attributes.len(), &"0"))
}
},
XmlEvent::Characters(_) => Ok(()),
_ => unreachable!(),
Expand Down
40 changes: 11 additions & 29 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::io::Write;
use std::fmt::Display;
use std::io::Write;

use serde::ser::{self, Impossible, Serialize};

use error::{Error, ErrorKind, Result};
use self::var::{Map, Struct};
use error::{Error, ErrorKind, Result};

mod var;


/// A convenience method for serializing some object to a buffer.
///
/// # Examples
Expand Down Expand Up @@ -40,7 +39,6 @@ pub fn to_writer<W: Write, S: Serialize>(writer: W, value: &S) -> Result<()> {
value.serialize(&mut ser)
}


/// A convenience method for serializing some object to a string.
///
/// # Examples
Expand Down Expand Up @@ -87,7 +85,7 @@ where
W: Write,
{
pub fn new(writer: W) -> Self {
Self { writer: writer }
Self { writer }
}

fn write_primitive<P: Display>(&mut self, primitive: P) -> Result<()> {
Expand All @@ -103,7 +101,6 @@ where
}
}


#[allow(unused_variables)]
impl<'w, W> ser::Serializer for &'w mut Serializer<W>
where
Expand Down Expand Up @@ -181,9 +178,7 @@ where
fn serialize_bytes(self, value: &[u8]) -> Result<Self::Ok> {
// TODO: I imagine you'd want to use base64 here.
// Not sure how to roundtrip effectively though...
Err(
ErrorKind::UnsupportedOperation("serialize_bytes".to_string()).into(),
)
Err(ErrorKind::UnsupportedOperation("serialize_bytes".to_string()).into())
}

fn serialize_none(self) -> Result<Self::Ok> {
Expand All @@ -208,19 +203,15 @@ where
variant_index: u32,
variant: &'static str,
) -> Result<Self::Ok> {
Err(
ErrorKind::UnsupportedOperation("serialize_unit_variant".to_string()).into(),
)
Err(ErrorKind::UnsupportedOperation("serialize_unit_variant".to_string()).into())
}

fn serialize_newtype_struct<T: ?Sized + Serialize>(
self,
name: &'static str,
value: &T,
) -> Result<Self::Ok> {
Err(
ErrorKind::UnsupportedOperation("serialize_newtype_struct".to_string()).into(),
)
Err(ErrorKind::UnsupportedOperation("serialize_newtype_struct".to_string()).into())
}

fn serialize_newtype_variant<T: ?Sized + Serialize>(
Expand All @@ -235,25 +226,19 @@ where

fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
// TODO: Figure out how to constrain the things written to only be composites
Err(
ErrorKind::UnsupportedOperation("serialize_seq".to_string()).into(),
)
Err(ErrorKind::UnsupportedOperation("serialize_seq".to_string()).into())
}

fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
Err(
ErrorKind::UnsupportedOperation("serialize_tuple".to_string()).into(),
)
Err(ErrorKind::UnsupportedOperation("serialize_tuple".to_string()).into())
}

fn serialize_tuple_struct(
self,
name: &'static str,
len: usize,
) -> Result<Self::SerializeTupleStruct> {
Err(
ErrorKind::UnsupportedOperation("serialize_tuple_struct".to_string()).into(),
)
Err(ErrorKind::UnsupportedOperation("serialize_tuple_struct".to_string()).into())
}

fn serialize_tuple_variant(
Expand All @@ -263,9 +248,7 @@ where
variant: &'static str,
len: usize,
) -> Result<Self::SerializeTupleVariant> {
Err(
ErrorKind::UnsupportedOperation("serialize_tuple_variant".to_string()).into(),
)
Err(ErrorKind::UnsupportedOperation("serialize_tuple_variant".to_string()).into())
}

fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
Expand All @@ -288,12 +271,11 @@ where
}
}


#[cfg(test)]
mod tests {
use super::*;
use serde::Serializer as SerSerializer;
use serde::ser::{SerializeMap, SerializeStruct};
use serde::Serializer as SerSerializer;

#[test]
fn test_serialize_bool() {
Expand Down
Loading