From ee243c47207c95199f80ff5232b61651b2c77951 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 10 Oct 2017 18:09:58 +0100 Subject: [PATCH] chatstates: Use the new helper macros to simplify parsing. --- src/chatstates.rs | 12 +++--------- src/lib.rs | 8 ++++++++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/chatstates.rs b/src/chatstates.rs index ecbecdff..995cd11e 100644 --- a/src/chatstates.rs +++ b/src/chatstates.rs @@ -38,15 +38,9 @@ impl TryFrom for ChatState { type Err = Error; fn try_from(elem: Element) -> Result { - 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, diff --git a/src/lib.rs b/src/lib.rs index 9f3b80bb..5b52da05 100644 --- a/src/lib.rs +++ b/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() {