This repository has been archived on 2024-05-26. You can view files and clone it, but cannot push or open issues or pull requests.
core/lua_macros/src/lib.rs

64 lines
1.9 KiB
Rust

mod mark_as_ci_command;
mod struct_to_ci_enum;
use mark_as_ci_command::generate_final_function;
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use struct_to_ci_enum::{generate_command_enum, generate_generate_ci_function};
use syn::{self, ItemFn, Field, parse::Parser};
#[proc_macro_attribute]
pub fn turn_struct_to_ci_commands(_attrs: TokenStream, input: TokenStream) -> TokenStream {
// Construct a representation of Rust code as a syntax tree
// that we can manipulate
let input = syn::parse(input)
.expect("This should always be valid rust code, as it's extracted from direct code");
// Build the trait implementation
let generate_ci_function: TokenStream2 = generate_generate_ci_function(&input);
let command_enum = generate_command_enum(&input);
quote! {
#command_enum
#generate_ci_function
}
.into()
}
/// Generate a default lua function implementation.
// TODO: Is this needed?
#[proc_macro_attribute]
pub fn gen_lua_function(_attrs: TokenStream, input: TokenStream) -> TokenStream {
// Construct a representation of Rust code as a syntax tree
// that we can manipulate
let parser = Field::parse_named;
let input = parser.parse(input)
.expect("This is only defined for named fileds.");
quote! {
#input
}
.into()
}
/// Turn a function into a valid ci command function
#[proc_macro_attribute]
pub fn ci_command(_attrs: TokenStream, input: TokenStream) -> TokenStream {
// Construct a representation of Rust code as a syntax tree
// that we can manipulate
let mut input: ItemFn = syn::parse(input)
.expect("This should always be valid rust code, as it's extracted from direct code");
// Build the trait implementation
let output_function: TokenStream2 = generate_final_function(&mut input);
//panic!("{:#?}", output_function);
quote! {
#output_function
}
.into()
}