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() { for _ in elem.children() {
return Err(Error::ParseError("Unknown child in delay element.")); return Err(Error::ParseError("Unknown child in delay element."));
} }
let from = elem.attr("from").and_then(|value| value.parse().ok()); let from = get_attr!(elem, "from", optional);
let stamp = elem.attr("stamp").ok_or(Error::ParseError("Mandatory argument 'stamp' not present in delay element."))?.to_owned(); let stamp = get_attr!(elem, "stamp", required);
let data = match elem.text().as_ref() { let data = match elem.text().as_ref() {
"" => None, "" => None,
text => Some(text.to_owned()), text => Some(text.to_owned()),

View file

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

View file

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

View file

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