mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 11:42:25 +00:00
xml: Strict namespace, some tests and restructuring
This commit is contained in:
@@ -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)]
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user