From 0ef8c215d76b9dedf84c1af483d501075b448816 Mon Sep 17 00:00:00 2001 From: Link Mauve Date: Mon, 8 Jun 2026 17:30:06 +0200 Subject: [PATCH] xmpp-parsers: Move the Stanza type from tokio-xmpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It doesn’t come with the ensure_id() method, but otherwise this is a much better location for it. --- parsers/ChangeLog | 2 ++ parsers/src/lib.rs | 2 ++ parsers/src/stanza.rs | 81 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 parsers/src/stanza.rs diff --git a/parsers/ChangeLog b/parsers/ChangeLog index 81c4d33c..ca69c307 100644 --- a/parsers/ChangeLog +++ b/parsers/ChangeLog @@ -24,6 +24,8 @@ XXXX-YY-ZZ RELEASER not be rejected by xmpp-parsers by default anymore. The `pedantic` feature can be used to opt into the previous default behaviour. * Improvements: + - Take the Stanza type from tokio-xmpp, it’s an enum which can wrap + Message, Presence and Iq. - Make Priority’s inner i8 pub, which had been broken since the conversion to xso. (!632) - ibr::LegacyQuery now implements Default, allowing users to more diff --git a/parsers/src/lib.rs b/parsers/src/lib.rs index fc98de01..79c568b8 100644 --- a/parsers/src/lib.rs +++ b/parsers/src/lib.rs @@ -64,6 +64,8 @@ pub mod presence; /// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core pub mod sasl; /// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core +pub mod stanza; +/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core pub mod stanza_error; /// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core pub mod starttls; diff --git a/parsers/src/stanza.rs b/parsers/src/stanza.rs new file mode 100644 index 00000000..090c7549 --- /dev/null +++ b/parsers/src/stanza.rs @@ -0,0 +1,81 @@ +// Copyright (c) 2024 Jonas Schäfer +// +// 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::{AsXml, FromXml}; + +use crate::{iq::Iq, message::Message, presence::Presence}; + +/// A stanza sent/received over the stream. +/// +/// WARNING: do not add variants to this enum! Adding variants which refer +/// to anything but [`Iq`], [`Message`] or [`Presence`] stanzas will cause the +/// stream management counters to be off. +#[derive(FromXml, AsXml, Debug, PartialEq)] +#[xml()] +pub enum Stanza { + /// IQ stanza + #[xml(transparent)] + Iq(Iq), + + /// Message stanza + #[xml(transparent)] + Message(Message), + + /// Presence stanza + #[xml(transparent)] + Presence(Presence), +} + +impl From for Stanza { + fn from(other: Iq) -> Self { + Self::Iq(other) + } +} + +impl From for Stanza { + fn from(other: Presence) -> Self { + Self::Presence(other) + } +} + +impl From for Stanza { + fn from(other: Message) -> Self { + Self::Message(other) + } +} + +impl TryFrom for Message { + type Error = Stanza; + + fn try_from(other: Stanza) -> Result { + match other { + Stanza::Message(st) => Ok(st), + other => Err(other), + } + } +} + +impl TryFrom for Presence { + type Error = Stanza; + + fn try_from(other: Stanza) -> Result { + match other { + Stanza::Presence(st) => Ok(st), + other => Err(other), + } + } +} + +impl TryFrom for Iq { + type Error = Stanza; + + fn try_from(other: Stanza) -> Result { + match other { + Stanza::Iq(st) => Ok(st), + other => Err(other), + } + } +}