Skip to main content

tonic_clap_macros/
lib.rs

1use proc_macro::TokenStream;
2use quote::quote;
3use syn::{DeriveInput, parse_macro_input};
4
5/// Combined derive macro that implements both Args and FromArgMatches
6#[proc_macro_derive(TonicClap)]
7pub fn derive_tonic_clap(input: TokenStream) -> TokenStream {
8    let input = parse_macro_input!(input as DeriveInput);
9    let name = &input.ident;
10
11    let expanded = quote! {
12        impl clap::Args for #name {
13            fn augment_args(cmd: clap::Command) -> clap::Command {
14                use bevy_reflect::Typed;
15                tonic_clap::impl_augment_args(cmd, Self::type_info())
16            }
17
18            fn augment_args_for_update(cmd: clap::Command) -> clap::Command {
19                Self::augment_args(cmd)
20            }
21        }
22
23        impl clap::FromArgMatches for #name {
24            fn from_arg_matches(
25                matches: &clap::ArgMatches,
26            ) -> ::std::result::Result<Self, clap::Error> {
27                tonic_clap::impl_from_arg_matches(matches)
28            }
29
30            fn update_from_arg_matches(
31                &mut self,
32                matches: &clap::ArgMatches,
33            ) -> ::std::result::Result<(), clap::Error> {
34                *self = tonic_clap::impl_from_arg_matches(matches)?;
35                Ok(())
36            }
37        }
38    };
39
40    TokenStream::from(expanded)
41}