xml serialize: Support flatten and Option<T>

This commit is contained in:
Lennart
2024-12-27 15:34:22 +01:00
parent c787a6e8f3
commit 9242557741
7 changed files with 82 additions and 18 deletions

View File

@@ -51,8 +51,8 @@ impl XmlSerialize for () {
}
#[allow(refining_impl_trait)]
fn attributes<'a>(&self) -> Vec<quick_xml::events::attributes::Attribute<'a>> {
vec![]
fn attributes<'a>(&self) -> Option<Vec<quick_xml::events::attributes::Attribute<'a>>> {
None
}
}

View File

@@ -11,7 +11,27 @@ pub trait XmlSerialize {
writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()>;
fn attributes<'a>(&self) -> impl IntoIterator<Item: Into<Attribute<'a>>>;
fn attributes<'a>(&self) -> Option<impl IntoIterator<Item: Into<Attribute<'a>>>>;
}
impl<T: XmlSerialize> XmlSerialize for Option<T> {
fn serialize<W: std::io::Write>(
&self,
ns: Option<&[u8]>,
tag: Option<&[u8]>,
writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> {
if let Some(some) = self {
some.serialize(ns, tag, writer)
} else {
Ok(())
}
}
#[allow(refining_impl_trait)]
fn attributes<'a>(&self) -> Option<Vec<Attribute<'a>>> {
None
}
}
pub trait XmlSerializeRoot {

View File

@@ -101,7 +101,7 @@ impl<T: Value> XmlSerialize for T {
}
#[allow(refining_impl_trait)]
fn attributes<'a>(&self) -> Vec<quick_xml::events::attributes::Attribute<'a>> {
vec![]
fn attributes<'a>(&self) -> Option<Vec<quick_xml::events::attributes::Attribute<'a>>> {
None
}
}