Use check_self!() where it makes sense.

This commit is contained in:
Emmanuel Gil Peyrot 2018-05-12 18:31:11 +02:00
parent e0438f9b88
commit 6bb466eea2
12 changed files with 62 additions and 95 deletions

View file

@ -26,9 +26,7 @@ impl TryFrom<Element> for Forwarded {
type Err = Error;
fn try_from(elem: Element) -> Result<Forwarded, Error> {
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() {

View file

@ -29,9 +29,7 @@ impl TryFrom<Element> for Query {
type Err = Error;
fn try_from(elem: Element) -> Result<Query, Error> {
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(),

View file

@ -207,9 +207,7 @@ impl TryFrom<Element> for Iq {
type Err = Error;
fn try_from(root: Element) -> Result<Iq, Error> {
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);

View file

@ -119,9 +119,7 @@ impl TryFrom<Element> for Content {
type Err = Error;
fn try_from(elem: Element) -> Result<Content, Error> {
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<Element> for ReasonElement {
type Err = Error;
fn try_from(elem: Element) -> Result<ReasonElement, Error> {
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<Element> for Jingle {
type Err = Error;
fn try_from(root: Element) -> Result<Jingle, Error> {
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),

View file

@ -111,57 +111,54 @@ impl TryFrom<Element> for Transport {
type Err = Error;
fn try_from(elem: Element) -> Result<Transport, Error> {
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,
})
}
}

View file

@ -189,9 +189,7 @@ impl TryFrom<Element> for Message {
type Err = Error;
fn try_from(root: Element) -> Result<Message, Error> {
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);

View file

@ -22,9 +22,7 @@ impl TryFrom<Element> for Muc {
type Err = Error;
fn try_from(elem: Element) -> Result<Muc, Error> {
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;

View file

@ -93,9 +93,7 @@ impl TryFrom<Element> for Actor {
type Err = Error;
fn try_from(elem: Element) -> Result<Actor, Error> {
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<Element> for Item {
type Err = Error;
fn try_from(elem: Element) -> Result<Item, Error> {
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<Actor> = None;
let mut continue_: Option<Continue> = None;
@ -221,9 +217,7 @@ impl TryFrom<Element> for MucUser {
type Err = Error;
fn try_from(elem: Element) -> Result<MucUser, Error> {
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!();

View file

@ -248,9 +248,7 @@ impl TryFrom<Element> for Presence {
type Err = Error;
fn try_from(root: Element) -> Result<Presence, Error> {
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 {

View file

@ -28,9 +28,7 @@ impl TryFrom<Element> for Set {
type Err = Error;
fn try_from(elem: Element) -> Result<Set, Error> {
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]

View file

@ -62,9 +62,7 @@ impl TryFrom<Element> for StanzaError {
type Err = Error;
fn try_from(elem: Element) -> Result<StanzaError, Error> {
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);

View file

@ -20,9 +20,7 @@ impl TryFrom<Element> for Version {
type Err = Error;
fn try_from(elem: Element) -> Result<Version, Error> {
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;