parsers: replace some generate_element! usage with derive macros

Jonas SchΓ€fer created

Change summary

parsers/src/http_upload.rs     | 18 +++++++++-------
parsers/src/message_correct.rs | 39 ++++++++++++++++++++--------------
parsers/src/occupant_id.rs     | 40 ++++++++++++++++++++---------------
parsers/src/pubsub/owner.rs    | 18 +++++++++-------
parsers/src/receipts.rs        | 23 +++++++++++---------
parsers/src/stanza_id.rs       | 21 ++++++++++--------
6 files changed, 91 insertions(+), 68 deletions(-)

Detailed changes

parsers/src/http_upload.rs πŸ”—

@@ -4,6 +4,8 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
+use xso::{FromXml, IntoXml};
+
 use crate::iq::{IqGetPayload, IqResultPayload};
 use crate::ns;
 use crate::Element;
@@ -90,14 +92,14 @@ generate_element!(
     ]
 );
 
-generate_element!(
-    /// Get URL
-    Get, "get", HTTP_UPLOAD,
-    attributes: [
-        /// URL
-        url: Required<String> = "url",
-    ]
-);
+/// Get URL
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::HTTP_UPLOAD, name = "get")]
+pub struct Get {
+    /// URL
+    #[xml(attribute)]
+    pub url: String,
+}
 
 generate_element!(
     /// Requesting a slot

parsers/src/message_correct.rs πŸ”—

@@ -4,17 +4,20 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
+use xso::{FromXml, IntoXml};
+
 use crate::message::MessagePayload;
+use crate::ns;
 
-generate_element!(
-    /// Defines that the message containing this payload should replace a
-    /// previous message, identified by the id.
-    Replace, "replace", MESSAGE_CORRECT,
-    attributes: [
-        /// The 'id' attribute of the message getting corrected.
-        id: Required<String> = "id",
-    ]
-);
+/// Defines that the message containing this payload should replace a
+/// previous message, identified by the id.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::MESSAGE_CORRECT, name = "replace")]
+pub struct Replace {
+    /// The 'id' attribute of the message getting corrected.
+    #[xml(attribute)]
+    pub id: String,
+}
 
 impl MessagePayload for Replace {}
 
@@ -47,7 +50,7 @@ mod tests {
     #[cfg(not(feature = "disable-validation"))]
     #[test]
     fn test_invalid_attribute() {
-        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' coucou=''/>"
+        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou' coucou=''/>"
             .parse()
             .unwrap();
         let error = Replace::try_from(elem).unwrap_err();
@@ -55,20 +58,21 @@ mod tests {
             FromElementError::Invalid(Error::Other(string)) => string,
             _ => panic!(),
         };
-        assert_eq!(message, "Unknown attribute in replace element.");
+        assert_eq!(message, "Unknown attribute in Replace element.");
     }
 
     #[test]
     fn test_invalid_child() {
-        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'><coucou/></replace>"
-            .parse()
-            .unwrap();
+        let elem: Element =
+            "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'><coucou/></replace>"
+                .parse()
+                .unwrap();
         let error = Replace::try_from(elem).unwrap_err();
         let message = match error {
             FromElementError::Invalid(Error::Other(string)) => string,
             _ => panic!(),
         };
-        assert_eq!(message, "Unknown child in replace element.");
+        assert_eq!(message, "Unknown child in Replace element.");
     }
 
     #[test]
@@ -81,7 +85,10 @@ mod tests {
             FromElementError::Invalid(Error::Other(string)) => string,
             _ => panic!(),
         };
-        assert_eq!(message, "Required attribute 'id' missing.");
+        assert_eq!(
+            message,
+            "Required attribute field 'id' on Replace element missing."
+        );
     }
 
     #[test]

parsers/src/occupant_id.rs πŸ”—

@@ -4,21 +4,23 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
+use xso::{FromXml, IntoXml};
+
 use crate::message::MessagePayload;
+use crate::ns;
 use crate::presence::PresencePayload;
 
-generate_element!(
-    /// Unique identifier given to a MUC participant.
-    ///
-    /// It allows clients to identify a MUC participant across reconnects and
-    /// renames. It thus prevents impersonification of anonymous users.
-    OccupantId, "occupant-id", OID,
-
-    attributes: [
-        /// The id associated to the sending user by the MUC service.
-        id: Required<String> = "id",
-    ]
-);
+/// Unique identifier given to a MUC participant.
+///
+/// It allows clients to identify a MUC participant across reconnects and
+/// renames. It thus prevents impersonification of anonymous users.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::OID, name = "occupant-id")]
+pub struct OccupantId {
+    /// The id associated to the sending user by the MUC service.
+    #[xml(attribute)]
+    pub id: String,
+}
 
 impl MessagePayload for OccupantId {}
 impl PresencePayload for OccupantId {}
@@ -52,15 +54,16 @@ mod tests {
 
     #[test]
     fn test_invalid_child() {
-        let elem: Element = "<occupant-id xmlns='urn:xmpp:occupant-id:0'><coucou/></occupant-id>"
-            .parse()
-            .unwrap();
+        let elem: Element =
+            "<occupant-id xmlns='urn:xmpp:occupant-id:0' id='foo'><coucou/></occupant-id>"
+                .parse()
+                .unwrap();
         let error = OccupantId::try_from(elem).unwrap_err();
         let message = match error {
             FromElementError::Invalid(Error::Other(string)) => string,
             _ => panic!(),
         };
-        assert_eq!(message, "Unknown child in occupant-id element.");
+        assert_eq!(message, "Unknown child in OccupantId element.");
     }
 
     #[test]
@@ -73,7 +76,10 @@ mod tests {
             FromElementError::Invalid(Error::Other(string)) => string,
             _ => panic!(),
         };
-        assert_eq!(message, "Required attribute 'id' missing.");
+        assert_eq!(
+            message,
+            "Required attribute field 'id' on OccupantId element missing."
+        );
     }
 
     #[test]

parsers/src/pubsub/owner.rs πŸ”—

@@ -5,6 +5,8 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
+use xso::{FromXml, IntoXml};
+
 use crate::data_forms::DataForm;
 use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
 use crate::ns;
@@ -73,14 +75,14 @@ generate_element!(
     ]
 );
 
-generate_element!(
-    /// A redirect element.
-    Redirect, "redirect", PUBSUB_OWNER,
-    attributes: [
-        /// The node this node will be redirected to.
-        uri: Required<String> = "uri",
-    ]
-);
+/// A redirect element.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::PUBSUB_OWNER, name = "redirect")]
+pub struct Redirect {
+    /// The node this node will be redirected to.
+    #[xml(attribute)]
+    pub uri: String,
+}
 
 generate_element!(
     /// Request to delete a node.

parsers/src/receipts.rs πŸ”—

@@ -17,15 +17,15 @@ pub struct Request;
 
 impl MessagePayload for Request {}
 
-generate_element!(
-    /// Notes that a previous message has correctly been received, it is
-    /// referenced by its 'id' attribute.
-    Received, "received", RECEIPTS,
-    attributes: [
-        /// The 'id' attribute of the received message.
-        id: Required<String> = "id",
-    ]
-);
+/// Notes that a previous message has correctly been received, it is
+/// referenced by its 'id' attribute.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::RECEIPTS, name = "received")]
+pub struct Received {
+    /// The 'id' attribute of the received message.
+    #[xml(attribute)]
+    pub id: String,
+}
 
 impl MessagePayload for Received {}
 
@@ -69,7 +69,10 @@ mod tests {
             FromElementError::Invalid(Error::Other(string)) => string,
             _ => panic!(),
         };
-        assert_eq!(message, "Required attribute 'id' missing.");
+        assert_eq!(
+            message,
+            "Required attribute field 'id' on Received element missing."
+        );
     }
 
     #[test]

parsers/src/stanza_id.rs πŸ”—

@@ -4,7 +4,10 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
+use xso::{FromXml, IntoXml};
+
 use crate::message::MessagePayload;
+use crate::ns;
 use jid::Jid;
 
 generate_element!(
@@ -22,15 +25,15 @@ generate_element!(
 
 impl MessagePayload for StanzaId {}
 
-generate_element!(
-    /// A hack for MUC before version 1.31 to track a message which may have
-    /// its 'id' attribute changed.
-    OriginId, "origin-id", SID,
-    attributes: [
-        /// The id this client set for this stanza.
-        id: Required<String> = "id",
-    ]
-);
+/// A hack for MUC before version 1.31 to track a message which may have
+/// its 'id' attribute changed.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::SID, name = "origin-id")]
+pub struct OriginId {
+    /// The id this client set for this stanza.
+    #[xml(attribute)]
+    pub id: String,
+}
 
 impl MessagePayload for OriginId {}