Skip to content

Commit

Permalink
Fix plugin macros (Pumpkin-MC#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
vyPal authored and urisinger committed Feb 16, 2025
1 parent 4a31ac4 commit ef6814c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 32 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
resolver = "2"
members = [
"pumpkin-api-macros",
"pumpkin-config",
"pumpkin-util",
"pumpkin-inventory",
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-api-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ proc-macro = true
[dependencies]
syn = { version = "2.0", features = ["full"] }
quote = "1.0"
proc-macro2 = "1.0"
proc-macro2 = "1.0"
37 changes: 6 additions & 31 deletions pumpkin-api-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
use std::sync::LazyLock;
use proc_macro::TokenStream;
use quote::quote;
use std::collections::HashMap;
use std::sync::LazyLock;
use std::sync::Mutex;
use syn::{parse_macro_input, parse_quote, ImplItem, ItemFn, ItemImpl, ItemStruct};

static PLUGIN_METHODS: LazyLock<Mutex<HashMap<String, Vec<String>>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));
static PLUGIN_METHODS: LazyLock<Mutex<Vec<String>>> = LazyLock::new(|| Mutex::new(Vec::new()));

#[proc_macro_attribute]
pub fn plugin_method(attr: TokenStream, item: TokenStream) -> TokenStream {
pub fn plugin_method(_attr: TokenStream, item: TokenStream) -> TokenStream {
let input_fn = parse_macro_input!(item as ItemFn);
let fn_name = &input_fn.sig.ident;
let fn_inputs = &input_fn.sig.inputs;
let fn_output = &input_fn.sig.output;
let fn_body = &input_fn.block;

let struct_name = if attr.is_empty() {
"MyPlugin".to_string()
} else {
attr.to_string().trim().to_string()
};

let method = quote! {
#[allow(unused_mut)]
async fn #fn_name(#fn_inputs) #fn_output {
Expand All @@ -32,35 +24,18 @@ pub fn plugin_method(attr: TokenStream, item: TokenStream) -> TokenStream {
}
.to_string();

PLUGIN_METHODS
.lock()
.unwrap()
.entry(struct_name)
.or_default()
.push(method);
PLUGIN_METHODS.lock().unwrap().push(method);

TokenStream::new()
}

#[proc_macro_attribute]
pub fn plugin_impl(attr: TokenStream, item: TokenStream) -> TokenStream {
pub fn plugin_impl(_attr: TokenStream, item: TokenStream) -> TokenStream {
// Parse the input struct
let input_struct = parse_macro_input!(item as ItemStruct);
let struct_ident = &input_struct.ident;

// Get the custom name from attribute or use the struct's name
let struct_name = if attr.is_empty() {
struct_ident.clone()
} else {
let attr_str = attr.to_string();
quote::format_ident!("{}", attr_str.trim())
};

let methods = PLUGIN_METHODS
.lock()
.unwrap()
.remove(&struct_name.to_string())
.unwrap_or_default();
let methods = PLUGIN_METHODS.lock().unwrap();

let methods: Vec<proc_macro2::TokenStream> = methods
.iter()
Expand Down

0 comments on commit ef6814c

Please sign in to comment.