From f2dcd55549abcffc7541a206ff1b4d0e83c53de5 Mon Sep 17 00:00:00 2001 From: Eric Coissac Date: Fri, 28 Nov 2025 18:33:03 +0100 Subject: [PATCH] =?UTF-8?q?On=20travaille=20sur=20la=20s=C3=A9rialisation?= =?UTF-8?q?=20XML.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 1 + pmoutils/Cargo.toml | 2 ++ pmoutils/src/lib.rs | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index d055bbec..11716945 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3433,6 +3433,7 @@ dependencies = [ "get_if_addrs", "netstat2", "os_info", + "quick-xml 0.38.4", "sysinfo", "users", ] diff --git a/pmoutils/Cargo.toml b/pmoutils/Cargo.toml index cd1380c2..49e86a59 100644 --- a/pmoutils/Cargo.toml +++ b/pmoutils/Cargo.toml @@ -9,3 +9,5 @@ os_info = "3.8" netstat2 = "0.11" sysinfo = "0.30" users = "0.11" +quick-xml = "0.38.3" +xmltree = "0.10" diff --git a/pmoutils/src/lib.rs b/pmoutils/src/lib.rs index 8169a51a..0e0a0279 100644 --- a/pmoutils/src/lib.rs +++ b/pmoutils/src/lib.rs @@ -20,6 +20,7 @@ pub mod ip_utils; pub use ip_utils::guess_local_ip; pub mod process; pub use process::{ProcessPortInfo, TransportProtocol, find_process_using_port}; +use xmltree::{Element, EmitterConfig}; /// Retourne une chaîne décrivant le système d'exploitation et sa version. /// @@ -52,3 +53,24 @@ pub fn get_os_string() -> String { format!("{}/Unknown", os_type) } } + +/// Trait générique pour obtenir un élément XML (xmltree::Element). +/// +/// Aligné sur la signature utilisée dans pmoupnp (UpnpObject::to_xml_element), +/// afin de pouvoir factoriser la sérialisation XML entre crates. +pub trait ToXmlElement { + /// Convertit l'objet en élément XML. + fn to_xml_element(&self) -> Element; + + /// Sérialise en chaîne XML formatée. + fn to_xml(&self) -> String { + let elem = self.to_xml_element(); + let config = EmitterConfig::new() + .perform_indent(true) + .indent_string(" "); + let mut buf = Vec::new(); + elem.write_with_config(&mut buf, config) + .expect("Failed to write XML"); + String::from_utf8(buf).expect("Invalid UTF-8") + } +}