#meta #syn #struct #parse #parser #to-tokens

syn-unnamed-struct

Extends syn expressions and meta structs with unnamed structs and meta lists

1 unstable release

0.1.0 Apr 3, 2022

#2244 in Procedural macros

Download history 88/week @ 2024-07-20 255/week @ 2024-07-27 53/week @ 2024-08-03 99/week @ 2024-08-10 81/week @ 2024-08-17 96/week @ 2024-08-24 99/week @ 2024-08-31 110/week @ 2024-09-07 86/week @ 2024-09-14 133/week @ 2024-09-21 135/week @ 2024-09-28 52/week @ 2024-10-05 84/week @ 2024-10-12 125/week @ 2024-10-19 73/week @ 2024-10-26 109/week @ 2024-11-02

398 downloads per month
Used in derive-from-ext

MIT/Apache

18KB
432 lines

syn_unnamed_struct

Parse and convert structs with no name to tokens. For usage in attribute macro arguments in place of Meta attributes to allow more structured data to be used (nested objects).

Usage in derive macro definition

use syn_unnamed_struct::Meta;

#[proc_macro_derive(CustomMacro, attributes(customMacro))]
pub fn derive(tokens: TokenStream) -> TokenStream {
    let input = parse_macro_input!(tokens);
    
    input.attrs.map(|attr| {
        let obj: Meta = attr.parse().expect("Coult not parse attribute");
        
        //can now interact and extract the properties from the Meta enum
        //...
    });
}

Example macro usage

#[derive(CustomMacro)]
#[customMacro(name="something", other={ entry1: "val1", entry2: "val2" })]
struct MyStruct {
    //...
}

Supported attributes

  • Unnamed structs
#[customMacro({ prop1: 123, prop2: 245 })]
  • Nested unnamed structs
#[customMacro({ prop1: 123, prop2: { prop2a: 123, prop2b: 245 } })]
  • Unnamed struct in Meta value
#[customMacro(prop1=123, prop2={ prop2a: 123, prop2b: 245 })]
  • Unnamed Meta list
#[customMacro(prop1, prop2, (prop3a=123, prop3b=245)))]
  • Nested unnamed Meta lists
#[customMacro(prop1=123, prop2=(prop2a=123, prop2b=245)))]

Notes

  • Cannot use darling since it is entwined with the syn Meta structs

Dependencies

~1.5MB
~36K SLoC