xml: clean up traits

This commit is contained in:
Lennart
2024-12-23 12:32:10 +01:00
parent 3e870bcbe6
commit b52a9f4fbe
3 changed files with 26 additions and 23 deletions

View File

@@ -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<R: BufRead>(reader: quick_xml::NsReader<R>) -> Result<Self, XmlDeError>;
#[inline]
fn parse_reader<R: BufRead>(input: R) -> Result<Self, XmlDeError>
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, XmlDeError> {
Self::parse_reader(s.as_bytes())
}
}
impl<T: XmlRoot + XmlDeserialize> XmlDocument for T {
fn parse<R: BufRead>(mut reader: quick_xml::NsReader<R>) -> Result<Self, XmlDeError>
where
Self: XmlDeserialize,
@@ -79,25 +103,4 @@ pub trait XmlRoot {
};
Err(XmlDeError::UnknownError)
}
fn parse_reader<R: BufRead>(input: R) -> Result<Self, XmlDeError>
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, XmlDeError> {
Self::parse_reader(s.as_bytes())
}
}
impl<T: XmlRoot + XmlDeserialize> XmlRootParseStr<'_> for T {}

View File

@@ -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() {

View File

@@ -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;