xml: Implement proper NamespaceOwned type

This commit is contained in:
Lennart
2025-05-14 20:18:45 +02:00
parent 1436af1f9c
commit 212274fce9
8 changed files with 53 additions and 15 deletions

View File

@@ -254,7 +254,8 @@ impl Field {
let value = quote! {
if let ::quick_xml::name::ResolveResult::Bound(ns) = &ns {
Some(rustical_xml::ValueDeserialize::deserialize(&String::from_utf8_lossy(ns.0.as_ref()))?)
Some(ns.into())
// Some(rustical_xml::ValueDeserialize::deserialize(&String::from_utf8_lossy(ns.0.as_ref()))?)
} else {
None
}

View File

@@ -4,6 +4,7 @@ use std::str::FromStr;
pub mod de;
mod error;
mod namespace;
pub mod se;
mod unparsed;
mod value;
@@ -11,6 +12,7 @@ mod value;
pub use de::XmlDeserialize;
pub use de::XmlDocument;
pub use error::XmlError;
pub use namespace::NamespaceOwned;
pub use se::XmlSerialize;
pub use se::XmlSerializeRoot;
pub use unparsed::Unparsed;

View File

@@ -0,0 +1,34 @@
use quick_xml::name::Namespace;
#[derive(Debug, Clone, Default, PartialEq)]
pub struct NamespaceOwned(pub Vec<u8>);
impl<'a> From<Namespace<'a>> for NamespaceOwned {
fn from(value: Namespace<'a>) -> Self {
Self(value.0.to_vec())
}
}
impl From<String> for NamespaceOwned {
fn from(value: String) -> Self {
Self(value.into_bytes())
}
}
impl From<&str> for NamespaceOwned {
fn from(value: &str) -> Self {
Self(value.as_bytes().to_vec())
}
}
impl<'a> From<&'a Namespace<'a>> for NamespaceOwned {
fn from(value: &'a Namespace<'a>) -> Self {
Self(value.0.to_vec())
}
}
impl NamespaceOwned {
pub fn as_ref(&self) -> Namespace {
Namespace(&self.0)
}
}

View File

@@ -1,6 +1,6 @@
use quick_xml::name::Namespace;
use rustical_xml::de::XmlDocument;
use rustical_xml::{Unparsed, XmlDeserialize, XmlRootTag};
use rustical_xml::{NamespaceOwned, Unparsed, XmlDeserialize, XmlRootTag};
use std::collections::HashSet;
#[test]
@@ -355,7 +355,7 @@ fn test_struct_untagged_ns() {
#[derive(Debug, XmlDeserialize, PartialEq, Default)]
struct Child(
#[xml(ty = "tag_name")] String,
#[xml(ty = "namespace")] Option<String>,
#[xml(ty = "namespace")] Option<NamespaceOwned>,
);
let doc = Document::parse_str(
@@ -367,7 +367,7 @@ fn test_struct_untagged_ns() {
assert_eq!(
doc,
Document {
child: Child("test".to_owned(), Some("hello".to_string()))
child: Child("test".to_owned(), Some("hello".to_string().into()))
}
);
}