chatstates: Use the new helper macros to simplify parsing.

Emmanuel Gil Peyrot created

Change summary

src/chatstates.rs | 12 +++---------
src/lib.rs        |  8 ++++++++
2 files changed, 11 insertions(+), 9 deletions(-)

Detailed changes

src/chatstates.rs 🔗

@@ -38,15 +38,9 @@ impl TryFrom<Element> for ChatState {
     type Err = Error;
 
     fn try_from(elem: Element) -> Result<ChatState, Error> {
-        if !elem.has_ns(ns::CHATSTATES) {
-            return Err(Error::ParseError("This is not a chatstate element."));
-        }
-        for _ in elem.children() {
-            return Err(Error::ParseError("Unknown child in chatstate element."));
-        }
-        for _ in elem.attrs() {
-            return Err(Error::ParseError("Unknown attribute in chatstate element."));
-        }
+        check_ns_only!(elem, "chatstate", ns::CHATSTATES);
+        check_no_children!(elem, "chatstate");
+        check_no_unknown_attributes!(elem, "chatstate", []);
         Ok(match elem.name() {
             "active" => ChatState::Active,
             "composing" => ChatState::Composing,

src/lib.rs 🔗

@@ -138,6 +138,14 @@ macro_rules! check_self {
     );
 }
 
+macro_rules! check_ns_only {
+    ($elem:ident, $name:tt, $ns:expr) => (
+        if !$elem.has_ns($ns) {
+            return Err(Error::ParseError(concat!("This is not a ", $name, " element.")));
+        }
+    );
+}
+
 macro_rules! check_no_children {
     ($elem:ident, $name:tt) => (
         for _ in $elem.children() {