shellder_macros/
lib.rs

1extern crate proc_macro;
2
3use proc_macro::TokenStream;
4use quote::quote;
5use syn::{parse_macro_input, ItemStruct};
6
7/// Marks a struct as a Shellder component
8#[proc_macro_attribute]
9pub fn component(_attr: TokenStream, item: TokenStream) -> TokenStream {
10    // Parse the input struct
11    let input = parse_macro_input!(item as ItemStruct);
12
13    // Get the struct name
14    let name = &input.ident;
15
16    // Generate an impl block that marks this as a Registerable component
17    let expanded = quote! {
18        #input
19
20        impl Registerable for #name {
21            fn register(container: &Container) {
22                container.register(Self);
23            }
24        }
25    };
26
27    TokenStream::from(expanded)
28}
29
30#[proc_macro_derive(Hooks)]
31pub fn derive_hooks(input: TokenStream) -> TokenStream {
32    // Parse the input into a syntax tree
33    let ast = syn::parse_macro_input!(input as syn::DeriveInput);
34
35    // Extract the struct name
36    let name = &ast.ident;
37
38    // Generate the impl Hooks
39    let expanded = quote! {
40        impl #name {
41            pub fn run(){
42                let instance = #name;
43                instance.startup();
44                instance.run();
45                instance.cleanup();
46            }
47        }
48    };
49
50    TokenStream::from(expanded)
51}