Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Os serializer #45

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ description = "xml-rs based deserializer for Serde (compatible with 0.9+)"
license = "MIT"
name = "serde-xml-rs"
repository = "https://github.com/RReverser/serde-xml-rs"
version = "0.2.1"
version = "0.3.0"

[dependencies]
log = "0.3.6"
serde = "1.0"
xml-rs = "0.6.0"
error-chain = "0.10.0"
xmltree = "0.4.0"

[dev-dependencies]
serde_derive = "1.0"
14 changes: 9 additions & 5 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {
if let XmlEvent::StartElement { .. } = *self.peek()? {
self.set_map_value()
}
self.read_inner_value::<V, V::Value, _>(
|this| expect!(this.peek()?, &XmlEvent::EndElement { .. } => visitor.visit_unit()),
)
self.read_inner_value::<V, V::Value, _>(|this| {
expect!(this.peek()?, &XmlEvent::EndElement { .. } => visitor.visit_unit())
})
}

fn deserialize_unit_struct<V: de::Visitor<'de>>(
Expand All @@ -272,7 +272,7 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {
}

fn deserialize_tuple<V: de::Visitor<'de>>(self, len: usize, visitor: V) -> Result<V::Value> {
visitor.visit_seq(SeqAccess::new(self, Some(len)))
visitor.visit_seq(SeqAccess::new(self, Some(len))?)
}

fn deserialize_enum<V: de::Visitor<'de>>(
Expand All @@ -299,7 +299,11 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {
}

fn deserialize_seq<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
visitor.visit_seq(SeqAccess::new(self, None))
expect!(self.next(), Ok(XmlEvent::StartElement { name, .. }) => {
let result = visitor.visit_seq(SeqAccess::new(self, None)?);
self.expect_end_element(name)?;
result
})
}

fn deserialize_map<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
Expand Down
26 changes: 17 additions & 9 deletions src/de/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::de;
use xml::reader::XmlEvent;

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

pub struct SeqAccess<'a, R: 'a + Read> {
de: &'a mut Deserializer<R>,
Expand All @@ -13,19 +13,27 @@ pub struct SeqAccess<'a, R: 'a + Read> {
}

impl<'a, R: 'a + Read> SeqAccess<'a, R> {
pub fn new(de: &'a mut Deserializer<R>, max_size: Option<usize>) -> Self {
let expected_name = if de.unset_map_value() {
debug_expect!(de.peek(), Ok(&XmlEvent::StartElement { ref name, .. }) => {
Some(name.local_name.clone())
})
pub fn new(de: &'a mut Deserializer<R>, max_size: Option<usize>) -> Result<Self> {
let expected_name: Result<Option<String>> = if de.unset_map_value() {
match de.peek() {
Ok(&XmlEvent::StartElement { ref name, .. }) => Ok(Some(name.local_name.clone())),
Ok(&XmlEvent::EndElement { .. }) => Ok(None),
other => Err(
ErrorKind::Custom(format!(
"Expected StartElement or EndElement, found {:?}",
other
)).into(),
),
}
} else {
None
Ok(None)
};
SeqAccess {
let expected_name = expected_name?;
Ok(SeqAccess {
de: de,
max_size: max_size,
expected_name: expected_name,
}
})
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ error_chain! {
description("unsupported operation")
display("unsupported operation: '{}'", operation)
}
NonPrimitiveKey {
description("Map key has non-primitive value")
display("Map key has non-primitive value")
}
}
}

Expand Down
21 changes: 19 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
//! }
//!
//! fn main() {
//! let src = r#"<Item><name>Banana</name><source>Store</source></Item>"#;
//! let src = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
//! <Item><name>Banana</name><source>Store</source></Item>";
//! let should_be = Item {
//! name: "Banana".to_string(),
//! source: "Store".to_string(),
Expand Down Expand Up @@ -52,5 +53,21 @@ pub mod ser;

pub use error::{Error, ErrorKind};
pub use xml::reader::{EventReader, ParserConfig};
pub use ser::{to_string, to_writer, Serializer};
pub use ser::{to_string, to_string_with_namespaces, to_writer, Serializer};
pub use de::{from_reader, from_str, Deserializer};

use serde::ser::SerializeMap;

/// Helper function for serializing lists of primitives as <name>item<name>
pub fn wrap_primitives<T: serde::ser::Serialize, S: serde::ser::Serializer>(
items: &Vec<T>,
serializer: S,
name: &'static str,
) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(None)?;
for ref item in items {
map.serialize_key(name)?;
map.serialize_value(item)?;
}
map.end()
}
Loading