From b89d3ab04daac22da2ec91aec822e3a216f6bb1e 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/lib.rs | 3 +++ xso/Cargo.toml | 1 + xso/src/asxml.rs | 30 ++++++++++++++++++++++++++++++ xso/src/lib.rs | 2 ++ 4 files changed, 36 insertions(+) diff --git a/tokio-xmpp/src/lib.rs b/tokio-xmpp/src/lib.rs index a08d11453beb603bbe6927abb00052799765b0b5..cd9f3aec10b9f663e7caad7a5e41d3307e88efdb 100644 --- a/tokio-xmpp/src/lib.rs +++ b/tokio-xmpp/src/lib.rs @@ -81,3 +81,6 @@ mod tests { use crate::parsers; } } + +// Re-export for debug purposes +pub use xso::asxml::PrintRawXml; diff --git a/xso/Cargo.toml b/xso/Cargo.toml index 4a0e5f36f7f123fe3bfe4e807b1ab238cf4c4981..afc7dfbbe2d14fcee8be365dae07d3be840af37e 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 5c13759670c8d0a3e817adf8e00f9ac62c8f848a..139bb3ce49dd42e9f43adfcebbb5abbd0e828392 100644 --- a/xso/src/asxml.rs +++ b/xso/src/asxml.rs @@ -18,6 +18,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); @@ -97,6 +101,32 @@ 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); + } + } + // TODO: rxml guarantees us that we have utf8 here. This unwrap can nonetheless be removed + // if Write is implemented for rxml. + write!(f, "{}", std::str::from_utf8(&buf).unwrap()) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/xso/src/lib.rs b/xso/src/lib.rs index b67efeb8e73d61ef81ec040b74fa5bb27dae902c..f24cc6fe3ea74675fb4aeefd5883aad46b4aaac3 100644 --- a/xso/src/lib.rs +++ b/xso/src/lib.rs @@ -87,6 +87,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")]