From b52a9f4fbe06254e49ddfef95138ce0a94004df0 Mon Sep 17 00:00:00 2001 From: Lennart <18233294+lennart-k@users.noreply.github.com> Date: Mon, 23 Dec 2024 12:32:10 +0100 Subject: [PATCH] xml: clean up traits --- crates/xml/src/de.rs | 45 +++++++++++++++++++---------------- crates/xml/tests/de_enum.rs | 2 +- crates/xml/tests/de_struct.rs | 2 +- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/crates/xml/src/de.rs b/crates/xml/src/de.rs index 87e14ca..586a1aa 100644 --- a/crates/xml/src/de.rs +++ b/crates/xml/src/de.rs @@ -44,6 +44,30 @@ pub trait XmlDeserialize: Sized { } pub trait XmlRoot { + fn root_tag() -> &'static [u8]; + fn root_ns() -> Option<&'static [u8]>; +} + +pub trait XmlDocument: XmlDeserialize { + fn parse(reader: quick_xml::NsReader) -> Result; + + #[inline] + fn parse_reader(input: R) -> Result + where + Self: XmlDeserialize, + { + let mut reader = quick_xml::NsReader::from_reader(input); + reader.config_mut().trim_text(true); + Self::parse(reader) + } + + #[inline] + fn parse_str(s: &str) -> Result { + Self::parse_reader(s.as_bytes()) + } +} + +impl XmlDocument for T { fn parse(mut reader: quick_xml::NsReader) -> Result where Self: XmlDeserialize, @@ -79,25 +103,4 @@ pub trait XmlRoot { }; Err(XmlDeError::UnknownError) } - - fn parse_reader(input: R) -> Result - where - Self: XmlDeserialize, - { - let mut reader = quick_xml::NsReader::from_reader(input); - reader.config_mut().trim_text(true); - Self::parse(reader) - } - - fn root_tag() -> &'static [u8]; - fn root_ns() -> Option<&'static [u8]>; } - -pub trait XmlRootParseStr<'i>: XmlRoot + XmlDeserialize { - #[inline] - fn parse_str(s: &'i str) -> Result { - Self::parse_reader(s.as_bytes()) - } -} - -impl XmlRootParseStr<'_> for T {} diff --git a/crates/xml/tests/de_enum.rs b/crates/xml/tests/de_enum.rs index e7634c3..ab62b43 100644 --- a/crates/xml/tests/de_enum.rs +++ b/crates/xml/tests/de_enum.rs @@ -1,4 +1,4 @@ -use rustical_xml::{de::XmlRootParseStr, Unit, XmlDeserialize, XmlRoot}; +use rustical_xml::{de::XmlDocument, Unit, XmlDeserialize, XmlRoot}; #[test] fn test_struct_tagged_enum() { diff --git a/crates/xml/tests/de_struct.rs b/crates/xml/tests/de_struct.rs index 6cbe542..881c88d 100644 --- a/crates/xml/tests/de_struct.rs +++ b/crates/xml/tests/de_struct.rs @@ -1,4 +1,4 @@ -use rustical_xml::de::XmlRootParseStr; +use rustical_xml::de::XmlDocument; use rustical_xml::XmlRoot; use rustical_xml::{Unit, Unparsed, XmlDeserialize}; use std::collections::HashSet;