to_display_derive/
lib.rs

1use proc_macro::TokenStream;
2use quote::quote;
3use syn::parse_macro_input;
4use syn::DeriveInput;
5
6#[proc_macro_derive(ToDisplay)]
7pub fn derive_to_display(input: TokenStream) -> TokenStream {
8    // Parse the input tokens into a syntax tree
9    let input = parse_macro_input!(input as DeriveInput);
10    let name = &input.ident;
11
12    // Generate the implementation
13    let expanded = quote! {
14        impl ::to_display::ToDisplay for #name {
15            type Displayer<'a> = &'a #name where Self: 'a;
16
17            fn display_with_context(&self, context: ::to_display::Context) -> Self::Displayer<'_> {
18                self
19            }
20        }
21    };
22
23    // Convert back to token stream and return
24    TokenStream::from(expanded)
25}