feat(derive): Add derive macro for interface
This commit is contained in:
12
crates/char_sdk_derive/Cargo.toml
Normal file
12
crates/char_sdk_derive/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "char_sdk_derive"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
syn = "1.0.107"
|
||||
quote = "1.0.23"
|
||||
[lib]
|
||||
proc-macro = true
|
24
crates/char_sdk_derive/src/lib.rs
Normal file
24
crates/char_sdk_derive/src/lib.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
extern crate proc_macro;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
|
||||
#[proc_macro_derive(CharAction)]
|
||||
pub fn derive_char_action_into_box(input: TokenStream) -> TokenStream {
|
||||
let ast = syn::parse(input).unwrap();
|
||||
|
||||
// Build the trait implementation
|
||||
impl_hello_macro(&ast)
|
||||
}
|
||||
|
||||
fn impl_hello_macro(ast: &syn::DeriveInput) -> TokenStream {
|
||||
let name = &ast.ident;
|
||||
let gen = quote! {
|
||||
impl Into<Box<dyn char_sdk::Action>> for #name {
|
||||
fn into(self) -> Box<dyn char_sdk::Action> {
|
||||
Box::new(self)
|
||||
}
|
||||
}
|
||||
};
|
||||
gen.into()
|
||||
}
|
Reference in New Issue
Block a user