Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix plugin macros #553

Merged
merged 1 commit into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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