From 764a7190e97e631041472da8039f16661f274536 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 25 May 2017 01:14:36 +0100 Subject: [PATCH] stanza_error: Simplify with IntoElements. --- src/jingle.rs | 20 ++++++++++---------- src/stanza_error.rs | 14 ++++++-------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/jingle.rs b/src/jingle.rs index 0658220..c5041b0 100644 --- a/src/jingle.rs +++ b/src/jingle.rs @@ -99,12 +99,12 @@ impl FromStr for Creator { } } -impl From for String { - fn from(creator: Creator) -> String { - String::from(match creator { +impl IntoAttributeValue for Creator { + fn into_attribute_value(self) -> Option { + Some(String::from(match self { Creator::Initiator => "initiator", Creator::Responder => "responder", - }) + })) } } @@ -137,14 +137,14 @@ impl FromStr for Senders { } } -impl From for String { - fn from(senders: Senders) -> String { - String::from(match senders { +impl IntoAttributeValue for Senders { + fn into_attribute_value(self) -> Option { + Some(String::from(match self { Senders::Both => "both", Senders::Initiator => "initiator", Senders::None_ => "none", Senders::Responder => "responder", - }) + })) } } @@ -202,10 +202,10 @@ impl Into for Content { fn into(self) -> Element { Element::builder("content") .ns(ns::JINGLE) - .attr("creator", String::from(self.creator)) + .attr("creator", self.creator) .attr("disposition", self.disposition) .attr("name", self.name) - .attr("senders", String::from(self.senders)) + .attr("senders", self.senders) .append(self.description) .append(self.transport) .append(self.security) diff --git a/src/stanza_error.rs b/src/stanza_error.rs index 0a32757..3841ad5 100644 --- a/src/stanza_error.rs +++ b/src/stanza_error.rs @@ -8,7 +8,7 @@ use std::convert::TryFrom; use std::str::FromStr; use std::collections::BTreeMap; -use minidom::{Element, IntoAttributeValue}; +use minidom::{Element, IntoElements, IntoAttributeValue, ElementEmitter}; use error::Error; use jid::Jid; @@ -110,9 +110,9 @@ impl FromStr for DefinedCondition { } } -impl From for String { - fn from(defined_condition: DefinedCondition) -> String { - String::from(match defined_condition { +impl IntoElements for DefinedCondition { + fn into_elements(self, emitter: &mut ElementEmitter) { + emitter.append_child(Element::builder(match self { DefinedCondition::BadRequest => "bad-request", DefinedCondition::Conflict => "conflict", DefinedCondition::FeatureNotImplemented => "feature-not-implemented", @@ -135,7 +135,7 @@ impl From for String { DefinedCondition::SubscriptionRequired => "subscription-required", DefinedCondition::UndefinedCondition => "undefined-condition", DefinedCondition::UnexpectedRequest => "unexpected-request", - }) + }).ns(ns::XMPP_STANZAS).build()); } } @@ -207,9 +207,7 @@ impl Into for StanzaError { .ns(ns::JABBER_CLIENT) .attr("type", self.type_) .attr("by", self.by.and_then(|by| Some(String::from(by)))) - .append(Element::builder(self.defined_condition) - .ns(ns::XMPP_STANZAS) - .build()) + .append(self.defined_condition) .build(); for (lang, text) in self.texts { let elem = Element::builder("text")