xso-proc: provide FromXml::xml_name_matcher on derived impls

That way, derived implementations can also make use of the performance
benefits.
This commit is contained in:
Jonas Schäfer 2025-05-01 16:26:32 +02:00 committed by Link Mauve
commit 59af49c691
5 changed files with 166 additions and 3 deletions

View file

@ -7,8 +7,45 @@
//! Definitions common to both enums and structs
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::*;
/// Template which renders to a `xso::fromxml::XmlNameMatcher` value.
pub(crate) enum XmlNameMatcher {
/// Renders as `xso::fromxml::XmlNameMatcher::Any`.
#[allow(dead_code)] // We keep it for completeness.
Any,
/// Renders as `xso::fromxml::XmlNameMatcher::InNamespace(#0)`.
InNamespace(TokenStream),
/// Renders as `xso::fromxml::XmlNameMatcher::Specific(#0, #1)`.
Specific(TokenStream, TokenStream),
/// Renders as `#0`.
///
/// This is an escape hatch for more complicated constructs, e.g. when
/// a superset of multiple matchers is required.
Custom(TokenStream),
}
impl ToTokens for XmlNameMatcher {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Self::Any => tokens.extend(quote! {
::xso::fromxml::XmlNameMatcher::<'static>::Any
}),
Self::InNamespace(ref namespace) => tokens.extend(quote! {
::xso::fromxml::XmlNameMatcher::<'static>::InNamespace(#namespace)
}),
Self::Specific(ref namespace, ref name) => tokens.extend(quote! {
::xso::fromxml::XmlNameMatcher::<'static>::Specific(#namespace, #name)
}),
Self::Custom(ref stream) => tokens.extend(stream.clone()),
}
}
}
/// Parts necessary to construct a `::xso::FromXml` implementation.
pub(crate) struct FromXmlParts {
/// Additional items necessary for the implementation.
@ -19,6 +56,9 @@ pub(crate) struct FromXmlParts {
/// The name of the type which is the `::xso::FromXml::Builder`.
pub(crate) builder_ty_ident: Ident,
/// The `XmlNameMatcher` to pre-select elements for this implementation.
pub(crate) name_matcher: XmlNameMatcher,
}
/// Parts necessary to construct a `::xso::AsXml` implementation.