kittycad_modeling_cmds_macros_impl/
modeling_cmd_variant.rs1use proc_macro2::TokenStream;
2use quote::{quote, quote_spanned};
3use syn::{spanned::Spanned, DeriveInput};
4
5pub fn derive_nonempty(input: DeriveInput) -> TokenStream {
6 let span = input.span();
8 let name = input.ident;
10 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}