diff --git a/yaserde/src/de/mod.rs b/yaserde/src/de/mod.rs index 86bbbe6..bcb85d9 100644 --- a/yaserde/src/de/mod.rs +++ b/yaserde/src/de/mod.rs @@ -53,16 +53,18 @@ impl<'de, R: Read> Deserializer { pub fn inner_next(&mut self) -> Result { loop { - if let Ok(next) = self.reader.next() { - match next { - XmlEvent::StartDocument { .. } - | XmlEvent::ProcessingInstruction { .. } - | XmlEvent::Comment(_) => { /* skip */ } - other => return Ok(other), + match self.reader.next() { + Ok(next) => { + match next { + XmlEvent::StartDocument { .. } + | XmlEvent::ProcessingInstruction { .. } + | XmlEvent::Comment(_) => { /* skip */ } + other => return Ok(other), + } + } + Err(msg) => { + return Err(msg.msg().to_string()); } - } else { - println!("{:?}", self.peeked); - return Err(String::from("bad content")); } } } diff --git a/yaserde/tests/errors.rs b/yaserde/tests/errors.rs new file mode 100644 index 0000000..e4253e3 --- /dev/null +++ b/yaserde/tests/errors.rs @@ -0,0 +1,38 @@ +#[macro_use] +extern crate log; +extern crate xml; +extern crate yaserde; +#[macro_use] +extern crate yaserde_derive; + +use std::io::Read; +use yaserde::YaDeserialize; +use yaserde::de::from_str; + +#[test] +fn de_no_content() { + #[derive(YaDeserialize, PartialEq, Debug)] + #[yaserde(root = "book")] + pub struct Book { + author: String, + title: String, + } + + let content = ""; + let loaded: Result = from_str(content); + assert_eq!(loaded, Err(String::from("Unexpected end of stream: no root element found"))); +} + +#[test] +fn de_wrong_end_balise() { + #[derive(YaDeserialize, PartialEq, Debug)] + #[yaserde(root = "book")] + pub struct Book { + author: String, + title: String, + } + + let content = "Antoine de Saint-ExupéryLittle prince"; + let loaded: Result = from_str(content); + assert_eq!(loaded, Err(String::from("Unexpected closing tag: book, expected author"))); +}