parsers: replace generate_empty_element with derive

This commit is contained in:
Jonas Schäfer 2024-06-21 17:54:12 +02:00
commit bc785fde28
14 changed files with 122 additions and 150 deletions

View file

@ -3,15 +3,15 @@
// This Source Code Form is subject to the terms of the Mozilla Public // 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 // 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/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use xso::{FromXml, IntoXml};
use crate::message::MessagePayload; use crate::message::MessagePayload;
use crate::ns;
generate_empty_element!(
/// Requests the attention of the recipient. /// Requests the attention of the recipient.
Attention, #[derive(FromXml, IntoXml, Debug, Clone, PartialEq)]
"attention", #[xml(namespace = ns::ATTENTION, name = "attention")]
ATTENTION pub struct Attention;
);
impl MessagePayload for Attention {} impl MessagePayload for Attention {}

View file

@ -4,19 +4,19 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // 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/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use xso::{FromXml, IntoXml};
use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload}; use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
use crate::ns; use crate::ns;
use crate::Element; use crate::Element;
use jid::Jid; use jid::Jid;
use xso::error::FromElementError; use xso::error::FromElementError;
generate_empty_element!(
/// The element requesting the blocklist, the result iq will contain a /// The element requesting the blocklist, the result iq will contain a
/// [BlocklistResult]. /// [BlocklistResult].
BlocklistRequest, #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
"blocklist", #[xml(namespace = ns::BLOCKING, name = "blocklist")]
BLOCKING pub struct BlocklistRequest;
);
impl IqGetPayload for BlocklistRequest {} impl IqGetPayload for BlocklistRequest {}
@ -87,12 +87,10 @@ generate_blocking_element!(
impl IqSetPayload for Unblock {} impl IqSetPayload for Unblock {}
generate_empty_element!(
/// The application-specific error condition when a message is blocked. /// The application-specific error condition when a message is blocked.
Blocked, #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
"blocked", #[xml(namespace = ns::BLOCKING_ERRORS, name = "blocked")]
BLOCKING_ERRORS pub struct Blocked;
);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View file

@ -4,35 +4,32 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // 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/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use xso::{FromXml, IntoXml};
use crate::forwarding::Forwarded; use crate::forwarding::Forwarded;
use crate::iq::IqSetPayload; use crate::iq::IqSetPayload;
use crate::message::MessagePayload; use crate::message::MessagePayload;
use crate::ns;
generate_empty_element!(
/// Enable carbons for this session. /// Enable carbons for this session.
Enable, #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
"enable", #[xml(namespace = ns::CARBONS, name = "enable")]
CARBONS pub struct Enable;
);
impl IqSetPayload for Enable {} impl IqSetPayload for Enable {}
generate_empty_element!(
/// Disable a previously-enabled carbons. /// Disable a previously-enabled carbons.
Disable, #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
"disable", #[xml(namespace = ns::CARBONS, name = "disable")]
CARBONS pub struct Disable;
);
impl IqSetPayload for Disable {} impl IqSetPayload for Disable {}
generate_empty_element!(
/// Request the enclosing message to not be copied to other carbons-enabled /// Request the enclosing message to not be copied to other carbons-enabled
/// resources of the user. /// resources of the user.
Private, #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
"private", #[xml(namespace = ns::CARBONS, name = "private")]
CARBONS pub struct Private;
);
impl MessagePayload for Private {} impl MessagePayload for Private {}

View file

@ -4,7 +4,10 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // 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/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use xso::{FromXml, IntoXml};
use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload}; use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
use crate::ns;
use crate::util::text_node_codecs::{Base64, Codec}; use crate::util::text_node_codecs::{Base64, Codec};
generate_elem_id!( generate_elem_id!(
@ -40,12 +43,10 @@ generate_element!(
impl IqSetPayload for Append {} impl IqSetPayload for Append {}
generate_empty_element!(
/// Client requests the current list of X.509 certificates. /// Client requests the current list of X.509 certificates.
ListCertsQuery, #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
"items", #[xml(namespace = ns::SASL_CERT, name = "items")]
SASL_CERT pub struct ListCertsQuery;
);
impl IqGetPayload for ListCertsQuery {} impl IqGetPayload for ListCertsQuery {}

View file

@ -4,26 +4,24 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // 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/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
generate_empty_element!( use xso::{FromXml, IntoXml};
use crate::ns;
/// Stream:feature sent by the server to advertise it supports CSI. /// Stream:feature sent by the server to advertise it supports CSI.
Feature, #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
"csi", #[xml(namespace = ns::CSI, name = "csi")]
CSI pub struct Feature;
);
generate_empty_element!(
/// Client indicates it is inactive. /// Client indicates it is inactive.
Inactive, #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
"inactive", #[xml(namespace = ns::CSI, name = "inactive")]
CSI pub struct Inactive;
);
generate_empty_element!(
/// Client indicates it is active again. /// Client indicates it is active again.
Active, #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
"active", #[xml(namespace = ns::CSI, name = "active")]
CSI pub struct Active;
);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View file

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

View file

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

View file

@ -5,15 +5,16 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // 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/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use crate::iq::IqGetPayload; 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 /// Represents a ping to the recipient, which must be answered with an
/// empty `<iq/>` or with an error. /// empty `<iq/>` or with an error.
Ping, #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
"ping", #[xml(namespace = ns::PING, name = "ping")]
PING pub struct Ping;
);
impl IqGetPayload for Ping {} impl IqGetPayload for Ping {}

View file

@ -4,15 +4,16 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // 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/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use crate::message::MessagePayload; 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 /// Requests that this message is acked by the final recipient once
/// received. /// received.
Request, #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
"request", #[xml(namespace = ns::RECEIPTS, name = "request")]
RECEIPTS pub struct Request;
);
impl MessagePayload for Request {} impl MessagePayload for Request {}

View file

@ -4,6 +4,8 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // 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/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use xso::{FromXml, IntoXml};
use crate::ns; use crate::ns;
use crate::util::text_node_codecs::{Base64, Codec}; use crate::util::text_node_codecs::{Base64, Codec};
use crate::Element; 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 /// Sent by the client at any point after [auth](struct.Auth.html) if it
/// wants to cancel the current authentication process. /// wants to cancel the current authentication process.
Abort, #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
"abort", #[xml(namespace = ns::SASL, name = "abort")]
SASL pub struct Abort;
);
generate_element!( generate_element!(
/// Sent by the server on SASL success. /// Sent by the server on SASL success.

View file

@ -4,6 +4,9 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // 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/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use xso::{FromXml, IntoXml};
use crate::ns;
use crate::stanza_error::DefinedCondition; use crate::stanza_error::DefinedCondition;
generate_element!( generate_element!(
@ -101,12 +104,10 @@ generate_element!(
] ]
); );
generate_empty_element!(
/// Requests the currently received stanzas by the other party. /// Requests the currently received stanzas by the other party.
R, #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
"r", #[xml(namespace = ns::SM, name = "r")]
SM pub struct R;
);
generate_element!( generate_element!(
/// Requests a stream resumption. /// Requests a stream resumption.
@ -135,12 +136,10 @@ generate_element!(
); );
// TODO: add support for optional and required. // TODO: add support for optional and required.
generate_empty_element!(
/// Represents availability of Stream Management in `<stream:features/>`. /// Represents availability of Stream Management in `<stream:features/>`.
StreamManagement, #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
"sm", #[xml(namespace = ns::SM, name = "sm")]
SM pub struct StreamManagement;
);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View file

@ -4,6 +4,8 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // 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/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use xso::{FromXml, IntoXml};
use crate::date::DateTime; use crate::date::DateTime;
use crate::iq::{IqGetPayload, IqResultPayload}; use crate::iq::{IqGetPayload, IqResultPayload};
use crate::ns; use crate::ns;
@ -12,12 +14,10 @@ use chrono::FixedOffset;
use std::str::FromStr; use std::str::FromStr;
use xso::error::{Error, FromElementError}; use xso::error::{Error, FromElementError};
generate_empty_element!(
/// An entity time query. /// An entity time query.
TimeQuery, #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
"time", #[xml(namespace = ns::TIME, name = "time")]
TIME pub struct TimeQuery;
);
impl IqGetPayload for TimeQuery {} impl IqGetPayload for TimeQuery {}

View file

@ -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 { macro_rules! generate_id {
($(#[$meta:meta])* $elem:ident) => ( ($(#[$meta:meta])* $elem:ident) => (
$(#[$meta])* $(#[$meta])*

View file

@ -4,17 +4,18 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // 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/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use crate::iq::{IqGetPayload, IqResultPayload}; 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. /// 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 /// It should only be used in an `<iq type='get'/>`, as it can only
/// represent the request, and not a result. /// represent the request, and not a result.
VersionQuery, #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
"query", #[xml(namespace = ns::VERSION, name = "query")]
VERSION pub struct VersionQuery;
);
impl IqGetPayload for VersionQuery {} impl IqGetPayload for VersionQuery {}