From f85b451fcf5dbcab6bd4c53b7ca93344a69f5e80 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 31 Oct 2017 15:47:38 +0000 Subject: [PATCH] Add a new check_no_attributes macro, to avoid the empty list. --- src/chatstates.rs | 2 +- src/data_forms.rs | 8 ++++---- src/lib.rs | 8 +++++++- src/pubsub/event.rs | 2 +- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/chatstates.rs b/src/chatstates.rs index 995cd11e..e226f21d 100644 --- a/src/chatstates.rs +++ b/src/chatstates.rs @@ -40,7 +40,7 @@ impl TryFrom for ChatState { fn try_from(elem: Element) -> Result { check_ns_only!(elem, "chatstate", ns::CHATSTATES); check_no_children!(elem, "chatstate"); - check_no_unknown_attributes!(elem, "chatstate", []); + check_no_attributes!(elem, "chatstate"); Ok(match elem.name() { "active" => ChatState::Active, "composing" => ChatState::Composing, diff --git a/src/data_forms.rs b/src/data_forms.rs index e3a68f46..a54be9c2 100644 --- a/src/data_forms.rs +++ b/src/data_forms.rs @@ -105,14 +105,14 @@ impl TryFrom for Field { for element in elem.children() { if element.is("value", ns::DATA_FORMS) { check_no_children!(element, "value"); - check_no_unknown_attributes!(element, "value", []); + check_no_attributes!(element, "value"); field.values.push(element.text()); } else if element.is("required", ns::DATA_FORMS) { if field.required { return Err(Error::ParseError("More than one required element.")); } check_no_children!(element, "required"); - check_no_unknown_attributes!(element, "required", []); + check_no_attributes!(element, "required"); field.required = true; } else if element.is("option", ns::DATA_FORMS) { if !field.is_list() { @@ -184,14 +184,14 @@ impl TryFrom for DataForm { return Err(Error::ParseError("More than one title in form element.")); } check_no_children!(child, "title"); - check_no_unknown_attributes!(child, "title", []); + check_no_attributes!(child, "title"); form.title = Some(child.text()); } else if child.is("instructions", ns::DATA_FORMS) { if form.instructions.is_some() { return Err(Error::ParseError("More than one instructions in form element.")); } check_no_children!(child, "instructions"); - check_no_unknown_attributes!(child, "instructions", []); + check_no_attributes!(child, "instructions"); form.instructions = Some(child.text()); } else if child.is("field", ns::DATA_FORMS) { let field = Field::try_from(child.clone())?; diff --git a/src/lib.rs b/src/lib.rs index 5b52da05..fee722cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -154,6 +154,12 @@ macro_rules! check_no_children { ); } +macro_rules! check_no_attributes { + ($elem:ident, $name:tt) => ( + check_no_unknown_attributes!($elem, $name, []); + ); +} + macro_rules! check_no_unknown_attributes { ($elem:ident, $name:tt, [$($attr:tt),*]) => ( for (_attr, _) in $elem.attrs() { @@ -182,7 +188,7 @@ macro_rules! generate_empty_element { fn try_from(elem: Element) -> Result<$elem, Error> { check_self!(elem, $name, $ns); check_no_children!(elem, $name); - check_no_unknown_attributes!(elem, $name, []); + check_no_attributes!(elem, $name); Ok($elem) } } diff --git a/src/pubsub/event.rs b/src/pubsub/event.rs index 9f8b1e4c..74b59ae6 100644 --- a/src/pubsub/event.rs +++ b/src/pubsub/event.rs @@ -143,7 +143,7 @@ impl TryFrom for PubSubEvent { fn try_from(elem: Element) -> Result { check_self!(elem, "event", ns::PUBSUB_EVENT); - check_no_unknown_attributes!(elem, "event", []); + check_no_attributes!(elem, "event"); let mut payload = None; for child in elem.children() {