parsers: replace generate_empty_element with derive

This commit is contained in:
Jonas Schäfer 2024-06-21 17:54:12 +02:00
parent 0adfd1218b
commit bc785fde28
14 changed files with 125 additions and 153 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. #[derive(FromXml, IntoXml, Debug, Clone, PartialEq)]
Attention, #[xml(namespace = ns::ATTENTION, name = "attention")]
"attention", pub struct Attention;
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]. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
BlocklistRequest, #[xml(namespace = ns::BLOCKING, name = "blocklist")]
"blocklist", pub struct BlocklistRequest;
BLOCKING
);
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. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
Blocked, #[xml(namespace = ns::BLOCKING_ERRORS, name = "blocked")]
"blocked", pub struct Blocked;
BLOCKING_ERRORS
);
#[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. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
Enable, #[xml(namespace = ns::CARBONS, name = "enable")]
"enable", pub struct Enable;
CARBONS
);
impl IqSetPayload for Enable {} impl IqSetPayload for Enable {}
generate_empty_element!( /// Disable a previously-enabled carbons.
/// Disable a previously-enabled carbons. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
Disable, #[xml(namespace = ns::CARBONS, name = "disable")]
"disable", pub struct Disable;
CARBONS
);
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. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
Private, #[xml(namespace = ns::CARBONS, name = "private")]
"private", pub struct Private;
CARBONS
);
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. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
ListCertsQuery, #[xml(namespace = ns::SASL_CERT, name = "items")]
"items", pub struct ListCertsQuery;
SASL_CERT
);
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};
/// Stream:feature sent by the server to advertise it supports CSI.
Feature,
"csi",
CSI
);
generate_empty_element!( use crate::ns;
/// Client indicates it is inactive.
Inactive,
"inactive",
CSI
);
generate_empty_element!( /// Stream:feature sent by the server to advertise it supports CSI.
/// Client indicates it is active again. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
Active, #[xml(namespace = ns::CSI, name = "csi")]
"active", pub struct Feature;
CSI
); /// 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)] #[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. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
RtcpMux, #[xml(namespace = ns::JINGLE_RTP, name = "rtcp-mux")]
"rtcp-mux", pub struct RtcpMux;
JINGLE_RTP
);
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. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
Leave, #[xml(namespace = ns::MIX_CORE, name = "leave")]
"leave", pub struct Leave;
MIX_CORE
);
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};
generate_empty_element!( use crate::iq::IqGetPayload;
/// Represents a ping to the recipient, which must be answered with an use crate::ns;
/// empty `<iq/>` or with an error.
Ping, /// Represents a ping to the recipient, which must be answered with an
"ping", /// empty `<iq/>` or with an error.
PING #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
); #[xml(namespace = ns::PING, name = "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};
generate_empty_element!( use crate::message::MessagePayload;
/// Requests that this message is acked by the final recipient once use crate::ns;
/// received.
Request, /// Requests that this message is acked by the final recipient once
"request", /// received.
RECEIPTS #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
); #[xml(namespace = ns::RECEIPTS, name = "request")]
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. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
Abort, #[xml(namespace = ns::SASL, name = "abort")]
"abort", pub struct Abort;
SASL
);
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. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
R, #[xml(namespace = ns::SM, name = "r")]
"r", pub struct R;
SM
);
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/>`. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
StreamManagement, #[xml(namespace = ns::SM, name = "sm")]
"sm", pub struct StreamManagement;
SM
);
#[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. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
TimeQuery, #[xml(namespace = ns::TIME, name = "time")]
"time", pub struct TimeQuery;
TIME
);
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};
generate_empty_element!( use crate::iq::{IqGetPayload, IqResultPayload};
/// Represents a query for the software version a remote entity is using. use crate::ns;
///
/// It should only be used in an `<iq type='get'/>`, as it can only /// Represents a query for the software version a remote entity is using.
/// represent the request, and not a result. ///
VersionQuery, /// It should only be used in an `<iq type='get'/>`, as it can only
"query", /// represent the request, and not a result.
VERSION #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
); #[xml(namespace = ns::VERSION, name = "query")]
pub struct VersionQuery;
impl IqGetPayload for VersionQuery {} impl IqGetPayload for VersionQuery {}