diff --git a/src/forwarding.rs b/src/forwarding.rs index 2348068b..8ab4ead2 100644 --- a/src/forwarding.rs +++ b/src/forwarding.rs @@ -26,9 +26,7 @@ impl TryFrom for Forwarded { type Err = Error; fn try_from(elem: Element) -> Result { - if !elem.is("forwarded", ns::FORWARD) { - return Err(Error::ParseError("This is not a forwarded element.")); - } + check_self!(elem, "forwarded", ns::FORWARD); let mut delay = None; let mut stanza = None; for child in elem.children() { diff --git a/src/ibr.rs b/src/ibr.rs index 05281f33..892c67d9 100644 --- a/src/ibr.rs +++ b/src/ibr.rs @@ -29,9 +29,7 @@ impl TryFrom for Query { type Err = Error; fn try_from(elem: Element) -> Result { - if !elem.is("query", ns::REGISTER) { - return Err(Error::ParseError("This is not an ibr element.")); - } + check_self!(elem, "query", ns::REGISTER, "IBR query"); let mut query = Query { registered: false, fields: HashMap::new(), diff --git a/src/iq.rs b/src/iq.rs index 65c13a24..9a7f3b59 100644 --- a/src/iq.rs +++ b/src/iq.rs @@ -207,9 +207,7 @@ impl TryFrom for Iq { type Err = Error; fn try_from(root: Element) -> Result { - if !root.is("iq", ns::DEFAULT_NS) { - return Err(Error::ParseError("This is not an iq element.")); - } + check_self!(root, "iq", ns::DEFAULT_NS); let from = get_attr!(root, "from", optional); let to = get_attr!(root, "to", optional); let id = get_attr!(root, "id", optional); diff --git a/src/jingle.rs b/src/jingle.rs index ae313766..08002bcf 100644 --- a/src/jingle.rs +++ b/src/jingle.rs @@ -119,9 +119,7 @@ impl TryFrom for Content { type Err = Error; fn try_from(elem: Element) -> Result { - if !elem.is("content", ns::JINGLE) { - return Err(Error::ParseError("This is not a content element.")); - } + check_self!(elem, "content", ns::JINGLE); let mut content = Content { creator: get_attr!(elem, "creator", required), @@ -252,9 +250,7 @@ impl TryFrom for ReasonElement { type Err = Error; fn try_from(elem: Element) -> Result { - if !elem.is("reason", ns::JINGLE) { - return Err(Error::ParseError("This is not a reason element.")); - } + check_self!(elem, "reason", ns::JINGLE); let mut reason = None; let mut text = None; for child in elem.children() { @@ -344,9 +340,7 @@ impl TryFrom for Jingle { type Err = Error; fn try_from(root: Element) -> Result { - if !root.is("jingle", ns::JINGLE) { - return Err(Error::ParseError("This is not a Jingle element.")); - } + check_self!(root, "jingle", ns::JINGLE, "Jingle"); let mut jingle = Jingle { action: get_attr!(root, "action", required), diff --git a/src/jingle_s5b.rs b/src/jingle_s5b.rs index c670124d..558eb560 100644 --- a/src/jingle_s5b.rs +++ b/src/jingle_s5b.rs @@ -111,57 +111,54 @@ impl TryFrom for Transport { type Err = Error; fn try_from(elem: Element) -> Result { - if elem.is("transport", ns::JINGLE_S5B) { - let sid = get_attr!(elem, "sid", required); - let dstaddr = get_attr!(elem, "dstaddr", optional); - let mode = get_attr!(elem, "mode", default); + check_self!(elem, "transport", ns::JINGLE_S5B); + let sid = get_attr!(elem, "sid", required); + let dstaddr = get_attr!(elem, "dstaddr", optional); + let mode = get_attr!(elem, "mode", default); - let mut payload = None; - for child in elem.children() { - payload = Some(if child.is("candidate", ns::JINGLE_S5B) { - let mut candidates = match payload { - Some(TransportPayload::Candidates(candidates)) => candidates, - Some(_) => return Err(Error::ParseError("Non-candidate child already present in JingleS5B transport element.")), - None => vec!(), - }; - candidates.push(Candidate::try_from(child.clone())?); - TransportPayload::Candidates(candidates) - } else if child.is("activated", ns::JINGLE_S5B) { - if payload.is_some() { - return Err(Error::ParseError("Non-activated child already present in JingleS5B transport element.")); - } - let cid = get_attr!(child, "cid", required); - TransportPayload::Activated(cid) - } else if child.is("candidate-error", ns::JINGLE_S5B) { - if payload.is_some() { - return Err(Error::ParseError("Non-candidate-error child already present in JingleS5B transport element.")); - } - TransportPayload::CandidateError - } else if child.is("candidate-used", ns::JINGLE_S5B) { - if payload.is_some() { - return Err(Error::ParseError("Non-candidate-used child already present in JingleS5B transport element.")); - } - let cid = get_attr!(child, "cid", required); - TransportPayload::CandidateUsed(cid) - } else if child.is("proxy-error", ns::JINGLE_S5B) { - if payload.is_some() { - return Err(Error::ParseError("Non-proxy-error child already present in JingleS5B transport element.")); - } - TransportPayload::ProxyError - } else { - return Err(Error::ParseError("Unknown child in JingleS5B transport element.")); - }); - } - let payload = payload.unwrap_or(TransportPayload::None); - Ok(Transport { - sid: sid, - dstaddr: dstaddr, - mode: mode, - payload: payload, - }) - } else { - Err(Error::ParseError("This is not an JingleS5B transport element.")) + let mut payload = None; + for child in elem.children() { + payload = Some(if child.is("candidate", ns::JINGLE_S5B) { + let mut candidates = match payload { + Some(TransportPayload::Candidates(candidates)) => candidates, + Some(_) => return Err(Error::ParseError("Non-candidate child already present in JingleS5B transport element.")), + None => vec!(), + }; + candidates.push(Candidate::try_from(child.clone())?); + TransportPayload::Candidates(candidates) + } else if child.is("activated", ns::JINGLE_S5B) { + if payload.is_some() { + return Err(Error::ParseError("Non-activated child already present in JingleS5B transport element.")); + } + let cid = get_attr!(child, "cid", required); + TransportPayload::Activated(cid) + } else if child.is("candidate-error", ns::JINGLE_S5B) { + if payload.is_some() { + return Err(Error::ParseError("Non-candidate-error child already present in JingleS5B transport element.")); + } + TransportPayload::CandidateError + } else if child.is("candidate-used", ns::JINGLE_S5B) { + if payload.is_some() { + return Err(Error::ParseError("Non-candidate-used child already present in JingleS5B transport element.")); + } + let cid = get_attr!(child, "cid", required); + TransportPayload::CandidateUsed(cid) + } else if child.is("proxy-error", ns::JINGLE_S5B) { + if payload.is_some() { + return Err(Error::ParseError("Non-proxy-error child already present in JingleS5B transport element.")); + } + TransportPayload::ProxyError + } else { + return Err(Error::ParseError("Unknown child in JingleS5B transport element.")); + }); } + let payload = payload.unwrap_or(TransportPayload::None); + Ok(Transport { + sid: sid, + dstaddr: dstaddr, + mode: mode, + payload: payload, + }) } } diff --git a/src/message.rs b/src/message.rs index dad3c486..c4c4a17f 100644 --- a/src/message.rs +++ b/src/message.rs @@ -189,9 +189,7 @@ impl TryFrom for Message { type Err = Error; fn try_from(root: Element) -> Result { - if !root.is("message", ns::DEFAULT_NS) { - return Err(Error::ParseError("This is not a message element.")); - } + check_self!(root, "message", ns::DEFAULT_NS); let from = get_attr!(root, "from", optional); let to = get_attr!(root, "to", optional); let id = get_attr!(root, "id", optional); diff --git a/src/muc/muc.rs b/src/muc/muc.rs index 9758b800..14a65302 100644 --- a/src/muc/muc.rs +++ b/src/muc/muc.rs @@ -22,9 +22,7 @@ impl TryFrom for Muc { type Err = Error; fn try_from(elem: Element) -> Result { - if !elem.is("x", ns::MUC) { - return Err(Error::ParseError("This is not an x element.")); - } + check_self!(elem, "x", ns::MUC); check_no_attributes!(elem, "x"); let mut password = None; diff --git a/src/muc/user.rs b/src/muc/user.rs index 236ca8c8..a6614a63 100644 --- a/src/muc/user.rs +++ b/src/muc/user.rs @@ -93,9 +93,7 @@ impl TryFrom for Actor { type Err = Error; fn try_from(elem: Element) -> Result { - if !elem.is("actor", ns::MUC_USER) { - return Err(Error::ParseError("This is not a actor element.")); - } + check_self!(elem, "actor", ns::MUC_USER); check_no_unknown_attributes!(elem, "actor", ["jid", "nick"]); for _ in elem.children() { return Err(Error::ParseError("Unknown child in actor element.")); @@ -160,9 +158,7 @@ impl TryFrom for Item { type Err = Error; fn try_from(elem: Element) -> Result { - if !elem.is("item", ns::MUC_USER) { - return Err(Error::ParseError("This is not a item element.")); - } + check_self!(elem, "item", ns::MUC_USER); check_no_unknown_attributes!(elem, "item", ["affiliation", "jid", "nick", "role"]); let mut actor: Option = None; let mut continue_: Option = None; @@ -221,9 +217,7 @@ impl TryFrom for MucUser { type Err = Error; fn try_from(elem: Element) -> Result { - if !elem.is("x", ns::MUC_USER) { - return Err(Error::ParseError("This is not an x element.")); - } + check_self!(elem, "x", ns::MUC_USER); check_no_attributes!(elem, "x"); let mut status = vec!(); let mut items = vec!(); diff --git a/src/presence.rs b/src/presence.rs index d25a7b09..f4a4f11f 100644 --- a/src/presence.rs +++ b/src/presence.rs @@ -248,9 +248,7 @@ impl TryFrom for Presence { type Err = Error; fn try_from(root: Element) -> Result { - if !root.is("presence", ns::DEFAULT_NS) { - return Err(Error::ParseError("This is not a presence element.")); - } + check_self!(root, "presence", ns::DEFAULT_NS); let mut show = None; let mut priority = None; let mut presence = Presence { diff --git a/src/rsm.rs b/src/rsm.rs index 17642986..6b9f5439 100644 --- a/src/rsm.rs +++ b/src/rsm.rs @@ -28,9 +28,7 @@ impl TryFrom for Set { type Err = Error; fn try_from(elem: Element) -> Result { - if !elem.is("set", ns::RSM) { - return Err(Error::ParseError("This is not a RSM element.")); - } + check_self!(elem, "set", ns::RSM, "RSM set"); let mut set = Set { after: None, before: None, @@ -136,7 +134,7 @@ mod tests { Error::ParseError(string) => string, _ => panic!(), }; - assert_eq!(message, "This is not a RSM element."); + assert_eq!(message, "This is not a RSM set element."); } #[test] diff --git a/src/stanza_error.rs b/src/stanza_error.rs index 4548864c..4977739d 100644 --- a/src/stanza_error.rs +++ b/src/stanza_error.rs @@ -62,9 +62,7 @@ impl TryFrom for StanzaError { type Err = Error; fn try_from(elem: Element) -> Result { - if !elem.is("error", ns::DEFAULT_NS) { - return Err(Error::ParseError("This is not an error element.")); - } + check_self!(elem, "error", ns::DEFAULT_NS); let type_ = get_attr!(elem, "type", required); let by = get_attr!(elem, "by", optional); diff --git a/src/version.rs b/src/version.rs index e14bb422..9521dc99 100644 --- a/src/version.rs +++ b/src/version.rs @@ -20,9 +20,7 @@ impl TryFrom for Version { type Err = Error; fn try_from(elem: Element) -> Result { - if !elem.is("query", ns::VERSION) { - return Err(Error::ParseError("This is not a version element.")); - } + check_self!(elem, "query", ns::VERSION, "version"); check_no_attributes!(elem, "version"); let mut name = None; let mut version = None;