danielhenrymantilla / macro_rules_attribute-rs

Use declarative macros as proc_macro attributes or derives
https://docs.rs/macro-rules-attribute
Apache License 2.0
102 stars 10 forks source link

[Question] How to use with serde? #19

Open failable opened 1 year ago

failable commented 1 year ago

Hi,

When I try to play with the library, I can not figure out how to make it work with serde. How can I fix this? Thanks.

#[macro_use]
extern crate macro_rules_attribute;

fn main() {
    derive_alias! {
        #[derive(Serde!)] = #[derive(serde::Serialize, serde::Deserialize)];
    }

    #[derive(Debug, Clone, Copy, Serde!)]
    struct Foo {
        #[serde(rename = "$id")] // cannot find attribute `serde` in this scope
        pub id: i64,
    }
}
danielhenrymantilla commented 1 year ago

Yeah, this is currently not supported, due to technical limitations; see https://docs.rs/macro_rules_attribute/0.2.0/macro_rules_attribute/macro.derive_alias.html#caveat-regarding-derive-helpers-inert-made-attributes:


attribute_alias! {
    #[apply(Serde)] = #[derive(::serde::Serialize, ::serde::Deserialize)];
}

#[apply(Serde)]
#[derive(Debug, Clone, Copy)]
struct Foo {
    #[serde(rename = "$id")] // OK
    pub id: i64,
}