1
0
mirror of https://gitlab.com/xmpp-rs/xmpp-rs.git synced 2024-06-09 01:34:03 +02:00

parsers-macros: make #[xml()] on dynamic enums optional

This commit is contained in:
Jonas Schäfer 2024-03-31 14:55:38 +02:00
parent eae3efc1de
commit c2355a6200
2 changed files with 28 additions and 3 deletions

View File

@ -858,7 +858,14 @@ struct EnumDef {
impl EnumDef {
/// Construct a new enum from its `#[xml(..)]` attribute and the variants.
fn new<'x, I: Iterator<Item = &'x Variant>>(meta: XmlCompoundMeta, input: I) -> Result<Self> {
fn new<'x, I: Iterator<Item = &'x Variant>>(
meta: Option<XmlCompoundMeta>,
input: I,
) -> Result<Self> {
let meta = match meta {
Some(v) => v,
None => XmlCompoundMeta::empty(Span::call_site()),
};
let prepare = meta.prepare;
let validate = meta.validate;
let debug = meta.debug;
@ -1015,7 +1022,7 @@ impl EnumDef {
/// `FromXml` derive macro implementation for enumerations.
pub(crate) fn try_from_element(item: syn::ItemEnum) -> Result<proc_macro2::TokenStream> {
let meta = XmlCompoundMeta::parse_from_attributes(&item.attrs)?;
let meta = XmlCompoundMeta::try_parse_from_attributes(&item.attrs)?;
let ident = item.ident;
let def = EnumDef::new(meta, item.variants.iter())?;
let try_from_impl =
@ -1046,7 +1053,7 @@ pub(crate) fn try_from_element(item: syn::ItemEnum) -> Result<proc_macro2::Token
/// `IntoXml` derive macro implementation for enumerations.
pub(crate) fn into_element(item: syn::ItemEnum) -> Result<proc_macro2::TokenStream> {
let meta = XmlCompoundMeta::parse_from_attributes(&item.attrs)?;
let meta = XmlCompoundMeta::try_parse_from_attributes(&item.attrs)?;
let ident = item.ident;
let mut def = EnumDef::new(meta, item.variants.iter())?;
let prepare = if let Some(prepare) = def.prepare.take() {

View File

@ -505,6 +505,24 @@ pub(crate) struct XmlCompoundMeta {
}
impl XmlCompoundMeta {
pub(crate) fn empty(span: Span) -> Self {
XmlCompoundMeta {
span,
namespace: None,
name: None,
attribute: None,
value: None,
fallback: Flag::Absent,
transparent: Flag::Absent,
exhaustive: Flag::Absent,
validate: None,
prepare: None,
normalize_with: None,
debug: Flag::Absent,
element: None,
}
}
/// Parse the meta values from a `#[xml(..)]` attribute.
///
/// Undefined options or options with incompatible values are rejected