macros: Merge generate_element_with_text!() into generate_element!().

Emmanuel Gil Peyrot created

Change summary

src/component.rs     | 19 +++++++------
src/delay.rs         | 10 ++++--
src/hashes.rs        |  8 +++--
src/ibb.rs           | 10 ++++--
src/macros.rs        | 64 ++++++++++++---------------------------------
src/media_element.rs | 11 ++++---
src/sasl.rs          | 32 +++++++++++++++-------
7 files changed, 71 insertions(+), 83 deletions(-)

Detailed changes

src/component.rs 🔗

@@ -10,17 +10,18 @@ use helpers::PlainText;
 use sha1::Sha1;
 use digest::Digest;
 
-generate_element_with_text!(
+generate_element!(
     /// The main authentication mechanism for components.
     Handshake, "handshake", COMPONENT,
-
-    /// If Some, contains the hex-encoded SHA-1 of the concatenation of the
-    /// stream id and the password, and is used to authenticate against the
-    /// server.
-    ///
-    /// If None, it is the successful reply from the server, the stream is now
-    /// fully established and both sides can now exchange stanzas.
-    data: PlainText<Option<String>>
+    text: (
+        /// If Some, contains the hex-encoded SHA-1 of the concatenation of the
+        /// stream id and the password, and is used to authenticate against the
+        /// server.
+        ///
+        /// If None, it is the successful reply from the server, the stream is now
+        /// fully established and both sides can now exchange stanzas.
+        data: PlainText<Option<String>>
+    )
 );
 
 impl Handshake {

src/delay.rs 🔗

@@ -12,18 +12,20 @@ use jid::Jid;
 
 use helpers::PlainText;
 
-generate_element_with_text!(
+generate_element!(
     /// Notes when and by whom a message got stored for later delivery.
     Delay, "delay", DELAY,
-    [
+    attributes: [
         /// The entity which delayed this message.
         from: Option<Jid> = "from" => optional,
 
         /// The time at which this message got stored.
         stamp: DateTime = "stamp" => required
     ],
-    /// The optional reason this message got delayed.
-    data: PlainText<Option<String>>
+    text: (
+        /// The optional reason this message got delayed.
+        data: PlainText<Option<String>>
+    )
 );
 
 #[cfg(test)]

src/hashes.rs 🔗

@@ -66,13 +66,15 @@ impl IntoAttributeValue for Algo {
     }
 }
 
-generate_element_with_text!(
+generate_element!(
     #[derive(PartialEq)]
     Hash, "hash", HASHES,
-    [
+    attributes: [
         algo: Algo = "algo" => required
     ],
-    hash: Base64<Vec<u8>>
+    text: (
+        hash: Base64<Vec<u8>>
+    )
 );
 
 impl Hash {

src/ibb.rs 🔗

@@ -42,18 +42,20 @@ attributes: [
 
 impl IqSetPayload for Open {}
 
-generate_element_with_text!(
+generate_element!(
 /// Exchange a chunk of data in an open stream.
 Data, "data", IBB,
-    [
+    attributes: [
         /// Sequence number of this chunk, must wraparound after 65535.
         seq: u16 = "seq" => required,
 
         /// The identifier of the stream on which data is being exchanged.
         sid: StreamId = "sid" => required
     ],
-    /// Vector of bytes to be exchanged.
-    data: Base64<Vec<u8>>
+    text: (
+        /// Vector of bytes to be exchanged.
+        data: Base64<Vec<u8>>
+    )
 );
 
 impl IqSetPayload for Data {}

src/macros.rs 🔗

@@ -338,52 +338,6 @@ macro_rules! generate_elem_id {
     );
 }
 
-macro_rules! generate_element_with_text {
-    ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, $(#[$text_meta:meta])* $text_ident:ident: $codec:ident < $text_type:ty >) => (
-        generate_element_with_text!($(#[$meta])* $elem, $name, $ns, [], $(#[$text_meta])* $text_ident: $codec<$text_type>);
-    );
-    ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, [$($(#[$attr_meta:meta])* $attr:ident: $attr_type:ty = $attr_name:tt => $attr_action:tt),*], $(#[$text_meta:meta])* $text_ident:ident: $codec:ident < $text_type:ty >) => (
-        $(#[$meta])*
-        #[derive(Debug, Clone)]
-        pub struct $elem {
-            $(
-            $(#[$attr_meta])*
-            pub $attr: $attr_type,
-            )*
-            $(#[$text_meta])*
-            pub $text_ident: $text_type,
-        }
-
-        impl ::try_from::TryFrom<::minidom::Element> for $elem {
-            type Err = ::error::Error;
-
-            fn try_from(elem: ::minidom::Element) -> Result<$elem, ::error::Error> {
-                check_self!(elem, $name, $ns);
-                check_no_children!(elem, $name);
-                check_no_unknown_attributes!(elem, $name, [$($attr_name),*]);
-                Ok($elem {
-                    $(
-                    $attr: get_attr!(elem, $attr_name, $attr_action),
-                    )*
-                    $text_ident: $codec::decode(&elem.text())?,
-                })
-            }
-        }
-
-        impl From<$elem> for ::minidom::Element {
-            fn from(elem: $elem) -> ::minidom::Element {
-                ::minidom::Element::builder($name)
-                        .ns(::ns::$ns)
-                        $(
-                        .attr($attr_name, elem.$attr)
-                        )*
-                        .append($codec::encode(&elem.$text_ident))
-                        .build()
-            }
-        }
-    );
-}
-
 macro_rules! start_decl {
     (Vec, $type:ty) => (
         Vec<$type>
@@ -482,7 +436,13 @@ macro_rules! generate_element {
     ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, attributes: [$($(#[$attr_meta:meta])* $attr:ident: $attr_type:ty = $attr_name:tt => $attr_action:tt),*,], children: [$($(#[$child_meta:meta])* $child_ident:ident: $coucou:tt<$child_type:ty> = ($child_name:tt, $child_ns:ident) => $child_constructor:ident),*]) => (
         generate_element!($(#[$meta])* $elem, $name, $ns, attributes: [$($(#[$attr_meta])* $attr: $attr_type = $attr_name => $attr_action),*], children: [$($(#[$child_meta])* $child_ident: $coucou<$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),*], children: [$($(#[$child_meta:meta])* $child_ident:ident: $coucou:tt<$child_type:ty> = ($child_name:tt, $child_ns:ident) => $child_constructor:ident),*]) => (
+    ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, text: ($(#[$text_meta:meta])* $text_ident:ident: $codec:ident < $text_type:ty >)) => (
+        generate_element!($(#[$meta])* $elem, $name, $ns, attributes: [], children: [], text: ($(#[$text_meta])* $text_ident: $codec<$text_type>));
+    );
+    ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, attributes: [$($(#[$attr_meta:meta])* $attr:ident: $attr_type:ty = $attr_name:tt => $attr_action:tt),+], text: ($(#[$text_meta:meta])* $text_ident:ident: $codec:ident < $text_type:ty >)) => (
+        generate_element!($(#[$meta])* $elem, $name, $ns, attributes: [$($(#[$attr_meta])* $attr: $attr_type = $attr_name => $attr_action),*], children: [], text: ($(#[$text_meta])* $text_ident: $codec<$text_type>));
+    );
+    ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, attributes: [$($(#[$attr_meta:meta])* $attr:ident: $attr_type:ty = $attr_name:tt => $attr_action:tt),*], children: [$($(#[$child_meta:meta])* $child_ident:ident: $coucou:tt<$child_type:ty> = ($child_name:tt, $child_ns:ident) => $child_constructor:ident),*] $(, text: ($(#[$text_meta:meta])* $text_ident:ident: $codec:ident < $text_type:ty >))*) => (
         $(#[$meta])*
         #[derive(Debug, Clone)]
         pub struct $elem {
@@ -494,6 +454,10 @@ macro_rules! generate_element {
             $(#[$child_meta])*
             pub $child_ident: start_decl!($coucou, $child_type),
             )*
+            $(
+            $(#[$text_meta])*
+            pub $text_ident: $text_type,
+            )*
         }
 
         impl ::try_from::TryFrom<::minidom::Element> for $elem {
@@ -521,6 +485,9 @@ macro_rules! generate_element {
                     $(
                     $child_ident: finish_parse_elem!($child_ident: $coucou = $child_name, $name),
                     )*
+                    $(
+                    $text_ident: $codec::decode(&elem.text())?,
+                    )*
                 })
             }
         }
@@ -535,6 +502,9 @@ macro_rules! generate_element {
                         $(
                         .append(generate_serialiser!(elem, $child_ident, $coucou, $child_constructor, ($child_name, $child_ns)))
                         )*
+                        $(
+                        .append($codec::encode(&elem.$text_ident))
+                        )*
                         .build()
             }
         }

src/media_element.rs 🔗

@@ -8,10 +8,10 @@
 
 use helpers::TrimmedPlainText;
 
-generate_element_with_text!(
+generate_element!(
     /// Represents an URI used in a media element.
     URI, "uri", MEDIA_ELEMENT,
-    [
+    attributes: [
         /// The MIME type of the URI referenced.
         ///
         /// See the [IANA MIME Media Types Registry][1] for a list of
@@ -21,9 +21,10 @@ generate_element_with_text!(
         /// [1]: https://www.iana.org/assignments/media-types/media-types.xhtml
         type_: String = "type" => required
     ],
-
-    /// The actual URI contained.
-    uri: TrimmedPlainText<String>
+    text: (
+        /// The actual URI contained.
+        uri: TrimmedPlainText<String>
+    )
 );
 
 generate_element!(

src/sasl.rs 🔗

@@ -13,27 +13,37 @@ generate_attribute!(Mechanism, "mechanism", {
     Anonymous => "ANONYMOUS",
 });
 
-generate_element_with_text!(Auth, "auth", SASL,
-    [
+generate_element!(Auth, "auth", SASL,
+    attributes: [
         mechanism: Mechanism = "mechanism" => required
     ],
-    data: Base64<Vec<u8>>
+    text: (
+        data: Base64<Vec<u8>>
+    )
 );
 
-generate_element_with_text!(Challenge, "challenge", SASL,
-    data: Base64<Vec<u8>>
+generate_element!(Challenge, "challenge", SASL,
+    text: (
+        data: Base64<Vec<u8>>
+    )
 );
 
-generate_element_with_text!(Response, "response", SASL,
-    data: Base64<Vec<u8>>
+generate_element!(Response, "response", SASL,
+    text: (
+        data: Base64<Vec<u8>>
+    )
 );
 
-generate_element_with_text!(Success, "success", SASL,
-    data: Base64<Vec<u8>>
+generate_element!(Success, "success", SASL,
+    text: (
+        data: Base64<Vec<u8>>
+    )
 );
 
-generate_element_with_text!(Failure, "failure", SASL,
-    data: TrimmedPlainText<String>
+generate_element!(Failure, "failure", SASL,
+    text: (
+        data: TrimmedPlainText<String>
+    )
 );
 
 #[cfg(test)]