Use check_no_children!() where it makes sense.

This commit is contained in:
Emmanuel Gil Peyrot 2018-05-14 16:33:47 +02:00
parent 93b018e5ac
commit 3d495ccf41
6 changed files with 13 additions and 37 deletions

View file

@ -33,9 +33,7 @@ fn get_sid(elem: Element) -> Result<SessionId, Error> {
}
fn check_empty_and_get_sid(elem: Element) -> Result<SessionId, Error> {
for _ in elem.children() {
return Err(Error::ParseError("Unknown child in Jingle message element."));
}
check_no_children!(elem, "Jingle message");
get_sid(elem)
}

View file

@ -199,18 +199,14 @@ impl TryFrom<Element> 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<Element> 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())

View file

@ -94,9 +94,7 @@ impl TryFrom<Element> for Actor {
fn try_from(elem: Element) -> Result<Actor, Error> {
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<Jid> = get_attr!(elem, "jid", optional);
let nick = get_attr!(elem, "nick", optional);

View file

@ -267,15 +267,11 @@ impl TryFrom<Element> 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<Element> 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());

View file

@ -175,14 +175,10 @@ impl TryFrom<Element> 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),

View file

@ -71,9 +71,7 @@ impl TryFrom<Element> 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<Element> 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 {