From b1f4c5521da9c763cb615b07e5c7ec47449caef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Tue, 12 Nov 2024 10:55:36 +0100 Subject: [PATCH] xso: Add PrintRawXml helper struct with Display MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a helper struct to be able to display Raw Xml, useful for debug logs. Signed-off-by: Maxime “pep” Buquet --- tokio-xmpp/src/event.rs | 7 ++++++- tokio-xmpp/src/lib.rs | 3 +++ xso/Cargo.toml | 1 + xso/src/asxml.rs | 28 ++++++++++++++++++++++++++++ xso/src/lib.rs | 2 ++ 5 files changed, 40 insertions(+), 1 deletion(-) diff --git a/tokio-xmpp/src/event.rs b/tokio-xmpp/src/event.rs index 58d5b26d..296428ff 100644 --- a/tokio-xmpp/src/event.rs +++ b/tokio-xmpp/src/event.rs @@ -6,6 +6,7 @@ use rand::{thread_rng, Rng}; use xmpp_parsers::{iq::Iq, jid::Jid, message::Message, presence::Presence}; +use xso::AsXml; use crate::xmlstream::XmppStreamElement; use crate::Error; @@ -16,15 +17,19 @@ fn make_id() -> String { } /// A stanza sent/received over the stream. -#[derive(Debug)] +#[derive(AsXml, Debug)] +#[xml()] pub enum Stanza { /// IQ stanza + #[xml(transparent)] Iq(Iq), /// Message stanza + #[xml(transparent)] Message(Message), /// Presence stanza + #[xml(transparent)] Presence(Presence), } diff --git a/tokio-xmpp/src/lib.rs b/tokio-xmpp/src/lib.rs index 5b00e42f..1c9200bb 100644 --- a/tokio-xmpp/src/lib.rs +++ b/tokio-xmpp/src/lib.rs @@ -70,3 +70,6 @@ pub use crate::error::Error; pub use minidom; pub use xmpp_parsers as parsers; pub use xmpp_parsers::jid; + +// Re-export for debug purposes +pub use xso::asxml::PrintRawXml; diff --git a/xso/Cargo.toml b/xso/Cargo.toml index 17083b25..8302e3da 100644 --- a/xso/Cargo.toml +++ b/xso/Cargo.toml @@ -10,6 +10,7 @@ categories = ["encoding"] license = "MPL-2.0" [dependencies] +bytes = { version = "1" } rxml = { version = "0.12.0", default-features = false } minidom = { version = "0.16", path = "../minidom" } xso_proc = { version = "0.1", path = "../xso-proc", optional = true } diff --git a/xso/src/asxml.rs b/xso/src/asxml.rs index 3b4e449e..8e3ef3d9 100644 --- a/xso/src/asxml.rs +++ b/xso/src/asxml.rs @@ -16,6 +16,10 @@ use crate::error::Error; use crate::rxml_util::Item; use crate::AsXml; +use core::fmt; + +use bytes::BytesMut; + /// Helper iterator to convert an `Option` to XML. pub struct OptionAsXml(Option); @@ -86,6 +90,30 @@ where } } +/// Provides a helper which implements Display printing raw XML +pub struct PrintRawXml<'x, T>(pub &'x T); + +impl<'x, T: AsXml> fmt::Display for PrintRawXml<'x, T> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let iter = match self.0.as_xml_iter() { + Ok(iter) => iter, + Err(err) => return write!(f, "", err), + }; + let mut writer = rxml::writer::Encoder::new(); + let mut buf = BytesMut::new(); + for item in iter { + let item = match item { + Ok(item) => item, + Err(err) => return write!(f, "", err), + }; + if let Err(err) = writer.encode(item.as_rxml_item(), &mut buf) { + return write!(f, "", err); + } + } + write!(f, "{:?}", buf) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/xso/src/lib.rs b/xso/src/lib.rs index 79c7d1c6..ba944f48 100644 --- a/xso/src/lib.rs +++ b/xso/src/lib.rs @@ -47,6 +47,8 @@ pub use text::TextCodec; #[doc(inline)] pub use rxml_util::Item; +pub use asxml::PrintRawXml; + #[doc = include_str!("from_xml_doc.md")] #[doc(inline)] #[cfg(feature = "macros")]