Skip to main content

famiq_macros/
lib.rs

1extern crate proc_macro;
2
3use proc_macro::TokenStream;
4use syn::*;
5use quote::*;
6
7#[proc_macro_attribute]
8pub fn set_widget_attributes(_attr: TokenStream, item: TokenStream) -> TokenStream {
9    let mut input = parse_macro_input!(item as ItemStruct);
10    let name = &input.ident;
11
12    // Insert attributes field
13    if let Fields::Named(fields) = &mut input.fields {
14        fields.named.push(syn::parse_quote! {
15            pub attributes: WidgetAttributes
16        });
17        fields.named.push(syn::parse_quote! {
18            pub cloned_attrs: WidgetAttributes
19        });
20    }
21    let expanded = quote! {
22        #input
23
24        impl SetWidgetAttributes for #name {
25            fn attributes(&mut self) -> &mut WidgetAttributes {
26                &mut self.attributes
27            }
28
29            fn cloned_attrs(&mut self) -> &mut WidgetAttributes {
30                &mut self.cloned_attrs
31            }
32        }
33    };
34    expanded.into()
35}