delay, eme, stanza_error: Use get_attr!.

This commit is contained in:
Emmanuel Gil Peyrot 2017-05-23 01:02:23 +01:00
parent d61d09f5b7
commit e3f1f31718
4 changed files with 11 additions and 17 deletions

View file

@ -30,8 +30,8 @@ impl<'a> TryFrom<&'a Element> for Delay {
for _ in elem.children() {
return Err(Error::ParseError("Unknown child in delay element."));
}
let from = elem.attr("from").and_then(|value| value.parse().ok());
let stamp = elem.attr("stamp").ok_or(Error::ParseError("Mandatory argument 'stamp' not present in delay element."))?.to_owned();
let from = get_attr!(elem, "from", optional);
let stamp = get_attr!(elem, "stamp", required);
let data = match elem.text().as_ref() {
"" => None,
text => Some(text.to_owned()),

View file

@ -28,8 +28,8 @@ impl<'a> TryFrom<&'a Element> for ExplicitMessageEncryption {
for _ in elem.children() {
return Err(Error::ParseError("Unknown child in encryption element."));
}
let namespace = elem.attr("namespace").ok_or(Error::ParseError("Mandatory argument 'namespace' not present in encryption element."))?.to_owned();
let name = elem.attr("name").and_then(|value| value.parse().ok());
let namespace = get_attr!(elem, "namespace", required);
let name = get_attr!(elem, "name", optional);
Ok(ExplicitMessageEncryption {
namespace: namespace,
name: name,

View file

@ -27,11 +27,8 @@ impl<'a> TryFrom<&'a Element> for Replace {
for _ in elem.children() {
return Err(Error::ParseError("Unknown child in replace element."));
}
let id = match elem.attr("id") {
Some(id) => id.to_owned(),
None => return Err(Error::ParseError("No 'id' attribute present in replace.")),
};
Ok(Replace { id: id })
let id = get_attr!(elem, "id", required);
Ok(Replace { id })
}
}
@ -73,7 +70,7 @@ mod tests {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "No 'id' attribute present in replace.");
assert_eq!(message, "Required attribute 'id' missing.");
}
#[test]

View file

@ -158,11 +158,8 @@ impl<'a> TryFrom<&'a Element> for StanzaError {
return Err(Error::ParseError("This is not an error element."));
}
let type_ = elem.attr("type")
.ok_or(Error::ParseError("Error must have a 'type' attribute."))?
.parse()?;
let by = elem.attr("by")
.and_then(|by| by.parse().ok());
let type_ = get_attr!(elem, "type", required);
let by = get_attr!(elem, "by", optional);
let mut defined_condition = None;
let mut texts = BTreeMap::new();
let mut other = None;
@ -172,7 +169,7 @@ impl<'a> TryFrom<&'a Element> for StanzaError {
for _ in child.children() {
return Err(Error::ParseError("Unknown element in error text."));
}
let lang = child.attr("xml:lang").unwrap_or("").to_owned();
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."));
}
@ -256,7 +253,7 @@ mod tests {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Error must have a 'type' attribute.");
assert_eq!(message, "Required attribute 'type' missing.");
let elem: Element = "<error xmlns='jabber:client' type='coucou'/>".parse().unwrap();
let error = StanzaError::try_from(&elem).unwrap_err();