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

Add support for parsing bools, #18

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ use xml::reader;
use std::fmt::{self, Display, Debug};
use std::error::Error as StdError;
use std::num;
use std::str;
use serde::de::Error as SerdeError;

pub enum Error {
ParseBoolError(str::ParseBoolError),
ParseIntError(num::ParseIntError),
Syntax(reader::Error),
Custom(String)
Expand Down Expand Up @@ -44,6 +46,7 @@ macro_rules! debug_expect {
impl Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::ParseBoolError(ref error) => Display::fmt(error, fmt),
Error::ParseIntError(ref error) => Display::fmt(error, fmt),
Error::Syntax(ref error) => Display::fmt(error, fmt),
Error::Custom(ref display) => Display::fmt(display, fmt)
Expand All @@ -54,6 +57,7 @@ impl Display for Error {
impl Debug for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::ParseBoolError(ref error) => Display::fmt(error, fmt),
Error::ParseIntError(ref error) => Display::fmt(error, fmt),
Error::Syntax(ref error) => Debug::fmt(error, fmt),
Error::Custom(ref display) => Display::fmt(display, fmt)
Expand All @@ -64,6 +68,7 @@ impl Debug for Error {
impl StdError for Error {
fn description(&self) -> &str {
match *self {
Error::ParseBoolError(ref error) => error.description(),
Error::ParseIntError(ref error) => error.description(),
Error::Syntax(ref error) => error.description(),
Error::Custom(_) => "other error"
Expand Down
19 changes: 18 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,23 @@ impl<'de, R: Read> Deserializer<R> {
})
})
}

fn parse_bool<N, V, F>(&mut self, visit: F) -> VResult<V::Value>
where N: std::str::FromStr<Err=std::str::ParseBoolError>,
V: Visitor<'de>,
F: FnOnce(N) -> VResult<V::Value>,
{
self.read_inner_value::<V, _>(|this| {
if let XmlEvent::EndElement { .. } = *this.peek()? {
return Err(Error::Custom(format!("expected a boolean")));
}

expect!(this.next()?, XmlEvent::Characters(s) => {
let value = s.parse::<N>().map_err(Error::ParseBoolError)?;
visit(value)
})
})
}
}

impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {
Expand Down Expand Up @@ -198,7 +215,7 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {
}

fn deserialize_bool<V: Visitor<'de>>(self, visitor: V) -> VResult<V::Value> {
self.deserialize_string(visitor)
self.parse_bool::<bool, V, _>(|value| visitor.visit_bool(value))
}

fn deserialize_char<V: Visitor<'de>>(self, visitor: V) -> VResult<V::Value> {
Expand Down
3 changes: 2 additions & 1 deletion src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ impl<'de> de::Deserializer<'de> for AttrValueDeserializer {
}

fn deserialize_bool<V: Visitor<'de>>(self, visitor: V) -> VResult<V::Value> {
visitor.visit_bool(!self.0.is_empty())
let b = self.0.parse().map_err(Error::ParseBoolError)?;
visitor.visit_bool(b)
}

forward_to_deserialize_any! {
Expand Down
24 changes: 14 additions & 10 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ use serde_xml_rs::deserialize;
#[derive(Debug, Deserialize, PartialEq)]
struct Item {
name: String,
source: String
source: String,
active: bool,
}

#[test]
fn simple_struct_from_attributes() {
let s = r##"
<item name="hello" source="world.rs" />
<item name="hello" source="world.rs" active="true" />
"##;

let item: Item = deserialize(s.as_bytes()).unwrap();

assert_eq!(item, Item {
name: "hello".to_string(),
source: "world.rs".to_string()
source: "world.rs".to_string(),
active: true,
});
}

Expand All @@ -28,14 +30,16 @@ fn simple_struct_from_attribute_and_child() {
let s = r##"
<item name="hello">
<source>world.rs</source>
<active>false</active>
</item>
"##;

let item: Item = deserialize(s.as_bytes()).unwrap();

assert_eq!(item, Item {
name: "hello".to_string(),
source: "world.rs".to_string()
source: "world.rs".to_string(),
active: false,
});
}

Expand All @@ -51,8 +55,8 @@ struct Project {
fn nested_collection() {
let s = r##"
<project name="my_project">
<item name="hello1" source="world1.rs" />
<item name="hello2" source="world2.rs" />
<item name="hello1" source="world1.rs" active="true" />
<item name="hello2" source="world2.rs" active="false" />
</project>
"##;

Expand All @@ -61,8 +65,8 @@ fn nested_collection() {
assert_eq!(project, Project {
name: "my_project".to_string(),
items: vec![
Item { name: "hello1".to_string(), source: "world1.rs".to_string() },
Item { name: "hello2".to_string(), source: "world2.rs".to_string() }
Item { name: "hello1".to_string(), source: "world1.rs".to_string(), active: true },
Item { name: "hello2".to_string(), source: "world2.rs".to_string(), active: false }
]
});
}
Expand All @@ -85,7 +89,7 @@ fn collection_of_enums() {
let s = r##"
<enums>
<A>test</A>
<B name="hello" flag="t" />
<B name="hello" flag="true" />
<C />
</enums>
"##;
Expand All @@ -99,4 +103,4 @@ fn collection_of_enums() {
MyEnum::C
]
});
}
}