kittycad_modeling_cmds_macros/
lib.rs

1//! Proc-macros for implementing kittycad-modeling-cmds traits.
2
3use kittycad_modeling_cmds_macros_impl::modeling_cmd_enum;
4use kittycad_modeling_cmds_macros_impl::modeling_cmd_output;
5use kittycad_modeling_cmds_macros_impl::modeling_cmd_variant;
6use kittycad_modeling_cmds_macros_impl::ok_modeling_cmd_response_enum;
7
8use proc_macro::TokenStream;
9use syn::{DeriveInput, ItemMod};
10
11/// This will derive the trait `ModelingCmdVariant` from the `kittycad-modeling-cmds` crate.
12/// Its associated type `output` will be the corresponding modeling command output type.
13#[proc_macro_derive(ModelingCmdVariant)]
14pub fn derive_modeling_cmd_variant_nonempty(input: TokenStream) -> TokenStream {
15    // Parse the input into a stream of Rust syntax tokens.
16    let input: DeriveInput = syn::parse2(input.into()).unwrap();
17    // Generate a new stream of Rust syntax tokens from the input stream.
18    // Then hand them back to the compiler.
19    // It's idiomatic to make your proc macros a thin wrapper around an "impl" function, because it
20    // simplifies unit testing. This is recommended in The Rust Book.
21    TokenStream::from(modeling_cmd_variant::derive_nonempty(input))
22}
23
24/// Generates the ModelingCmd enum from all its variants.
25#[proc_macro]
26pub fn define_modeling_cmd_enum(item: TokenStream) -> TokenStream {
27    let input: ItemMod = syn::parse2(item.into()).unwrap();
28    TokenStream::from(modeling_cmd_enum::generate(input))
29}
30
31/// Derives `ModelingCmdOutput`.
32#[proc_macro_derive(ModelingCmdOutput)]
33pub fn derive_modeling_cmd_output(input: TokenStream) -> TokenStream {
34    let input: DeriveInput = syn::parse2(input.into()).unwrap();
35    TokenStream::from(modeling_cmd_output::derive(input))
36}
37
38/// Generates the OkModelingCmdResponse enum from all its variants.
39#[proc_macro]
40pub fn define_ok_modeling_cmd_response_enum(item: TokenStream) -> TokenStream {
41    let input: ItemMod = syn::parse2(item.into()).unwrap();
42    TokenStream::from(ok_modeling_cmd_response_enum::generate(input))
43}