Skip to main content

patchable_macro/
lib.rs

1//! # Patchable Macro
2//!
3//! Procedural macros for the `patchable` crate: `Patchable` and `Patch` derives.
4//!
5//! See `context` for details on the generated patch struct and trait implementations.
6
7use proc_macro::TokenStream;
8
9use quote::quote;
10use syn::{self, DeriveInput};
11
12mod context;
13
14#[proc_macro_derive(Patchable, attributes(patchable))]
15pub fn derive_patchable(input: TokenStream) -> TokenStream {
16    let input: DeriveInput = syn::parse_macro_input!(input as DeriveInput);
17    match context::MacroContext::new(&input) {
18        Ok(ctx) => {
19            let patch_struct_def = ctx.build_patch_struct();
20            let patchable_trait_impl = ctx.build_patchable_trait_impl();
21
22            quote! {
23                const _: () = {
24                    #[automatically_derived]
25                    #patch_struct_def
26                    #[automatically_derived]
27                    #patchable_trait_impl
28                };
29            }
30            .into()
31        }
32        Err(e) => e.to_compile_error().into(),
33    }
34}
35
36#[proc_macro_derive(Patch, attributes(patchable))]
37pub fn derive_patch(input: TokenStream) -> TokenStream {
38    let input: DeriveInput = syn::parse_macro_input!(input as DeriveInput);
39    match context::MacroContext::new(&input) {
40        Ok(ctx) => {
41            let patch_trait_impl = ctx.build_patch_trait_impl();
42
43            quote! {
44                const _: () = {
45                    #[automatically_derived]
46                    #patch_trait_impl
47                };
48            }
49            .into()
50        }
51        Err(e) => e.to_compile_error().into(),
52    }
53}