Replace assert!()s with proper errors in parsers.

Emmanuel Gil Peyrot created

Change summary

src/body.rs          | 2 +-
src/chatstates.rs    | 2 +-
src/data_forms.rs    | 5 ++++-
src/disco.rs         | 5 ++++-
src/ibb.rs           | 2 +-
src/jingle.rs        | 5 ++++-
src/media_element.rs | 5 ++++-
src/ping.rs          | 5 ++++-
8 files changed, 23 insertions(+), 8 deletions(-)

Detailed changes

src/body.rs 🔗

@@ -15,7 +15,7 @@ impl MessagePayload for Body {}
 
 pub fn parse_body(root: &Element) -> Result<Body, Error> {
     if !root.is("body", JABBER_CLIENT_NS) {
-        return Err(Error::ParseError("Not a body element."));
+        return Err(Error::ParseError("This is not a body element."));
     }
     for _ in root.children() {
         return Err(Error::ParseError("Unknown child in body element."));

src/chatstates.rs 🔗

@@ -31,7 +31,7 @@ pub fn parse_chatstate(root: &Element) -> Result<ChatState, Error> {
     } else if root.is("paused", CHATSTATES_NS) {
         Ok(ChatState::Paused)
     } else {
-        Err(Error::ParseError("Unknown chatstate element."))
+        Err(Error::ParseError("This is not a chatstate element."))
     }
 }
 

src/data_forms.rs 🔗

@@ -52,7 +52,10 @@ pub struct DataForm {
 }
 
 pub fn parse_data_form(root: &Element) -> Result<DataForm, Error> {
-    assert!(root.is("x", DATA_FORMS_NS));
+    if !root.is("x", DATA_FORMS_NS) {
+        return Err(Error::ParseError("This is not a data form element.")),
+    }
+
     let type_: DataFormType = match root.attr("type") {
         Some(type_) => type_.parse()?,
         None => return Err(Error::ParseError("Type attribute on data form is mandatory.")),

src/disco.rs 🔗

@@ -29,7 +29,10 @@ pub struct Disco {
 }
 
 pub fn parse_disco(root: &Element) -> Result<Disco, Error> {
-    assert!(root.is("query", DISCO_INFO_NS));
+    if !root.is("query", DISCO_INFO_NS) {
+        return Err(Error::ParseError("This is not a disco#info element.")),
+    }
+
     let mut identities: Vec<Identity> = vec!();
     let mut features: Vec<Feature> = vec!();
     let mut extensions: Vec<DataForm> = vec!();

src/ibb.rs 🔗

@@ -64,7 +64,7 @@ pub fn parse_ibb(root: &Element) -> Result<IBB, Error> {
             stanza: stanza
         })
     } else {
-        Err(Error::ParseError("Unknown ibb element."))
+        Err(Error::ParseError("This is not an ibb element."))
     }
 }
 

src/jingle.rs 🔗

@@ -209,7 +209,10 @@ pub struct Jingle {
 }
 
 pub fn parse_jingle(root: &Element) -> Result<Jingle, Error> {
-    assert!(root.is("jingle", JINGLE_NS));
+    if !root.is("jingle", JINGLE_NS) {
+        return Err(Error::ParseError("This is not a Jingle element.")),
+    }
+
     let mut contents: Vec<Content> = vec!();
 
     let action = root.attr("action")

src/media_element.rs 🔗

@@ -18,7 +18,10 @@ pub struct MediaElement {
 }
 
 pub fn parse_media_element(root: &Element) -> Result<MediaElement, Error> {
-    assert!(root.is("media", MEDIA_ELEMENT_NS));
+    if !root.is("media", MEDIA_ELEMENT_NS) {
+        return Err(Error::ParseError("This is not a media element.")),
+    }
+
     let width = root.attr("width").and_then(|width| width.parse().ok());
     let height = root.attr("height").and_then(|height| height.parse().ok());
     let mut uris = vec!();

src/ping.rs 🔗

@@ -9,7 +9,10 @@ pub struct Ping {
 }
 
 pub fn parse_ping(root: &Element) -> Result<Ping, Error> {
-    assert!(root.is("ping", PING_NS));
+    if !root.is("ping", PING_NS) {
+        return Err(Error::ParseError("This is not a ping element.")),
+    }
+
     for _ in root.children() {
         return Err(Error::ParseError("Unknown child in ping element."));
     }