kittycad_modeling_cmds_macros_impl/
modeling_cmd_variant.rs

1use proc_macro2::TokenStream;
2use quote::{quote, quote_spanned};
3use syn::{spanned::Spanned, DeriveInput};
4
5pub fn derive_nonempty(input: DeriveInput) -> TokenStream {
6    // Where in the input source code is this type defined?
7    let span = input.span();
8    // Name of type which is deriving this trait.
9    let name = input.ident;
10    // Delegate to whichever macro can generate code for this type (struct, enum, etc)
11    match input.data {
12        syn::Data::Struct(_) => derive_nonempty_on_struct(name),
13        syn::Data::Enum(_) => quote_spanned! {span =>
14            compile_error!("ModelingCmdVariant cannot be implemented on an enum type")
15        },
16        syn::Data::Union(_) => quote_spanned! {span =>
17            compile_error!("ModelingCmdVariant cannot be implemented on a union type")
18        },
19    }
20}
21
22fn derive_nonempty_on_struct(name: proc_macro2::Ident) -> TokenStream {
23    quote! {
24        impl kittycad_modeling_cmds::ModelingCmdVariant for #name {
25            type Output = kittycad_modeling_cmds::output::#name;
26            fn into_enum(self) -> kittycad_modeling_cmds::ModelingCmd {
27                kittycad_modeling_cmds::ModelingCmd::#name(self)
28            }
29            fn name() -> &'static str {
30                stringify!(#name)
31            }
32        }
33    }
34}