xml: namespace serialization

This commit is contained in:
Lennart
2024-12-31 18:20:02 +01:00
parent 61e2dab37f
commit 098e374e4d
20 changed files with 215 additions and 133 deletions

View File

@@ -54,11 +54,7 @@ impl<T: XmlRootTag + XmlDeserialize> XmlDocument for T {
// Wrong tag
(_, _, name) if name.as_ref() != Self::root_tag() => false,
// Wrong namespace
(Some(root_ns), ns, _)
if &ResolveResult::Bound(Namespace(root_ns)) != ns =>
{
false
}
(Some(root_ns), ns, _) if &ResolveResult::Bound(root_ns) != ns => false,
_ => true,
};
if !matches {

View File

@@ -1,5 +1,6 @@
use quick_xml::events::BytesEnd;
use quick_xml::events::{BytesStart, Event};
use quick_xml::name::{Namespace, QName};
use std::collections::HashMap;
use std::io::BufRead;
pub mod de;
@@ -38,14 +39,26 @@ impl XmlDeserialize for () {
impl XmlSerialize for () {
fn serialize<W: std::io::Write>(
&self,
ns: Option<&[u8]>,
ns: Option<Namespace>,
tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> {
let tag_str = tag.map(String::from_utf8_lossy);
if let Some(tag) = &tag_str {
writer.write_event(Event::Empty(BytesStart::new(tag.clone())))?;
let prefix = ns
.map(|ns| namespaces.get(&ns))
.unwrap_or(None)
.map(|prefix| [*prefix, b":"].concat());
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(())
}
@@ -84,5 +97,5 @@ impl XmlDeserialize for Unparsed {
pub trait XmlRootTag {
fn root_tag() -> &'static [u8];
fn root_ns() -> Option<&'static [u8]>;
fn root_ns() -> Option<Namespace<'static>>;
}

View File

@@ -1,4 +1,6 @@
use quick_xml::events::attributes::Attribute;
use std::collections::HashMap;
use quick_xml::{events::attributes::Attribute, name::Namespace};
pub use xml_derive::XmlSerialize;
use crate::XmlRootTag;
@@ -6,8 +8,9 @@ use crate::XmlRootTag;
pub trait XmlSerialize {
fn serialize<W: std::io::Write>(
&self,
ns: Option<&[u8]>,
ns: Option<Namespace>,
tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()>;
@@ -17,12 +20,13 @@ pub trait XmlSerialize {
impl<T: XmlSerialize> XmlSerialize for Option<T> {
fn serialize<W: std::io::Write>(
&self,
ns: Option<&[u8]>,
ns: Option<Namespace>,
tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> {
if let Some(some) = self {
some.serialize(ns, tag, writer)
some.serialize(ns, tag, namespaces, writer)
} else {
Ok(())
}
@@ -46,6 +50,7 @@ impl<T: XmlSerialize + XmlRootTag> XmlSerializeRoot for T {
&self,
writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> {
self.serialize(Self::root_ns(), Some(Self::root_tag()), writer)
let namespaces = HashMap::new();
self.serialize(Self::root_ns(), Some(Self::root_tag()), &namespaces, writer)
}
}

View File

@@ -1,4 +1,6 @@
use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
use quick_xml::name::{Namespace, QName};
use std::collections::HashMap;
use std::num::{ParseFloatError, ParseIntError};
use std::{convert::Infallible, io::BufRead};
use thiserror::Error;
@@ -93,19 +95,30 @@ impl<T: Value> XmlDeserialize for T {
impl<T: Value> XmlSerialize for T {
fn serialize<W: std::io::Write>(
&self,
ns: Option<&[u8]>,
ns: Option<Namespace>,
tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> {
let tag_str = tag.map(String::from_utf8_lossy);
if let Some(tag) = &tag_str {
writer.write_event(Event::Start(BytesStart::new(tag.clone())))?;
let prefix = ns
.map(|ns| namespaces.get(&ns))
.unwrap_or(None)
.map(|prefix| [*prefix, b":"].concat());
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::Start(bytes_start))?;
}
writer.write_event(Event::Text(BytesText::new(&self.serialize())))?;
if let Some(tag) = &tag_str {
writer.write_event(Event::End(BytesEnd::new(tag.clone())))?;
if let Some(qname) = &qname {
writer.write_event(Event::End(BytesEnd::from(qname.to_owned())))?;
}
Ok(())
}