stanza_error: Simplify attribute handling.

This commit is contained in:
Emmanuel Gil Peyrot 2017-05-24 23:56:35 +01:00
parent 898baddd3f
commit 3678d9f0d3

View file

@ -8,7 +8,7 @@ use std::convert::TryFrom;
use std::str::FromStr; use std::str::FromStr;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use minidom::Element; use minidom::{Element, IntoAttributeValue};
use error::Error; use error::Error;
use jid::Jid; use jid::Jid;
@ -39,15 +39,15 @@ impl FromStr for ErrorType {
} }
} }
impl From<ErrorType> for String { impl IntoAttributeValue for ErrorType {
fn from(type_: ErrorType) -> String { fn into_attribute_value(self) -> Option<String> {
String::from(match type_ { Some(String::from(match self {
ErrorType::Auth => "auth", ErrorType::Auth => "auth",
ErrorType::Cancel => "cancel", ErrorType::Cancel => "cancel",
ErrorType::Continue => "continue", ErrorType::Continue => "continue",
ErrorType::Modify => "modify", ErrorType::Modify => "modify",
ErrorType::Wait => "wait", ErrorType::Wait => "wait",
}) }))
} }
} }
@ -189,11 +189,7 @@ impl TryFrom<Element> for StanzaError {
other = Some(child.clone()); other = Some(child.clone());
} }
} }
let defined_condition = defined_condition.ok_or(Error::ParseError("Error must have a defined-condition."))?;
if defined_condition.is_none() {
return Err(Error::ParseError("Error must have a defined-condition."));
}
let defined_condition = defined_condition.unwrap();
Ok(StanzaError { Ok(StanzaError {
type_: type_, type_: type_,
@ -209,16 +205,13 @@ impl Into<Element> for StanzaError {
fn into(self) -> Element { fn into(self) -> Element {
let mut root = Element::builder("error") let mut root = Element::builder("error")
.ns(ns::JABBER_CLIENT) .ns(ns::JABBER_CLIENT)
.attr("type", String::from(self.type_.clone())) .attr("type", self.type_)
.attr("by", match self.by { .attr("by", self.by.and_then(|by| Some(String::from(by))))
Some(ref by) => Some(String::from(by.clone())), .append(Element::builder(self.defined_condition)
None => None,
})
.append(Element::builder(self.defined_condition.clone())
.ns(ns::XMPP_STANZAS) .ns(ns::XMPP_STANZAS)
.build()) .build())
.build(); .build();
for (lang, text) in self.texts.clone() { for (lang, text) in self.texts {
let elem = Element::builder("text") let elem = Element::builder("text")
.ns(ns::XMPP_STANZAS) .ns(ns::XMPP_STANZAS)
.attr("xml:lang", lang) .attr("xml:lang", lang)
@ -226,8 +219,8 @@ impl Into<Element> for StanzaError {
.build(); .build();
root.append_child(elem); root.append_child(elem);
} }
if let Some(ref other) = self.other { if let Some(other) = self.other {
root.append_child(other.clone()); root.append_child(other);
} }
root root
} }