xso-proc: Add n = 1 flag to element meta

This allows exactly one arbitrary payload in any element, and is handled
after every other element with a more specific matcher has been parsed.

Both the #[xml(element(n = 1))] meta and its shortcut #[xml(element)]
are allowed and treated the exact same way.
This commit is contained in:
Emmanuel Gil Peyrot 2024-12-19 20:36:57 +01:00 committed by Jonas Schäfer
commit c72cf3ddd1
7 changed files with 265 additions and 80 deletions

View file

@ -1035,17 +1035,19 @@ impl XmlFieldMeta {
/// Parse a `#[xml(element)]` meta.
fn element_from_meta(meta: ParseNestedMeta<'_>) -> Result<Self> {
let mut amount = None;
meta.parse_nested_meta(|meta| {
if meta.path.is_ident("n") {
if amount.is_some() {
return Err(Error::new_spanned(meta.path, "duplicate `n` key"));
if meta.input.peek(syn::token::Paren) {
meta.parse_nested_meta(|meta| {
if meta.path.is_ident("n") {
if amount.is_some() {
return Err(Error::new_spanned(meta.path, "duplicate `n` key"));
}
amount = Some(meta.value()?.parse()?);
Ok(())
} else {
Err(Error::new_spanned(meta.path, "unsupported key"))
}
amount = Some(meta.value()?.parse()?);
Ok(())
} else {
Err(Error::new_spanned(meta.path, "unsupported key"))
}
})?;
})?;
}
Ok(Self::Element {
span: meta.path.span(),
amount,