diff --git a/src/delay.rs b/src/delay.rs index a15b61bc..79424dcb 100644 --- a/src/delay.rs +++ b/src/delay.rs @@ -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()), diff --git a/src/eme.rs b/src/eme.rs index ad28afc7..217517ce 100644 --- a/src/eme.rs +++ b/src/eme.rs @@ -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, diff --git a/src/message_correct.rs b/src/message_correct.rs index ecc555cc..ab0d3b2b 100644 --- a/src/message_correct.rs +++ b/src/message_correct.rs @@ -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] diff --git a/src/stanza_error.rs b/src/stanza_error.rs index 2f509ee2..85f21298 100644 --- a/src/stanza_error.rs +++ b/src/stanza_error.rs @@ -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 = "".parse().unwrap(); let error = StanzaError::try_from(&elem).unwrap_err();