TedDriggs / darling

A Rust proc-macro attribute parser
MIT License
983 stars 66 forks source link

Shape support on non-derived attribute macros? #301

Closed kolektiv closed 1 month ago

kolektiv commented 1 month ago

Question is really just the title! #[darling(supports(...))] is very useful when implementing derive macros, but it would be great for simple attribute macros as well. Obviously there's a lot more potential cases where the macro might be applied, but even just restricting application to the current options as derive (if wished) would be useful. I've got a macro which will only ever apply to unit or item structs, for example, and it would be nice to make that an error if misused!

TedDriggs commented 1 month ago

This works fine today.

FromDeriveInput takes a DeriveInput, which is a thing that can be the target of a derive macro: A struct, enum, or union definition. However, if your attribute macro's item can be parsed as a syn::DeriveInput then your options struct can derive FromDeriveInput.

Here is an example from another crate I worked on. Notice that I'm parsing the item as a DeriveInput, then calling Collection::from_derive_input on that, and that Collection has the supports(enum_newtype) declaration set here.