From 6b7195530b1bb5abc7c5362417e2209e2bcc71f8 Mon Sep 17 00:00:00 2001 From: Link Mauve Date: Mon, 8 Jun 2026 18:37:16 +0200 Subject: [PATCH] tokio-xmpp: Use Stanza from xmpp-parsers The previous commit moved it from src/event.rs into xmpp-parsers, so now we can use that type directly. --- tokio-xmpp/src/client/mod.rs | 4 +- tokio-xmpp/src/component/mod.rs | 3 +- tokio-xmpp/src/event.rs | 102 ++++---------------------------- tokio-xmpp/src/lib.rs | 5 +- 4 files changed, 19 insertions(+), 95 deletions(-) diff --git a/tokio-xmpp/src/client/mod.rs b/tokio-xmpp/src/client/mod.rs index 4974e216..8ba65967 100644 --- a/tokio-xmpp/src/client/mod.rs +++ b/tokio-xmpp/src/client/mod.rs @@ -7,7 +7,7 @@ use crate::client::{receiver::ClientReceiver, sender::ClientSender}; use crate::connect::ServerConnector; use crate::error::Error; -use crate::event::Event; +use crate::event::{ensure_stanza_id, Event}; use crate::stanzastream::{self, StanzaStage, StanzaState, StanzaStream, StanzaToken}; use crate::xmlstream::Timeouts; use crate::Stanza; @@ -85,7 +85,7 @@ impl Client { /// For sending Iq request stanzas, it is recommended to use /// [`send_iq`][`Self::send_iq`], which allows awaiting the response. pub async fn send_stanza(&mut self, mut stanza: Stanza) -> Result { - stanza.ensure_id(); + ensure_stanza_id(&mut stanza); let mut token = self.stream_tx.send(Box::new(stanza)).await; match token.wait_for(StanzaStage::Sent).await { diff --git a/tokio-xmpp/src/component/mod.rs b/tokio-xmpp/src/component/mod.rs index 27bb232e..33ebe94d 100644 --- a/tokio-xmpp/src/component/mod.rs +++ b/tokio-xmpp/src/component/mod.rs @@ -8,6 +8,7 @@ use xmpp_parsers::jid::Jid; use crate::{ component::login::component_login, connect::ServerConnector, + event::ensure_stanza_id, xmlstream::{Timeouts, XmppStream}, Error, Stanza, }; @@ -33,7 +34,7 @@ pub struct Component { impl Component { /// Send stanza pub async fn send_stanza(&mut self, mut stanza: Stanza) -> Result<(), Error> { - stanza.ensure_id(); + ensure_stanza_id(&mut stanza); self.send(stanza).await } diff --git a/tokio-xmpp/src/event.rs b/tokio-xmpp/src/event.rs index da9d8eca..995b9a78 100644 --- a/tokio-xmpp/src/event.rs +++ b/tokio-xmpp/src/event.rs @@ -4,14 +4,7 @@ // 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 xmpp_parsers::{ - iq::Iq, - jid::Jid, - message::{Id, Message}, - presence::Presence, - stream_features::StreamFeatures, -}; -use xso::{AsXml, FromXml}; +use xmpp_parsers::{jid::Jid, message::Id, stanza::Stanza, stream_features::StreamFeatures}; use crate::xmlstream::XmppStreamElement; use crate::Error; @@ -21,91 +14,18 @@ pub(crate) fn make_id() -> String { format!("{}", id) } -/// 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 Stanza { - /// Assign a random ID to the stanza, if no ID has been assigned yet. - pub fn ensure_id(&mut self) -> &str { - match self { - Self::Iq(iq) => { - let id = iq.id_mut(); - if id.is_empty() { - *id = make_id(); - } - id +/// Assign a random ID to the stanza, if no ID has been assigned yet. +pub fn ensure_stanza_id(stanza: &mut Stanza) -> &str { + match stanza { + Stanza::Iq(iq) => { + let id = iq.id_mut(); + if id.is_empty() { + *id = make_id(); } - Self::Message(message) => message.id.get_or_insert_with(|| Id(make_id())).0.as_ref(), - Self::Presence(presence) => presence.id.get_or_insert_with(make_id), - } - } -} - -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), + id } + Stanza::Message(message) => message.id.get_or_insert_with(|| Id(make_id())).0.as_ref(), + Stanza::Presence(presence) => presence.id.get_or_insert_with(make_id), } } diff --git a/tokio-xmpp/src/lib.rs b/tokio-xmpp/src/lib.rs index b7599339..58de281d 100644 --- a/tokio-xmpp/src/lib.rs +++ b/tokio-xmpp/src/lib.rs @@ -135,7 +135,10 @@ pub use client::{ #[cfg(feature = "insecure-tcp")] pub use component::Component; -pub use event::{Event, Stanza}; +pub use event::Event; + +/// Deprecated reexport of Stanza from xmpp-parsers. +pub use xmpp_parsers::stanza::Stanza; #[cfg(test)] mod tests {