mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
xml: Add namespace deserialisation
This commit is contained in:
@@ -246,6 +246,25 @@ impl Field {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn namespace_branch(&self) -> Option<proc_macro2::TokenStream> {
|
||||
if self.attrs.xml_ty != FieldType::Namespace {
|
||||
return None;
|
||||
}
|
||||
let builder_field_ident = self.builder_field_ident();
|
||||
|
||||
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()))?)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
Some(quote! {
|
||||
builder.#builder_field_ident = #value;
|
||||
})
|
||||
}
|
||||
|
||||
pub fn tagname_branch(&self) -> Option<proc_macro2::TokenStream> {
|
||||
if self.attrs.xml_ty != FieldType::TagName {
|
||||
return None;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::attrs::{FieldType, StructAttrs};
|
||||
use crate::Field;
|
||||
use crate::attrs::{FieldType, StructAttrs};
|
||||
use core::panic;
|
||||
use darling::FromDeriveInput;
|
||||
use quote::quote;
|
||||
@@ -115,6 +115,7 @@ impl NamedStruct {
|
||||
self.fields.iter().filter_map(Field::text_branch).collect();
|
||||
let attr_field_branches = self.fields.iter().filter_map(Field::attr_branch);
|
||||
let tagname_field_branches = self.fields.iter().filter_map(Field::tagname_branch);
|
||||
let namespace_field_branches = self.fields.iter().filter_map(Field::namespace_branch);
|
||||
|
||||
let builder_field_builds = self.fields.iter().map(Field::builder_field_build);
|
||||
|
||||
@@ -144,6 +145,7 @@ impl NamedStruct {
|
||||
|
||||
let (ns, name) = reader.resolve_element(start.name());
|
||||
#(#tagname_field_branches);*
|
||||
#(#namespace_field_branches);*
|
||||
|
||||
for attr in start.attributes() {
|
||||
let attr = attr?;
|
||||
|
||||
@@ -342,3 +342,32 @@ fn test_struct_tuple() {
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_struct_untagged_ns() {
|
||||
#[derive(Debug, XmlDeserialize, XmlRootTag, PartialEq)]
|
||||
#[xml(root = b"document")]
|
||||
struct Document {
|
||||
#[xml(ty = "untagged")]
|
||||
child: Child,
|
||||
}
|
||||
|
||||
#[derive(Debug, XmlDeserialize, PartialEq, Default)]
|
||||
struct Child(
|
||||
#[xml(ty = "tag_name")] String,
|
||||
#[xml(ty = "namespace")] Option<String>,
|
||||
);
|
||||
|
||||
let doc = Document::parse_str(
|
||||
r#"
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<document><test xmlns="hello" /></document>"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
doc,
|
||||
Document {
|
||||
child: Child("test".to_owned(), Some("hello".to_string()))
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user