From 3d495ccf416c8b61a3ee26a1af3aaa5f7c5ed75d Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Mon, 14 May 2018 16:33:47 +0200 Subject: [PATCH] Use check_no_children!() where it makes sense. --- src/jingle_message.rs | 4 +--- src/message.rs | 12 +++--------- src/muc/user.rs | 4 +--- src/presence.rs | 14 ++++---------- src/pubsub/event.rs | 8 ++------ src/stanza_error.rs | 8 ++------ 6 files changed, 13 insertions(+), 37 deletions(-) diff --git a/src/jingle_message.rs b/src/jingle_message.rs index 159ce597..268d93a4 100644 --- a/src/jingle_message.rs +++ b/src/jingle_message.rs @@ -33,9 +33,7 @@ fn get_sid(elem: Element) -> Result { } fn check_empty_and_get_sid(elem: Element) -> Result { - for _ in elem.children() { - return Err(Error::ParseError("Unknown child in Jingle message element.")); - } + check_no_children!(elem, "Jingle message"); get_sid(elem) } diff --git a/src/message.rs b/src/message.rs index 2fffdb90..f103ba8e 100644 --- a/src/message.rs +++ b/src/message.rs @@ -199,18 +199,14 @@ impl TryFrom for Message { let mut payloads = vec!(); for elem in root.children() { if elem.is("body", ns::DEFAULT_NS) { - for _ in elem.children() { - return Err(Error::ParseError("Unknown child in body element.")); - } + check_no_children!(elem, "body"); let lang = get_attr!(elem, "xml:lang", default); let body = Body(elem.text()); if bodies.insert(lang, body).is_some() { return Err(Error::ParseError("Body element present twice for the same xml:lang.")); } } else if elem.is("subject", ns::DEFAULT_NS) { - for _ in elem.children() { - return Err(Error::ParseError("Unknown child in subject element.")); - } + check_no_children!(elem, "subject"); let lang = get_attr!(elem, "xml:lang", default); let subject = Subject(elem.text()); if subjects.insert(lang, subject).is_some() { @@ -220,9 +216,7 @@ impl TryFrom for Message { if thread.is_some() { return Err(Error::ParseError("Thread element present twice.")); } - for _ in elem.children() { - return Err(Error::ParseError("Unknown child in thread element.")); - } + check_no_children!(elem, "thread"); thread = Some(Thread(elem.text())); } else { payloads.push(elem.clone()) diff --git a/src/muc/user.rs b/src/muc/user.rs index b1390dd6..f53799cd 100644 --- a/src/muc/user.rs +++ b/src/muc/user.rs @@ -94,9 +94,7 @@ impl TryFrom for Actor { fn try_from(elem: Element) -> Result { check_self!(elem, "actor", MUC_USER); check_no_unknown_attributes!(elem, "actor", ["jid", "nick"]); - for _ in elem.children() { - return Err(Error::ParseError("Unknown child in actor element.")); - } + check_no_children!(elem, "actor"); let jid: Option = get_attr!(elem, "jid", optional); let nick = get_attr!(elem, "nick", optional); diff --git a/src/presence.rs b/src/presence.rs index ecb58754..9eba1987 100644 --- a/src/presence.rs +++ b/src/presence.rs @@ -267,15 +267,11 @@ impl TryFrom for Presence { return Err(Error::ParseError("More than one show element in a presence.")); } check_no_attributes!(elem, "show"); - for _ in elem.children() { - return Err(Error::ParseError("Unknown child in show element.")); - } + check_no_children!(elem, "show"); show = Some(Show::from_str(elem.text().as_ref())?); } else if elem.is("status", ns::DEFAULT_NS) { check_no_unknown_attributes!(elem, "status", ["xml:lang"]); - for _ in elem.children() { - return Err(Error::ParseError("Unknown child in status element.")); - } + check_no_children!(elem, "status"); let lang = get_attr!(elem, "xml:lang", default); if presence.statuses.insert(lang, elem.text()).is_some() { return Err(Error::ParseError("Status element present twice for the same xml:lang.")); @@ -284,10 +280,8 @@ impl TryFrom for Presence { if priority.is_some() { return Err(Error::ParseError("More than one priority element in a presence.")); } - check_no_attributes!(elem, "status"); - for _ in elem.children() { - return Err(Error::ParseError("Unknown child in priority element.")); - } + check_no_attributes!(elem, "priority"); + check_no_children!(elem, "priority"); priority = Some(Priority::from_str(elem.text().as_ref())?); } else { presence.payloads.push(elem.clone()); diff --git a/src/pubsub/event.rs b/src/pubsub/event.rs index bd7a9351..8ac16caf 100644 --- a/src/pubsub/event.rs +++ b/src/pubsub/event.rs @@ -175,14 +175,10 @@ impl TryFrom for PubSubEvent { } else if child.is("items", ns::PUBSUB_EVENT) { payload = Some(parse_items(child.clone(), node)?); } else if child.is("purge", ns::PUBSUB_EVENT) { - for _ in child.children() { - return Err(Error::ParseError("Unknown child in purge element.")); - } + check_no_children!(child, "purge"); payload = Some(PubSubEvent::Purge { node }); } else if child.is("subscription", ns::PUBSUB_EVENT) { - for _ in child.children() { - return Err(Error::ParseError("Unknown child in purge element.")); - } + check_no_children!(child, "subscription"); payload = Some(PubSubEvent::Subscription { node: node, expiry: get_attr!(child, "expiry", optional), diff --git a/src/stanza_error.rs b/src/stanza_error.rs index f2501498..46496c91 100644 --- a/src/stanza_error.rs +++ b/src/stanza_error.rs @@ -71,9 +71,7 @@ impl TryFrom for StanzaError { for child in elem.children() { if child.is("text", ns::XMPP_STANZAS) { - for _ in child.children() { - return Err(Error::ParseError("Unknown element in error text.")); - } + check_no_children!(child, "text"); let lang = get_attr!(elem, "xml:lang", default); if texts.insert(lang, child.text()).is_some() { return Err(Error::ParseError("Text element present twice for the same xml:lang.")); @@ -82,9 +80,7 @@ impl TryFrom for StanzaError { if defined_condition.is_some() { return Err(Error::ParseError("Error must not have more than one defined-condition.")); } - for _ in child.children() { - return Err(Error::ParseError("Unknown element in defined-condition.")); - } + check_no_children!(child, "defined-condition"); let condition = DefinedCondition::try_from(child.clone())?; defined_condition = Some(condition); } else {