Rust Derive Macros to reduce number of attribute derive calls

I have a struct that has a large number of attributes with repeatable patterns like so:

struct Foo {
   #[nom(Cond = ...
              Map = ...
   #[serde(....
   bar: Option<u8>,

I am trying to use a derive attribute macro to reduce the amount of code I have to write like so:

struct Foo {
   #[helper]
   bar: Option<u8>
}

Is this the right way to go about this?

#[nom] and #[serde] are derive macro helper attributes. They do not do anything on their own; the derive macro looks for them (by name) and interprets them. Therefore, if you want to provide an alternate syntax for them, you have to do this by rewriting the code to derive macros from nom and serde will see. This can only be done using an attribute macro (which can rewrite the code it is applied to), not a derive macro (which can only generate additional code).

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.