chatstates: Use the new helper macros to simplify parsing.

This commit is contained in:
Emmanuel Gil Peyrot 2017-10-10 18:09:58 +01:00
parent a2b6033336
commit ee243c4720
2 changed files with 11 additions and 9 deletions

View file

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

View file

@ -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 { macro_rules! check_no_children {
($elem:ident, $name:tt) => ( ($elem:ident, $name:tt) => (
for _ in $elem.children() { for _ in $elem.children() {