feat(derive): implement derive(GameState)

This commit is contained in:
nativerv
2024-02-17 05:21:17 +03:00
parent 4996e4924e
commit e833546669
2 changed files with 25 additions and 11 deletions
+15 -11
View File
@@ -1,25 +1,29 @@
extern crate proc_macro;
use bevy_controls::contract::{Action, ActionInner};
use proc_macro::TokenStream;
use quote::quote;
use syn;
#[proc_macro_derive(Action)]
pub fn action(input: TokenStream) -> TokenStream {
// Construct a representation of Rust code as a syntax tree
// that we can manipulate
let ast = syn::parse(input).unwrap();
let ast = syn::parse::<syn::DeriveInput>(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 Action for #name {}
impl ActionInner for #name {}
impl bevy_controls::contract::Action for #name {}
impl bevy_controls::contract::ActionInner for #name {}
};
gen.into()
}
#[proc_macro_derive(GameState)]
pub fn game_state(input: TokenStream) -> TokenStream {
let ast = syn::parse::<syn::DeriveInput>(input).unwrap();
let name = &ast.ident;
let gen = quote! {
impl bevy_controls::contract::GameState for #name {}
impl bevy_controls::contract::GameStateInner for #name {}
};
gen.into()
}