mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 16:32:29 +00:00
xml: clean up traits
This commit is contained in:
@@ -44,6 +44,30 @@ pub trait XmlDeserialize: Sized {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait XmlRoot {
|
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>
|
fn parse<R: BufRead>(mut reader: quick_xml::NsReader<R>) -> Result<Self, XmlDeError>
|
||||||
where
|
where
|
||||||
Self: XmlDeserialize,
|
Self: XmlDeserialize,
|
||||||
@@ -79,25 +103,4 @@ pub trait XmlRoot {
|
|||||||
};
|
};
|
||||||
Err(XmlDeError::UnknownError)
|
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 {}
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use rustical_xml::{de::XmlRootParseStr, Unit, XmlDeserialize, XmlRoot};
|
use rustical_xml::{de::XmlDocument, Unit, XmlDeserialize, XmlRoot};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_struct_tagged_enum() {
|
fn test_struct_tagged_enum() {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use rustical_xml::de::XmlRootParseStr;
|
use rustical_xml::de::XmlDocument;
|
||||||
use rustical_xml::XmlRoot;
|
use rustical_xml::XmlRoot;
|
||||||
use rustical_xml::{Unit, Unparsed, XmlDeserialize};
|
use rustical_xml::{Unit, Unparsed, XmlDeserialize};
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|||||||
Reference in New Issue
Block a user