xml: Strict namespace, some tests and restructuring

This commit is contained in:
Lennart
2025-01-15 19:12:54 +01:00
parent d021e7b8bf
commit 3e0571bb72
15 changed files with 176 additions and 157 deletions

View File

@@ -3,15 +3,9 @@ use std::collections::HashMap;
use darling::{util::Flag, FromDeriveInput, FromField, FromMeta, FromVariant};
use syn::LitByteStr;
#[derive(Default, FromMeta, Clone)]
pub struct ContainerAttrs {
pub ns_strict: Flag,
}
#[derive(Default, FromMeta, Clone)]
pub struct TagAttrs {
pub rename: Option<LitByteStr>,
pub ns_strict: Flag,
pub ns: Option<syn::Path>,
}
@@ -27,18 +21,12 @@ pub struct VariantAttrs {
#[derive(Default, FromDeriveInput, Clone)]
#[darling(attributes(xml))]
pub struct EnumAttrs {
#[darling(flatten)]
// TODO: implement ns_strict
pub _container: ContainerAttrs,
pub untagged: Flag,
}
#[derive(Default, FromDeriveInput, Clone)]
#[darling(attributes(xml))]
pub struct StructAttrs {
#[darling(flatten)]
pub container: ContainerAttrs,
pub root: Option<LitByteStr>,
pub ns: Option<syn::Path>,
#[darling(default)]

View File

@@ -1,5 +1,5 @@
use super::{
attrs::{ContainerAttrs, FieldAttrs, FieldType},
attrs::{FieldAttrs, FieldType},
get_generic_type,
};
use darling::FromField;
@@ -23,20 +23,14 @@ pub struct Field {
pub field: syn::Field,
pub field_num: usize,
pub attrs: FieldAttrs,
pub container_attrs: ContainerAttrs,
}
impl Field {
pub fn from_syn_field(
field: syn::Field,
field_num: usize,
container_attrs: ContainerAttrs,
) -> Self {
pub fn from_syn_field(field: syn::Field, field_num: usize) -> Self {
Self {
attrs: FieldAttrs::from_field(&field).unwrap(),
field,
field_num,
container_attrs,
}
}
@@ -51,11 +45,6 @@ impl Field {
})
}
/// Whether to enforce the correct XML namespace
pub fn ns_strict(&self) -> bool {
self.attrs.common.ns_strict.is_present() || self.container_attrs.ns_strict.is_present()
}
/// Field identifier
pub fn field_ident(&self) -> &Option<syn::Ident> {
&self.field.ident
@@ -169,25 +158,18 @@ impl Field {
return None;
}
let namespace_match = if self.ns_strict() {
if self.attrs.common.ns.is_some() {
quote! {quick_xml::name::ResolveResult::Bound(ns)}
} else {
quote! {quick_xml::name::ResolveResult::Unbound}
}
let namespace_match = if self.attrs.common.ns.is_some() {
quote! {quick_xml::name::ResolveResult::Bound(ns)}
} else {
quote! {_}
quote! {quick_xml::name::ResolveResult::Unbound}
};
let namespace_condition = if self.ns_strict() {
self.attrs
.common
.ns
.as_ref()
.map(|ns| quote! { if ns == #ns })
} else {
None
};
let namespace_condition = self
.attrs
.common
.ns
.as_ref()
.map(|ns| quote! { if ns == #ns });
let field_name = self.xml_name();
let builder_field_ident = self.builder_field_ident();

View File

@@ -25,9 +25,7 @@ impl NamedStruct {
.named
.iter()
.enumerate()
.map(|(i, field)| {
Field::from_syn_field(field.to_owned(), i, attrs.container.clone())
})
.map(|(i, field)| Field::from_syn_field(field.to_owned(), i))
.collect(),
attrs,
ident: input.ident.to_owned(),
@@ -38,9 +36,7 @@ impl NamedStruct {
.unnamed
.iter()
.enumerate()
.map(|(i, field)| {
Field::from_syn_field(field.to_owned(), i, attrs.container.clone())
})
.map(|(i, field)| Field::from_syn_field(field.to_owned(), i))
.collect(),
attrs,
ident: input.ident.to_owned(),

View File

@@ -81,7 +81,7 @@ fn test_tagged_enum_complex() {
nice: (),
}
let asd = Propfind::parse_str(
let _ = Propfind::parse_str(
r#"
<propfind>
<prop>

View File

@@ -154,7 +154,7 @@ fn test_struct_ns() {
const NS_HELLO: Namespace = Namespace(b"hello");
#[derive(Debug, XmlDeserialize, XmlRootTag, PartialEq)]
#[xml(root = b"document", ns_strict)]
#[xml(root = b"document")]
struct Document {
#[xml(ns = "NS_HELLO")]
child: (),
@@ -169,7 +169,7 @@ fn test_struct_attr() {
const NS_HELLO: Namespace = Namespace(b"hello");
#[derive(Debug, XmlDeserialize, XmlRootTag, PartialEq)]
#[xml(root = b"document", ns_strict)]
#[xml(root = b"document")]
struct Document {
#[xml(ns = "NS_HELLO")]
child: (),
@@ -196,12 +196,12 @@ fn test_struct_attr() {
#[test]
fn test_struct_generics() {
#[derive(XmlDeserialize, XmlRootTag)]
#[xml(root = b"document", ns_strict)]
#[xml(root = b"document")]
struct Document<T: XmlDeserialize> {
child: T,
}
let doc = Document::<Unparsed>::parse_str(
let _ = Document::<Unparsed>::parse_str(
r#"
<document>
<child>
@@ -216,12 +216,12 @@ fn test_struct_generics() {
#[test]
fn test_struct_unparsed() {
#[derive(XmlDeserialize, XmlRootTag)]
#[xml(root = b"document", ns_strict)]
#[xml(root = b"document")]
struct Document {
child: Unparsed,
}
let doc = Document::parse_str(
let _ = Document::parse_str(
r#"
<document>
<child>
@@ -236,7 +236,7 @@ fn test_struct_unparsed() {
#[test]
fn test_xml_values() {
#[derive(XmlDeserialize, XmlRootTag, PartialEq, Debug)]
#[xml(root = b"document", ns_strict)]
#[xml(root = b"document")]
struct Document {
href: String,
}