diff --git a/instant-xml-macros/src/de.rs b/instant-xml-macros/src/de.rs index c03f0c4..119e5f8 100644 --- a/instant-xml-macros/src/de.rs +++ b/instant-xml-macros/src/de.rs @@ -11,13 +11,12 @@ pub(crate) fn from_xml(input: &syn::DeriveInput) -> TokenStream { }; match &input.data { - syn::Data::Struct(_) if meta.scalar => { - syn::Error::new(input.span(), "scalar structs are unsupported!").to_compile_error() + syn::Data::Struct(_) if meta.mode.is_some() => { + syn::Error::new(input.span(), "no enum mode allowed on struct type").to_compile_error() } syn::Data::Struct(ref data) => deserialize_struct(input, data, meta), - syn::Data::Enum(_) if !meta.scalar => { - syn::Error::new(input.span(), "non-scalar enums are currently unsupported!") - .to_compile_error() + syn::Data::Enum(_) if meta.mode.is_none() => { + syn::Error::new(input.span(), "missing enum mode").to_compile_error() } syn::Data::Enum(ref data) => deserialize_enum(input, data, meta), _ => todo!(), diff --git a/instant-xml-macros/src/lib.rs b/instant-xml-macros/src/lib.rs index b80662f..e21363d 100644 --- a/instant-xml-macros/src/lib.rs +++ b/instant-xml-macros/src/lib.rs @@ -33,7 +33,7 @@ struct ContainerMeta<'input> { ns: NamespaceMeta, rename: Option, rename_all: RenameRule, - scalar: bool, + mode: Option, } impl<'input> ContainerMeta<'input> { @@ -41,7 +41,7 @@ impl<'input> ContainerMeta<'input> { let mut ns = NamespaceMeta::default(); let mut rename = Default::default(); let mut rename_all = Default::default(); - let mut scalar = Default::default(); + let mut mode = None; for (item, span) in meta_items(&input.attrs) { match item { @@ -59,7 +59,10 @@ impl<'input> ContainerMeta<'input> { Err(err) => return Err(syn::Error::new(span, err)), }; } - MetaItem::Scalar => scalar = true, + MetaItem::Scalar => match mode { + None => mode = Some(Mode::Scalar), + Some(_) => return Err(syn::Error::new(span, "cannot have two enum modes")), + }, } } @@ -68,7 +71,7 @@ impl<'input> ContainerMeta<'input> { ns, rename, rename_all, - scalar, + mode, }) } @@ -133,10 +136,7 @@ impl FieldMeta { )) } MetaItem::Scalar => { - return Err(syn::Error::new( - span, - "attribute 'scalar' is invalid for struct fields", - )) + return Err(syn::Error::new(span, "invalid attribute for struct field")); } } } @@ -166,28 +166,10 @@ impl VariantMeta { for (item, span) in meta_items(&input.attrs) { match item { MetaItem::Rename(lit) => rename = Some(lit.to_token_stream()), - MetaItem::Attribute => { + _ => { return Err(syn::Error::new( span, - "attribute 'attribute' is invalid for enum variants", - )) - } - MetaItem::Ns(_ns) => { - return Err(syn::Error::new( - span, - "attribute 'ns' is invalid for enum variants", - )) - } - MetaItem::RenameAll(_) => { - return Err(syn::Error::new( - span, - "attribute 'rename_all' invalid in field xml attribute", - )) - } - MetaItem::Scalar => { - return Err(syn::Error::new( - span, - "attribute 'scalar' is invalid for enum variants", + "only 'rename' attribute is permitted on enum variants", )) } } @@ -715,6 +697,10 @@ fn discard_path_lifetimes(path: &mut syn::TypePath) { } } +enum Mode { + Scalar, +} + #[cfg(test)] mod tests { use syn::parse_quote; diff --git a/instant-xml-macros/src/ser.rs b/instant-xml-macros/src/ser.rs index 750d307..5a3d0bd 100644 --- a/instant-xml-macros/src/ser.rs +++ b/instant-xml-macros/src/ser.rs @@ -13,12 +13,12 @@ pub fn to_xml(input: &syn::DeriveInput) -> proc_macro2::TokenStream { }; match &input.data { - syn::Data::Struct(_) if meta.scalar => { - syn::Error::new(input.span(), "scalar structs are unsupported!").to_compile_error() + syn::Data::Struct(_) if meta.mode.is_some() => { + syn::Error::new(input.span(), "enum mode not allowed on struct type").to_compile_error() } syn::Data::Struct(ref data) => serialize_struct(input, data, meta), - syn::Data::Enum(_) if !meta.scalar => { - syn::Error::new(input.span(), "non-scalar enums are currently unsupported!") + syn::Data::Enum(_) if meta.mode.is_none() => { + syn::Error::new(input.span(), "missing enum mode") .to_compile_error() } syn::Data::Enum(ref data) => serialize_enum(input, data, meta),