patchable_macro/
lib.rs

1use proc_macro::TokenStream;
2
3use quote::quote;
4use syn::{self, DeriveInput};
5
6mod context;
7
8#[proc_macro_derive(Patchable, attributes(patchable))]
9pub fn derive_state_and_patchable_impl(input: TokenStream) -> TokenStream {
10    let input: DeriveInput = syn::parse_macro_input!(input as DeriveInput);
11    match context::MacroContext::new(&input) {
12        Ok(ctx) => {
13            let state_struct_def = ctx.build_state_struct();
14            let patchable_trait_impl = ctx.build_patchable_trait_impl();
15
16            quote! {
17                const _: () = {
18                    #state_struct_def
19                    #[automatically_derived]
20                    #patchable_trait_impl
21                };
22            }
23            .into()
24        }
25        Err(e) => e.to_compile_error().into(),
26    }
27}