Add a new check_no_attributes macro, to avoid the empty list.

This commit is contained in:
Emmanuel Gil Peyrot 2017-10-31 15:47:38 +00:00
parent 1892e1ca04
commit f85b451fcf
4 changed files with 13 additions and 7 deletions

View file

@ -40,7 +40,7 @@ impl TryFrom<Element> for ChatState {
fn try_from(elem: Element) -> Result<ChatState, Error> { fn try_from(elem: Element) -> Result<ChatState, Error> {
check_ns_only!(elem, "chatstate", ns::CHATSTATES); check_ns_only!(elem, "chatstate", ns::CHATSTATES);
check_no_children!(elem, "chatstate"); check_no_children!(elem, "chatstate");
check_no_unknown_attributes!(elem, "chatstate", []); check_no_attributes!(elem, "chatstate");
Ok(match elem.name() { Ok(match elem.name() {
"active" => ChatState::Active, "active" => ChatState::Active,
"composing" => ChatState::Composing, "composing" => ChatState::Composing,

View file

@ -105,14 +105,14 @@ impl TryFrom<Element> for Field {
for element in elem.children() { for element in elem.children() {
if element.is("value", ns::DATA_FORMS) { if element.is("value", ns::DATA_FORMS) {
check_no_children!(element, "value"); check_no_children!(element, "value");
check_no_unknown_attributes!(element, "value", []); check_no_attributes!(element, "value");
field.values.push(element.text()); field.values.push(element.text());
} else if element.is("required", ns::DATA_FORMS) { } else if element.is("required", ns::DATA_FORMS) {
if field.required { if field.required {
return Err(Error::ParseError("More than one required element.")); return Err(Error::ParseError("More than one required element."));
} }
check_no_children!(element, "required"); check_no_children!(element, "required");
check_no_unknown_attributes!(element, "required", []); check_no_attributes!(element, "required");
field.required = true; field.required = true;
} else if element.is("option", ns::DATA_FORMS) { } else if element.is("option", ns::DATA_FORMS) {
if !field.is_list() { if !field.is_list() {
@ -184,14 +184,14 @@ impl TryFrom<Element> for DataForm {
return Err(Error::ParseError("More than one title in form element.")); return Err(Error::ParseError("More than one title in form element."));
} }
check_no_children!(child, "title"); check_no_children!(child, "title");
check_no_unknown_attributes!(child, "title", []); check_no_attributes!(child, "title");
form.title = Some(child.text()); form.title = Some(child.text());
} else if child.is("instructions", ns::DATA_FORMS) { } else if child.is("instructions", ns::DATA_FORMS) {
if form.instructions.is_some() { if form.instructions.is_some() {
return Err(Error::ParseError("More than one instructions in form element.")); return Err(Error::ParseError("More than one instructions in form element."));
} }
check_no_children!(child, "instructions"); check_no_children!(child, "instructions");
check_no_unknown_attributes!(child, "instructions", []); check_no_attributes!(child, "instructions");
form.instructions = Some(child.text()); form.instructions = Some(child.text());
} else if child.is("field", ns::DATA_FORMS) { } else if child.is("field", ns::DATA_FORMS) {
let field = Field::try_from(child.clone())?; let field = Field::try_from(child.clone())?;

View file

@ -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 { macro_rules! check_no_unknown_attributes {
($elem:ident, $name:tt, [$($attr:tt),*]) => ( ($elem:ident, $name:tt, [$($attr:tt),*]) => (
for (_attr, _) in $elem.attrs() { for (_attr, _) in $elem.attrs() {
@ -182,7 +188,7 @@ macro_rules! generate_empty_element {
fn try_from(elem: Element) -> Result<$elem, Error> { fn try_from(elem: Element) -> Result<$elem, Error> {
check_self!(elem, $name, $ns); check_self!(elem, $name, $ns);
check_no_children!(elem, $name); check_no_children!(elem, $name);
check_no_unknown_attributes!(elem, $name, []); check_no_attributes!(elem, $name);
Ok($elem) Ok($elem)
} }
} }

View file

@ -143,7 +143,7 @@ impl TryFrom<Element> for PubSubEvent {
fn try_from(elem: Element) -> Result<PubSubEvent, Error> { fn try_from(elem: Element) -> Result<PubSubEvent, Error> {
check_self!(elem, "event", ns::PUBSUB_EVENT); check_self!(elem, "event", ns::PUBSUB_EVENT);
check_no_unknown_attributes!(elem, "event", []); check_no_attributes!(elem, "event");
let mut payload = None; let mut payload = None;
for child in elem.children() { for child in elem.children() {