xml: Some restructuring

This commit is contained in:
Lennart
2025-01-15 18:24:23 +01:00
parent d74f0ba660
commit d5c66ed233
9 changed files with 128 additions and 121 deletions

View File

@@ -1,9 +1,10 @@
use std::collections::HashMap;
use quick_xml::{events::attributes::Attribute, name::Namespace};
pub use xml_derive::XmlSerialize;
use crate::XmlRootTag;
use quick_xml::{
events::{attributes::Attribute, BytesStart, Event},
name::{Namespace, QName},
};
use std::collections::HashMap;
pub use xml_derive::XmlSerialize;
pub trait XmlSerialize {
fn serialize<W: std::io::Write>(
@@ -54,3 +55,42 @@ impl<T: XmlSerialize + XmlRootTag> XmlSerializeRoot for T {
self.serialize(Self::root_ns(), Some(Self::root_tag()), &namespaces, writer)
}
}
impl XmlSerialize for () {
fn serialize<W: std::io::Write>(
&self,
ns: Option<Namespace>,
tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> {
let prefix = ns
.map(|ns| namespaces.get(&ns))
.unwrap_or(None)
.map(|prefix| {
if !prefix.is_empty() {
[*prefix, b":"].concat()
} else {
Vec::new()
}
});
let has_prefix = prefix.is_some();
let tagname = tag.map(|tag| [&prefix.unwrap_or_default(), tag].concat());
let qname = tagname.as_ref().map(|tagname| QName(tagname));
if let Some(qname) = &qname {
let mut bytes_start = BytesStart::from(qname.to_owned());
if !has_prefix {
if let Some(ns) = &ns {
bytes_start.push_attribute((b"xmlns".as_ref(), ns.as_ref()));
}
}
writer.write_event(Event::Empty(bytes_start))?;
}
Ok(())
}
#[allow(refining_impl_trait)]
fn attributes<'a>(&self) -> Option<Vec<quick_xml::events::attributes::Attribute<'a>>> {
None
}
}