kittycad_execution_plan_macros/lib.rs
1//! Proc-macros for implementing execution-plan traits.
2
3mod derive_from_memory;
4mod derive_value;
5mod helpers;
6
7use self::{derive_from_memory::impl_derive_from_memory, derive_value::impl_derive_value};
8use proc_macro::TokenStream;
9use quote::quote;
10use syn::DeriveInput;
11
12/// This will derive the trait `Value` from the `kittycad-execution-plan-traits` crate.
13#[proc_macro_derive(ExecutionPlanValue)]
14pub fn derive_value(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(impl_derive_value(input, "e! {::kittycad_execution_plan_traits}))
22}
23
24/// This will derive the trait `Value` from the `kittycad-execution-plan-traits` crate.
25#[proc_macro_derive(ExecutionPlanFromMemory)]
26pub fn derive_from_memory(input: TokenStream) -> TokenStream {
27 // Parse the input into a stream of Rust syntax tokens.
28 let input: DeriveInput = syn::parse2(input.into()).unwrap();
29 // Generate a new stream of Rust syntax tokens from the input stream.
30 // Then hand them back to the compiler.
31 // It's idiomatic to make your proc macros a thin wrapper around an "impl" function, because it
32 // simplifies unit testing. This is recommended in The Rust Book.
33 TokenStream::from(impl_derive_from_memory(
34 input,
35 "e! {::kittycad_execution_plan_traits},
36 ))
37}