xml: Comprehensive refactoring from byte strings to strings

This commit is contained in:
Lennart
2025-08-28 18:01:41 +02:00
parent 85787e69bc
commit c4604d4376
37 changed files with 158 additions and 160 deletions

View File

@@ -49,7 +49,7 @@ impl<T: XmlRootTag + XmlDeserialize> XmlDocument for T {
let (ns, name) = reader.resolve_element(start.name());
let matches = match (Self::root_ns(), &ns, name) {
// Wrong tag
(_, _, name) if name.as_ref() != Self::root_tag() => false,
(_, _, name) if name.as_ref() != Self::root_tag().as_bytes() => false,
// Wrong namespace
(Some(root_ns), ns, _) if &ResolveResult::Bound(root_ns) != ns => false,
_ => true,
@@ -60,7 +60,7 @@ impl<T: XmlRootTag + XmlDeserialize> XmlDocument for T {
format!("{ns:?}"),
String::from_utf8_lossy(name.as_ref()).to_string(),
format!("{root_ns:?}"),
String::from_utf8_lossy(Self::root_tag()).to_string(),
Self::root_tag().to_owned(),
));
};

View File

@@ -23,9 +23,9 @@ pub use xml_derive::PropName;
pub use xml_derive::XmlRootTag;
pub trait XmlRootTag {
fn root_tag() -> &'static [u8];
fn root_tag() -> &'static str;
fn root_ns() -> Option<Namespace<'static>>;
fn root_ns_prefixes() -> HashMap<Namespace<'static>, &'static [u8]>;
fn root_ns_prefixes() -> HashMap<Namespace<'static>, &'static str>;
}
#[derive(Debug)]

View File

@@ -28,7 +28,7 @@ impl<'a> From<&'a Namespace<'a>> for NamespaceOwned {
}
impl NamespaceOwned {
pub fn as_ref(&self) -> Namespace {
pub fn as_ref(&self) -> Namespace<'_> {
Namespace(&self.0)
}
}

View File

@@ -10,8 +10,8 @@ pub trait XmlSerialize {
fn serialize(
&self,
ns: Option<Namespace>,
tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
tag: Option<&str>,
namespaces: &HashMap<Namespace, &str>,
writer: &mut quick_xml::Writer<&mut Vec<u8>>,
) -> std::io::Result<()>;
@@ -22,8 +22,8 @@ impl<T: XmlSerialize> XmlSerialize for Option<T> {
fn serialize(
&self,
ns: Option<Namespace>,
tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
tag: Option<&str>,
namespaces: &HashMap<Namespace, &str>,
writer: &mut quick_xml::Writer<&mut Vec<u8>>,
) -> std::io::Result<()> {
if let Some(some) = self {
@@ -60,8 +60,8 @@ impl XmlSerialize for () {
fn serialize(
&self,
ns: Option<Namespace>,
tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
tag: Option<&str>,
namespaces: &HashMap<Namespace, &str>,
writer: &mut quick_xml::Writer<&mut Vec<u8>>,
) -> std::io::Result<()> {
let prefix = ns
@@ -69,14 +69,14 @@ impl XmlSerialize for () {
.unwrap_or(None)
.map(|prefix| {
if !prefix.is_empty() {
[*prefix, b":"].concat()
[*prefix, ":"].concat()
} else {
Vec::new()
String::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));
let qname = tagname.as_ref().map(|tagname| QName(tagname.as_bytes()));
if let Some(qname) = &qname {
let mut bytes_start = BytesStart::from(qname.to_owned());
if !has_prefix && let Some(ns) = &ns {

View File

@@ -107,8 +107,8 @@ impl<T: ValueSerialize> XmlSerialize for T {
fn serialize(
&self,
ns: Option<Namespace>,
tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
tag: Option<&str>,
namespaces: &HashMap<Namespace, &str>,
writer: &mut quick_xml::Writer<&mut Vec<u8>>,
) -> std::io::Result<()> {
let prefix = ns
@@ -116,20 +116,18 @@ impl<T: ValueSerialize> XmlSerialize for T {
.unwrap_or(None)
.map(|prefix| {
if !prefix.is_empty() {
[*prefix, b":"].concat()
[*prefix, ":"].concat()
} else {
Vec::new()
String::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));
let qname = tagname.as_ref().map(|tagname| QName(tagname.as_bytes()));
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()));
}
if !has_prefix && let Some(ns) = &ns {
bytes_start.push_attribute((b"xmlns".as_ref(), ns.as_ref()));
}
writer.write_event(Event::Start(bytes_start))?;
}