From d9f2af6c97cf790bd3ac689388c55989bf52dc57 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 15 May 2018 02:06:38 +0200 Subject: [PATCH] mam: Use a macro to generate Result_. --- src/macros.rs | 3 +++ src/mam.rs | 52 ++++++++++----------------------------------------- 2 files changed, 13 insertions(+), 42 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index d4b6427c..df4b03e0 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -543,6 +543,9 @@ macro_rules! generate_element_with_children { ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, child: ($(#[$child_meta:meta])* $child_ident:ident: $child_type:ty = ($child_name:tt, $child_ns:ident) => $child_constructor:ident)) => ( generate_element_with_children!($(#[$meta])* $elem, $name, $ns, attributes: [], child: ($(#[$child_meta])* $child_ident: $child_type = ($child_name, $child_ns) => $child_constructor)); ); + ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, attributes: [$($(#[$attr_meta:meta])* $attr:ident: $attr_type:ty = $attr_name:tt => $attr_action:tt),*,], child: ($(#[$child_meta:meta])* $child_ident:ident: $child_type:ty = ($child_name:tt, $child_ns:ident) => $child_constructor:ident)) => ( + generate_element_with_children!($(#[$meta])* $elem, $name, $ns, attributes: [$($(#[$attr_meta])* $attr: $attr_type = $attr_name => $attr_action),*], child: ($(#[$child_meta])* $child_ident: $child_type = ($child_name, $child_ns) => $child_constructor)); + ); ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, attributes: [$($(#[$attr_meta:meta])* $attr:ident: $attr_type:ty = $attr_name:tt => $attr_action:tt),*], child: ($(#[$child_meta:meta])* $child_ident:ident: $child_type:ty = ($child_name:tt, $child_ns:ident) => $child_constructor:ident)) => ( $(#[$meta])* #[derive(Debug, Clone)] diff --git a/src/mam.rs b/src/mam.rs index d9f88929..22052cf3 100644 --- a/src/mam.rs +++ b/src/mam.rs @@ -25,12 +25,16 @@ pub struct Query { pub set: Option, } -#[derive(Debug, Clone)] -pub struct Result_ { - pub queryid: String, - pub id: String, - pub forwarded: Forwarded, -} +generate_element_with_children!( + Result_, "result", MAM, + attributes: [ + id: String = "id" => required, + queryid: String = "queryid" => required, + ], + child: ( + forwarded: Forwarded = ("forwarded", FORWARD) => Forwarded + ) +); #[derive(Debug, Clone)] pub struct Fin { @@ -74,31 +78,6 @@ impl TryFrom for Query { } } -impl TryFrom for Result_ { - type Err = Error; - - fn try_from(elem: Element) -> Result { - check_self!(elem, "result", MAM); - check_no_unknown_attributes!(elem, "result", ["queryid", "id"]); - let mut forwarded = None; - for child in elem.children() { - if child.is("forwarded", ns::FORWARD) { - forwarded = Some(Forwarded::try_from(child.clone())?); - } else { - return Err(Error::ParseError("Unknown child in result element.")); - } - } - let forwarded = forwarded.ok_or(Error::ParseError("Mandatory forwarded element missing in result."))?; - let queryid = get_attr!(elem, "queryid", required); - let id = get_attr!(elem, "id", required); - Ok(Result_ { - queryid, - id, - forwarded, - }) - } -} - impl TryFrom for Fin { type Err = Error; @@ -168,17 +147,6 @@ impl From for Element { } } -impl From for Element { - fn from(result: Result_) -> Element { - Element::builder("result") - .ns(ns::MAM) - .attr("queryid", result.queryid) - .attr("id", result.id) - .append(result.forwarded) - .build() - } -} - impl From for Element { fn from(fin: Fin) -> Element { Element::builder("fin")