xso: implement catch-all for unknown elements
This commit is contained in:
parent
2103ef0191
commit
0361b5905b
10 changed files with 412 additions and 48 deletions
|
|
@ -23,7 +23,7 @@ use crate::types::{
|
|||
option_as_xml_ty, option_ty, ref_ty, ty_from_ident,
|
||||
};
|
||||
|
||||
use super::{Field, FieldBuilderPart, FieldIteratorPart, FieldTempInit};
|
||||
use super::{Field, FieldBuilderPart, FieldIteratorPart, FieldTempInit, NestedMatcher};
|
||||
|
||||
/// The field maps to a child
|
||||
pub(super) struct ChildField {
|
||||
|
|
@ -101,7 +101,7 @@ impl Field for ChildField {
|
|||
init: quote! { ::core::option::Option::None },
|
||||
ty: option_ty(ty.clone()),
|
||||
},
|
||||
matcher: quote! {
|
||||
matcher: NestedMatcher::Selective(quote! {
|
||||
match #matcher {
|
||||
::core::result::Result::Ok(v) => if #field_access.is_some() {
|
||||
::core::result::Result::Err(::xso::error::FromEventsError::Invalid(::xso::error::Error::Other(#duplicate_msg)))
|
||||
|
|
@ -110,7 +110,7 @@ impl Field for ChildField {
|
|||
},
|
||||
::core::result::Result::Err(e) => ::core::result::Result::Err(e),
|
||||
}
|
||||
},
|
||||
}),
|
||||
builder,
|
||||
collect: quote! {
|
||||
#field_access = ::core::option::Option::Some(#fetch);
|
||||
|
|
@ -132,7 +132,7 @@ impl Field for ChildField {
|
|||
init: quote! { #ty_default() },
|
||||
ty: ty.clone(),
|
||||
},
|
||||
matcher,
|
||||
matcher: NestedMatcher::Selective(matcher),
|
||||
builder,
|
||||
collect: quote! {
|
||||
#ty_extend(&mut #field_access, [#fetch]);
|
||||
|
|
|
|||
110
xso-proc/src/field/element.rs
Normal file
110
xso-proc/src/field/element.rs
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
// Copyright (c) 2024 Jonas Schäfer <jonas@zombofant.net>
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
//! This module concerns the processing of untyped `minidom::Element`
|
||||
//! children.
|
||||
//!
|
||||
//! In particular, it provides the `#[xml(element)]` implementation.
|
||||
|
||||
use proc_macro2::{Span, TokenStream};
|
||||
use quote::quote;
|
||||
use syn::*;
|
||||
|
||||
use crate::error_message::ParentRef;
|
||||
use crate::scope::{AsItemsScope, FromEventsScope};
|
||||
use crate::types::{
|
||||
default_fn, element_ty, from_xml_builder_ty, into_iterator_into_iter_fn, into_iterator_iter_ty,
|
||||
item_iter_ty, option_ty, ref_ty,
|
||||
};
|
||||
|
||||
use super::{Field, FieldBuilderPart, FieldIteratorPart, FieldTempInit, NestedMatcher};
|
||||
|
||||
pub(super) struct ElementField;
|
||||
|
||||
impl Field for ElementField {
|
||||
fn make_builder_part(
|
||||
&self,
|
||||
scope: &FromEventsScope,
|
||||
_container_name: &ParentRef,
|
||||
member: &Member,
|
||||
ty: &Type,
|
||||
) -> Result<FieldBuilderPart> {
|
||||
let FromEventsScope {
|
||||
ref substate_result,
|
||||
..
|
||||
} = scope;
|
||||
let field_access = scope.access_field(member);
|
||||
|
||||
let element_ty = element_ty(Span::call_site());
|
||||
let default_fn = default_fn(ty.clone());
|
||||
let builder = from_xml_builder_ty(element_ty.clone());
|
||||
|
||||
Ok(FieldBuilderPart::Nested {
|
||||
extra_defs: TokenStream::default(),
|
||||
value: FieldTempInit {
|
||||
init: quote! { #default_fn() },
|
||||
ty: ty.clone(),
|
||||
},
|
||||
matcher: NestedMatcher::Fallback(quote! {
|
||||
#builder::new(name, attrs)
|
||||
}),
|
||||
builder,
|
||||
collect: quote! {
|
||||
<#ty as ::core::iter::Extend::<#element_ty>>::extend(&mut #field_access, [#substate_result]);
|
||||
},
|
||||
finalize: quote! {
|
||||
#field_access
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
fn make_iterator_part(
|
||||
&self,
|
||||
scope: &AsItemsScope,
|
||||
_container_name: &ParentRef,
|
||||
bound_name: &Ident,
|
||||
_member: &Member,
|
||||
ty: &Type,
|
||||
) -> Result<FieldIteratorPart> {
|
||||
let AsItemsScope { ref lifetime, .. } = scope;
|
||||
|
||||
let element_ty = element_ty(Span::call_site());
|
||||
let iter_ty = item_iter_ty(element_ty.clone(), lifetime.clone());
|
||||
let element_iter = into_iterator_iter_ty(ref_ty(ty.clone(), lifetime.clone()));
|
||||
let into_iter = into_iterator_into_iter_fn(ref_ty(ty.clone(), lifetime.clone()));
|
||||
|
||||
let state_ty = Type::Tuple(TypeTuple {
|
||||
paren_token: token::Paren::default(),
|
||||
elems: [element_iter, option_ty(iter_ty)].into_iter().collect(),
|
||||
});
|
||||
|
||||
Ok(FieldIteratorPart::Content {
|
||||
extra_defs: TokenStream::default(),
|
||||
value: FieldTempInit {
|
||||
init: quote! {
|
||||
(#into_iter(#bound_name), ::core::option::Option::None)
|
||||
},
|
||||
ty: state_ty,
|
||||
},
|
||||
generator: quote! {
|
||||
loop {
|
||||
if let ::core::option::Option::Some(current) = #bound_name.1.as_mut() {
|
||||
if let ::core::option::Option::Some(item) = current.next() {
|
||||
break ::core::option::Option::Some(item).transpose();
|
||||
}
|
||||
}
|
||||
if let ::core::option::Option::Some(item) = #bound_name.0.next() {
|
||||
#bound_name.1 = ::core::option::Option::Some(
|
||||
<#element_ty as ::xso::AsXml>::as_xml_iter(item)?
|
||||
);
|
||||
} else {
|
||||
break ::core::result::Result::Ok(::core::option::Option::None)
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -18,10 +18,14 @@ use crate::scope::{AsItemsScope, FromEventsScope};
|
|||
|
||||
mod attribute;
|
||||
mod child;
|
||||
#[cfg(feature = "minidom")]
|
||||
mod element;
|
||||
mod text;
|
||||
|
||||
use self::attribute::AttributeField;
|
||||
use self::child::{ChildField, ExtractDef};
|
||||
#[cfg(feature = "minidom")]
|
||||
use self::element::ElementField;
|
||||
use self::text::TextField;
|
||||
|
||||
/// Code slices necessary for declaring and initializing a temporary variable
|
||||
|
|
@ -34,6 +38,33 @@ pub(crate) struct FieldTempInit {
|
|||
pub(crate) init: TokenStream,
|
||||
}
|
||||
|
||||
/// Configure how a nested field builder selects child elements.
|
||||
pub(crate) enum NestedMatcher {
|
||||
/// Matches a specific child element fallabily.
|
||||
Selective(
|
||||
/// Expression which evaluates to `Result<T, FromEventsError>`,
|
||||
/// consuming `name: rxml::QName` and `attrs: rxml::AttrMap`.
|
||||
///
|
||||
/// `T` must be the type specified in the
|
||||
/// [`FieldBuilderPart::Nested::builder`] field.
|
||||
TokenStream,
|
||||
),
|
||||
|
||||
#[cfg_attr(not(feature = "minidom"), allow(dead_code))]
|
||||
/// Matches any child element not matched by another matcher.
|
||||
///
|
||||
/// Only a single field may use this variant, otherwise an error is
|
||||
/// raised during execution of the proc macro.
|
||||
Fallback(
|
||||
/// Expression which evaluates to `T` (or `return`s an error),
|
||||
/// consuming `name: rxml::QName` and `attrs: rxml::AttrMap`.
|
||||
///
|
||||
/// `T` must be the type specified in the
|
||||
/// [`FieldBuilderPart::Nested::builder`] field.
|
||||
TokenStream,
|
||||
),
|
||||
}
|
||||
|
||||
/// Describe how a struct or enum variant's member is parsed from XML data.
|
||||
///
|
||||
/// This struct is returned from [`FieldDef::make_builder_part`] and
|
||||
|
|
@ -73,12 +104,9 @@ pub(crate) enum FieldBuilderPart {
|
|||
/// parsing.
|
||||
value: FieldTempInit,
|
||||
|
||||
/// Expression which evaluates to `Result<T, FromEventsError>`,
|
||||
/// consuming `name: rxml::QName` and `attrs: rxml::AttrMap`.
|
||||
///
|
||||
/// `T` must be the type specified in the
|
||||
/// [`Self::Nested::builder`] field.
|
||||
matcher: TokenStream,
|
||||
/// Configure child matching behaviour for this field. See
|
||||
/// [`NestedMatcher`] for options.
|
||||
matcher: NestedMatcher,
|
||||
|
||||
/// Type implementing `xso::FromEventsBuilder` which parses the child
|
||||
/// element.
|
||||
|
|
@ -343,6 +371,31 @@ fn new_field(
|
|||
}),
|
||||
}))
|
||||
}
|
||||
|
||||
#[cfg(feature = "minidom")]
|
||||
XmlFieldMeta::Element { span, amount } => {
|
||||
match amount {
|
||||
Some(AmountConstraint::Any(_)) => (),
|
||||
Some(AmountConstraint::FixedSingle(span)) => {
|
||||
return Err(Error::new(
|
||||
span,
|
||||
"only `n = ..` is supported for #[xml(element)]` currently",
|
||||
))
|
||||
}
|
||||
None => return Err(Error::new(span, "`n` must be set to `..` currently")),
|
||||
}
|
||||
|
||||
Ok(Box::new(ElementField))
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "minidom"))]
|
||||
XmlFieldMeta::Element { span, amount } => {
|
||||
let _ = amount;
|
||||
Err(Error::new(
|
||||
span,
|
||||
"#[xml(element)] requires xso to be built with the \"minidom\" feature.",
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -351,6 +404,9 @@ fn new_field(
|
|||
/// See [`Compound`][`crate::compound::Compound`] for more information on
|
||||
/// compounds in general.
|
||||
pub(crate) struct FieldDef {
|
||||
/// A span which refers to the field's definition.
|
||||
span: Span,
|
||||
|
||||
/// The member identifying the field.
|
||||
member: Member,
|
||||
|
||||
|
|
@ -388,6 +444,7 @@ impl FieldDef {
|
|||
let ty = field.ty.clone();
|
||||
|
||||
Ok(Self {
|
||||
span: field_span,
|
||||
inner: new_field(meta, ident, &ty, container_namespace)?,
|
||||
member,
|
||||
ty,
|
||||
|
|
@ -406,6 +463,7 @@ impl FieldDef {
|
|||
) -> Result<Self> {
|
||||
let span = meta.span();
|
||||
Ok(Self {
|
||||
span,
|
||||
member: Member::Unnamed(Index { index, span }),
|
||||
ty: ty.clone(),
|
||||
inner: new_field(meta, None, ty, container_namespace)?,
|
||||
|
|
@ -454,4 +512,9 @@ impl FieldDef {
|
|||
pub(crate) fn is_text_field(&self) -> bool {
|
||||
self.inner.captures_text()
|
||||
}
|
||||
|
||||
/// Return a span which points at the field's definition.'
|
||||
pub(crate) fn span(&self) -> Span {
|
||||
self.span
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue