endpoint_gen_macros/lib.rs
1use proc_macro::TokenStream;
2use quote::quote;
3use syn::{parse_macro_input, DeriveInput};
4
5/// Derive macro ensures that the type manually implements GenElement<Self>.
6/// This enforces that any type used as a Definition variant has proper validation logic.
7///
8/// # Example
9/// ```ignore
10/// #[derive(DefinitionVariant)]
11/// pub struct EnumElement {
12/// config: RustGenConfig,
13/// inner: Type,
14/// }
15///
16/// impl GenElement<EnumElement> for EnumElement {
17/// fn validate_element(&self) -> eyre::Result<()> {
18/// // validation logic
19/// }
20/// }
21/// ```
22#[proc_macro_derive(DefinitionVariant)]
23pub fn derive_definition_variant(input: TokenStream) -> TokenStream {
24 let input = parse_macro_input!(input as DeriveInput);
25 let name = &input.ident;
26
27 // Generate code that requires GenElement<Self> to be implemented
28 // We do this by calling a function that depends on GenElement being in scope
29 let expanded = quote! {
30 // Compile-time check: ensure GenElement<Self> is implemented
31 const _: () = {
32 const fn check_gen_element<T: GenElement<T>>() {}
33 const fn assert_impl() {
34 check_gen_element::<#name>();
35 }
36 };
37 };
38
39 TokenStream::from(expanded)
40}