From 898baddd3f97f0dbe0ecef6fe7df51c2b9f1d653 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Wed, 24 May 2017 23:47:27 +0100 Subject: [PATCH] disco: Split Into for Identity and Feature. --- src/disco.rs | 63 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/src/disco.rs b/src/disco.rs index acd2d48..46d25de 100644 --- a/src/disco.rs +++ b/src/disco.rs @@ -6,7 +6,7 @@ use std::convert::TryFrom; -use minidom::Element; +use minidom::{Element, IntoElements, ElementEmitter}; use error::Error; use ns; @@ -18,6 +18,21 @@ pub struct Feature { pub var: String, } +impl Into for Feature { + fn into(self) -> Element { + Element::builder("feature") + .ns(ns::DISCO_INFO) + .attr("var", self.var) + .build() + } +} + +impl IntoElements for Feature { + fn into_elements(self, emitter: &mut ElementEmitter) { + emitter.append_child(self.into()); + } +} + #[derive(Debug, Clone)] pub struct Identity { pub category: String, // TODO: use an enum here. @@ -26,6 +41,24 @@ pub struct Identity { pub name: Option, } +impl Into for Identity { + fn into(self) -> Element { + Element::builder("identity") + .ns(ns::DISCO_INFO) + .attr("category", self.category) + .attr("type", self.type_) + .attr("xml:lang", self.xml_lang) + .attr("name", self.name) + .build() + } +} + +impl IntoElements for Identity { + fn into_elements(self, emitter: &mut ElementEmitter) { + emitter.append_child(self.into()); + } +} + #[derive(Debug, Clone)] pub struct Disco { pub node: Option, @@ -111,31 +144,15 @@ impl TryFrom for Disco { impl Into for Disco { fn into(self) -> Element { - let mut root = Element::builder("query") - .ns(ns::DISCO_INFO) - .attr("node", self.node.clone()) - .build(); - for identity in self.identities { - let identity_element = Element::builder("identity") - .ns(ns::DISCO_INFO) - .attr("category", identity.category.clone()) - .attr("type", identity.type_.clone()) - .attr("xml:lang", identity.xml_lang.clone()) - .attr("name", identity.name.clone()) - .build(); - root.append_child(identity_element); - } - for feature in self.features { - let feature_element = Element::builder("feature") - .ns(ns::DISCO_INFO) - .attr("var", feature.var.clone()) - .build(); - root.append_child(feature_element); - } for _ in self.extensions { panic!("Not yet implemented!"); } - root + Element::builder("query") + .ns(ns::DISCO_INFO) + .attr("node", self.node) + .append(self.identities) + .append(self.features) + .build() } }