patchable_macro/
lib.rs

1//! # Patchable Macro
2//!
3//! Procedural macro for the `patchable` crate.
4//!
5//! This crate contains the `Patchable` derive macro, which generates:
6//! 1. A companion "Patch" struct (e.g., `MyStructPatch`).
7//! 2. An implementation of the `Patchable` trait for the original struct.
8
9use proc_macro::TokenStream;
10
11use quote::quote;
12use syn::{self, DeriveInput};
13
14mod context;
15
16#[proc_macro_derive(Patchable, attributes(patchable))]
17pub fn derive_state_and_patchable_impl(input: TokenStream) -> TokenStream {
18    let input: DeriveInput = syn::parse_macro_input!(input as DeriveInput);
19    match context::MacroContext::new(&input) {
20        Ok(ctx) => {
21            let state_struct_def = ctx.build_state_struct();
22            let patchable_trait_impl = ctx.build_patchable_trait_impl();
23
24            quote! {
25                const _: () = {
26                    #state_struct_def
27                    #[automatically_derived]
28                    #patchable_trait_impl
29                };
30            }
31            .into()
32        }
33        Err(e) => e.to_compile_error().into(),
34    }
35}