mam: Use a macro to generate Result_.

Emmanuel Gil Peyrot created

Change summary

src/macros.rs |  3 +++
src/mam.rs    | 52 ++++++++++------------------------------------------
2 files changed, 13 insertions(+), 42 deletions(-)

Detailed changes

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)]

src/mam.rs 🔗

@@ -25,12 +25,16 @@ pub struct Query {
     pub set: Option<Set>,
 }
 
-#[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<Element> for Query {
     }
 }
 
-impl TryFrom<Element> for Result_ {
-    type Err = Error;
-
-    fn try_from(elem: Element) -> Result<Result_, Error> {
-        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<Element> for Fin {
     type Err = Error;
 
@@ -168,17 +147,6 @@ impl From<Query> for Element {
     }
 }
 
-impl From<Result_> 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<Fin> for Element {
     fn from(fin: Fin) -> Element {
         Element::builder("fin")