parsers: replace generate_empty_element with derive

Jonas SchΓ€fer created

Change summary

parsers/src/attention.rs       | 12 ++++++------
parsers/src/blocking.rs        | 24 +++++++++++-------------
parsers/src/carbons.rs         | 35 ++++++++++++++++-------------------
parsers/src/cert_management.rs | 13 +++++++------
parsers/src/csi.rs             | 34 ++++++++++++++++------------------
parsers/src/jingle_rtp.rs      | 15 ++++++++-------
parsers/src/mix.rs             | 15 ++++++++-------
parsers/src/ping.rs            | 15 ++++++++-------
parsers/src/receipts.rs        | 15 ++++++++-------
parsers/src/sasl.rs            | 14 +++++++-------
parsers/src/sm.rs              | 23 +++++++++++------------
parsers/src/time.rs            | 12 ++++++------
parsers/src/util/macros.rs     | 26 --------------------------
parsers/src/version.rs         | 19 ++++++++++---------
14 files changed, 122 insertions(+), 150 deletions(-)

Detailed changes

parsers/src/attention.rs πŸ”—

@@ -3,15 +3,15 @@
 // This Source Code Form is subject to the terms of the Mozilla Public
 // 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_empty_element!(
-    /// Requests the attention of the recipient.
-    Attention,
-    "attention",
-    ATTENTION
-);
+/// Requests the attention of the recipient.
+#[derive(FromXml, IntoXml, Debug, Clone, PartialEq)]
+#[xml(namespace = ns::ATTENTION, name = "attention")]
+pub struct Attention;
 
 impl MessagePayload for Attention {}
 

parsers/src/blocking.rs πŸ”—

@@ -4,19 +4,19 @@
 // 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, IqSetPayload};
 use crate::ns;
 use crate::Element;
 use jid::Jid;
 use xso::error::FromElementError;
 
-generate_empty_element!(
-    /// The element requesting the blocklist, the result iq will contain a
-    /// [BlocklistResult].
-    BlocklistRequest,
-    "blocklist",
-    BLOCKING
-);
+/// The element requesting the blocklist, the result iq will contain a
+/// [BlocklistResult].
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::BLOCKING, name = "blocklist")]
+pub struct BlocklistRequest;
 
 impl IqGetPayload for BlocklistRequest {}
 
@@ -87,12 +87,10 @@ generate_blocking_element!(
 
 impl IqSetPayload for Unblock {}
 
-generate_empty_element!(
-    /// The application-specific error condition when a message is blocked.
-    Blocked,
-    "blocked",
-    BLOCKING_ERRORS
-);
+/// The application-specific error condition when a message is blocked.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::BLOCKING_ERRORS, name = "blocked")]
+pub struct Blocked;
 
 #[cfg(test)]
 mod tests {

parsers/src/carbons.rs πŸ”—

@@ -4,35 +4,32 @@
 // 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::forwarding::Forwarded;
 use crate::iq::IqSetPayload;
 use crate::message::MessagePayload;
+use crate::ns;
 
-generate_empty_element!(
-    /// Enable carbons for this session.
-    Enable,
-    "enable",
-    CARBONS
-);
+/// Enable carbons for this session.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::CARBONS, name = "enable")]
+pub struct Enable;
 
 impl IqSetPayload for Enable {}
 
-generate_empty_element!(
-    /// Disable a previously-enabled carbons.
-    Disable,
-    "disable",
-    CARBONS
-);
+/// Disable a previously-enabled carbons.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::CARBONS, name = "disable")]
+pub struct Disable;
 
 impl IqSetPayload for Disable {}
 
-generate_empty_element!(
-    /// Request the enclosing message to not be copied to other carbons-enabled
-    /// resources of the user.
-    Private,
-    "private",
-    CARBONS
-);
+/// Request the enclosing message to not be copied to other carbons-enabled
+/// resources of the user.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::CARBONS, name = "private")]
+pub struct Private;
 
 impl MessagePayload for Private {}
 

parsers/src/cert_management.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::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
+use crate::ns;
 use crate::util::text_node_codecs::{Base64, Codec};
 
 generate_elem_id!(
@@ -40,12 +43,10 @@ generate_element!(
 
 impl IqSetPayload for Append {}
 
-generate_empty_element!(
-    /// Client requests the current list of X.509 certificates.
-    ListCertsQuery,
-    "items",
-    SASL_CERT
-);
+/// Client requests the current list of X.509 certificates.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::SASL_CERT, name = "items")]
+pub struct ListCertsQuery;
 
 impl IqGetPayload for ListCertsQuery {}
 

parsers/src/csi.rs πŸ”—

@@ -4,26 +4,24 @@
 // 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/.
 
-generate_empty_element!(
-    /// Stream:feature sent by the server to advertise it supports CSI.
-    Feature,
-    "csi",
-    CSI
-);
+use xso::{FromXml, IntoXml};
 
-generate_empty_element!(
-    /// Client indicates it is inactive.
-    Inactive,
-    "inactive",
-    CSI
-);
+use crate::ns;
 
-generate_empty_element!(
-    /// Client indicates it is active again.
-    Active,
-    "active",
-    CSI
-);
+/// Stream:feature sent by the server to advertise it supports CSI.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::CSI, name = "csi")]
+pub struct Feature;
+
+/// Client indicates it is inactive.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::CSI, name = "inactive")]
+pub struct Inactive;
+
+/// Client indicates it is active again.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::CSI, name = "active")]
+pub struct Active;
 
 #[cfg(test)]
 mod tests {

parsers/src/jingle_rtp.rs πŸ”—

@@ -4,17 +4,18 @@
 // 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::jingle_rtcp_fb::RtcpFb;
 use crate::jingle_rtp_hdrext::RtpHdrext;
 use crate::jingle_ssma::{Group, Source};
+use crate::ns;
 
-generate_empty_element!(
-    /// Specifies the ability to multiplex RTP Data and Control Packets on a single port as
-    /// described in RFCΒ 5761.
-    RtcpMux,
-    "rtcp-mux",
-    JINGLE_RTP
-);
+/// Specifies the ability to multiplex RTP Data and Control Packets on a single port as
+/// described in RFCΒ 5761.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::JINGLE_RTP, name = "rtcp-mux")]
+pub struct RtcpMux;
 
 generate_element!(
     /// Wrapper element describing an RTP session.

parsers/src/mix.rs πŸ”—

@@ -7,8 +7,11 @@
 // TODO: validate nicks by applying the β€œnickname” profile of the PRECIS OpaqueString class, as
 // defined in RFC 7700.
 
+use xso::{FromXml, IntoXml};
+
 use crate::iq::{IqResultPayload, IqSetPayload};
 use crate::message::MessagePayload;
+use crate::ns;
 use crate::pubsub::{NodeName, PubSubPayload};
 use jid::BareJid;
 
@@ -146,13 +149,11 @@ impl UpdateSubscription {
     }
 }
 
-generate_empty_element!(
-    /// Request to leave a given MIX channel.  It will automatically unsubscribe the user from all
-    /// nodes on this channel.
-    Leave,
-    "leave",
-    MIX_CORE
-);
+/// Request to leave a given MIX channel.  It will automatically unsubscribe the user from all
+/// nodes on this channel.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::MIX_CORE, name = "leave")]
+pub struct Leave;
 
 impl IqSetPayload for Leave {}
 impl IqResultPayload for Leave {}

parsers/src/ping.rs πŸ”—

@@ -5,15 +5,16 @@
 // 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;
+use crate::ns;
 
-generate_empty_element!(
-    /// Represents a ping to the recipient, which must be answered with an
-    /// empty `<iq/>` or with an error.
-    Ping,
-    "ping",
-    PING
-);
+/// Represents a ping to the recipient, which must be answered with an
+/// empty `<iq/>` or with an error.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::PING, name = "ping")]
+pub struct Ping;
 
 impl IqGetPayload for Ping {}
 

parsers/src/receipts.rs πŸ”—

@@ -4,15 +4,16 @@
 // 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_empty_element!(
-    /// Requests that this message is acked by the final recipient once
-    /// received.
-    Request,
-    "request",
-    RECEIPTS
-);
+/// Requests that this message is acked by the final recipient once
+/// received.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::RECEIPTS, name = "request")]
+pub struct Request;
 
 impl MessagePayload for Request {}
 

parsers/src/sasl.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::ns;
 use crate::util::text_node_codecs::{Base64, Codec};
 use crate::Element;
@@ -78,13 +80,11 @@ generate_element!(
     )
 );
 
-generate_empty_element!(
-    /// Sent by the client at any point after [auth](struct.Auth.html) if it
-    /// wants to cancel the current authentication process.
-    Abort,
-    "abort",
-    SASL
-);
+/// Sent by the client at any point after [auth](struct.Auth.html) if it
+/// wants to cancel the current authentication process.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::SASL, name = "abort")]
+pub struct Abort;
 
 generate_element!(
     /// Sent by the server on SASL success.

parsers/src/sm.rs πŸ”—

@@ -4,6 +4,9 @@
 // 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::ns;
 use crate::stanza_error::DefinedCondition;
 
 generate_element!(
@@ -101,12 +104,10 @@ generate_element!(
     ]
 );
 
-generate_empty_element!(
-    /// Requests the currently received stanzas by the other party.
-    R,
-    "r",
-    SM
-);
+/// Requests the currently received stanzas by the other party.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::SM, name = "r")]
+pub struct R;
 
 generate_element!(
     /// Requests a stream resumption.
@@ -135,12 +136,10 @@ generate_element!(
 );
 
 // TODO: add support for optional and required.
-generate_empty_element!(
-    /// Represents availability of Stream Management in `<stream:features/>`.
-    StreamManagement,
-    "sm",
-    SM
-);
+/// Represents availability of Stream Management in `<stream:features/>`.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::SM, name = "sm")]
+pub struct StreamManagement;
 
 #[cfg(test)]
 mod tests {

parsers/src/time.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::date::DateTime;
 use crate::iq::{IqGetPayload, IqResultPayload};
 use crate::ns;
@@ -12,12 +14,10 @@ use chrono::FixedOffset;
 use std::str::FromStr;
 use xso::error::{Error, FromElementError};
 
-generate_empty_element!(
-    /// An entity time query.
-    TimeQuery,
-    "time",
-    TIME
-);
+/// An entity time query.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::TIME, name = "time")]
+pub struct TimeQuery;
 
 impl IqGetPayload for TimeQuery {}
 

parsers/src/util/macros.rs πŸ”—

@@ -365,32 +365,6 @@ macro_rules! check_no_unknown_attributes {
     );
 }
 
-macro_rules! generate_empty_element {
-    ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident) => (
-        $(#[$meta])*
-        #[derive(Debug, Clone, PartialEq)]
-        pub struct $elem;
-
-        impl ::std::convert::TryFrom<crate::Element> for $elem {
-            type Error = xso::error::FromElementError;
-
-            fn try_from(elem: crate::Element) -> Result<$elem, xso::error::FromElementError> {
-                check_self!(elem, $name, $ns);
-                check_no_children!(elem, $name);
-                check_no_attributes!(elem, $name);
-                Ok($elem)
-            }
-        }
-
-        impl From<$elem> for crate::Element {
-            fn from(_: $elem) -> crate::Element {
-                crate::Element::builder($name, crate::ns::$ns)
-                    .build()
-            }
-        }
-    );
-}
-
 macro_rules! generate_id {
     ($(#[$meta:meta])* $elem:ident) => (
         $(#[$meta])*

parsers/src/version.rs πŸ”—

@@ -4,17 +4,18 @@
 // 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;
 
-generate_empty_element!(
-    /// Represents a query for the software version a remote entity is using.
-    ///
-    /// It should only be used in an `<iq type='get'/>`, as it can only
-    /// represent the request, and not a result.
-    VersionQuery,
-    "query",
-    VERSION
-);
+/// Represents a query for the software version a remote entity is using.
+///
+/// It should only be used in an `<iq type='get'/>`, as it can only
+/// represent the request, and not a result.
+#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
+#[xml(namespace = ns::VERSION, name = "query")]
+pub struct VersionQuery;
 
 impl IqGetPayload for VersionQuery {}