From 649286d59e701c5d836f3daa3a9d71ba52413be2 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 31 Oct 2017 18:49:22 +0000 Subject: [PATCH] disco: Use generate_element_with_only_attributes. --- src/disco.rs | 129 ++++++++------------------------------------------- 1 file changed, 19 insertions(+), 110 deletions(-) diff --git a/src/disco.rs b/src/disco.rs index 2c3189f0..9964cd8c 100644 --- a/src/disco.rs +++ b/src/disco.rs @@ -16,66 +16,23 @@ use ns; use data_forms::{DataForm, DataFormType}; +generate_element_with_only_attributes!( /// Structure representing a `` element. /// /// It should only be used in an ``, as it can only represent /// the request, and not a result. -#[derive(Debug, Clone)] -pub struct DiscoInfoQuery { +DiscoInfoQuery, "query", ns::DISCO_INFO, [ /// Node on which we are doing the discovery. - pub node: Option, -} - -impl TryFrom for DiscoInfoQuery { - type Err = Error; - - fn try_from(elem: Element) -> Result { - check_self!(elem, "query", ns::DISCO_INFO); - check_no_children!(elem, "query"); - check_no_unknown_attributes!(elem, "query", ["node"]); - Ok(DiscoInfoQuery { - node: get_attr!(elem, "node", optional), - }) - } -} - -impl From for Element { - fn from(disco: DiscoInfoQuery) -> Element { - Element::builder("query") - .ns(ns::DISCO_INFO) - .attr("node", disco.node) - .build() - } -} + node: Option = "node" => optional, +]); +generate_element_with_only_attributes!( /// Structure representing a `` element. -#[derive(Debug, Clone, PartialEq)] -pub struct Feature { +#[derive(PartialEq)] +Feature, "feature", ns::DISCO_INFO, [ /// Namespace of the feature we want to represent. - pub var: String, -} - -impl TryFrom for Feature { - type Err = Error; - - fn try_from(elem: Element) -> Result { - check_self!(elem, "feature", ns::DISCO_INFO, "disco#info feature"); - check_no_children!(elem, "disco#info feature"); - check_no_unknown_attributes!(elem, "disco#info feature", ["var"]); - Ok(Feature { - var: get_attr!(elem, "var", required) - }) - } -} - -impl From for Element { - fn from(feature: Feature) -> Element { - Element::builder("feature") - .ns(ns::DISCO_INFO) - .attr("var", feature.var) - .build() - } -} + var: String = "var" => required, +]); /// Structure representing an `` element. #[derive(Debug, Clone)] @@ -223,74 +180,26 @@ impl From for Element { } } +generate_element_with_only_attributes!( /// Structure representing a `` element. /// /// It should only be used in an ``, as it can only represent /// the request, and not a result. -#[derive(Debug, Clone)] -pub struct DiscoItemsQuery { +DiscoItemsQuery, "query", ns::DISCO_ITEMS, [ /// Node on which we are doing the discovery. - pub node: Option, -} - -impl TryFrom for DiscoItemsQuery { - type Err = Error; - - fn try_from(elem: Element) -> Result { - check_self!(elem, "query", ns::DISCO_ITEMS, "disco#items query"); - check_no_children!(elem, "disco#items query"); - check_no_unknown_attributes!(elem, "disco#items query", ["node"]); - Ok(DiscoItemsQuery { - node: get_attr!(elem, "node", optional), - }) - } -} - -impl From for Element { - fn from(disco: DiscoItemsQuery) -> Element { - Element::builder("query") - .ns(ns::DISCO_ITEMS) - .attr("node", disco.node) - .build() - } -} + node: Option = "node" => optional, +]); +generate_element_with_only_attributes!( /// Structure representing an `` element. -#[derive(Debug, Clone)] -pub struct Item { +Item, "item", ns::DISCO_ITEMS, [ /// JID of the entity pointed by this item. - pub jid: Jid, + jid: Jid = "jid" => required, /// Node of the entity pointed by this item. - pub node: Option, + node: Option = "node" => optional, /// Name of the entity pointed by this item. - pub name: Option, -} - -impl TryFrom for Item { - type Err = Error; - - fn try_from(elem: Element) -> Result { - check_self!(elem, "item", ns::DISCO_ITEMS); - check_no_children!(elem, "item"); - check_no_unknown_attributes!(elem, "item", ["jid", "node", "name"]); - Ok(Item { - jid: get_attr!(elem, "jid", required), - node: get_attr!(elem, "node", optional), - name: get_attr!(elem, "name", optional), - }) - } -} - -impl From for Element { - fn from(item: Item) -> Element { - Element::builder("item") - .ns(ns::DISCO_ITEMS) - .attr("jid", item.jid) - .attr("node", item.node) - .attr("name", item.name) - .build() - } -} + name: Option = "name" => optional, +]); /// Structure representing a `` element.