Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
0x53A committed Nov 28, 2024
1 parent 2257921 commit bfb578f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
2 changes: 0 additions & 2 deletions godot-macros/src/class/data_models/inherent_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,10 @@ pub fn transform_inherent_impl(
// We are the primary `impl` block.

let storage = quote! {
#[used]
#[allow(non_upper_case_globals)]
#[doc(hidden)]
static #method_storage_name: std::sync::Mutex<Vec<fn()>> = std::sync::Mutex::new(Vec::new());

#[used]
#[allow(non_upper_case_globals)]
#[doc(hidden)]
static #constants_storage_name: std::sync::Mutex<Vec<fn()>> = std::sync::Mutex::new(Vec::new());
Expand Down
12 changes: 3 additions & 9 deletions godot-macros/src/class/godot_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,13 @@
use proc_macro2::TokenStream;

use crate::class::{transform_inherent_impl, transform_trait_impl};
use crate::util::{bail, KvParser};
use crate::util::{bail, venial_parse_meta, KvParser};
use crate::ParseResult;

use quote::quote;
use quote::{format_ident, quote};

fn parse_inherent_impl_attr(meta: TokenStream) -> Result<super::InherentImplAttr, venial::Error> {
// Hack because venial doesn't support direct meta parsing yet.
let input = quote! {
#[godot_api(#meta)]
fn func();
};

let item = venial::parse_item(input)?;
let item = venial_parse_meta(&meta, format_ident!("godot_api"), &quote! { fn func(); })?;
let mut attr = KvParser::parse_required(item.attributes(), "godot_api", &meta)?;
let secondary = attr.handle_alone("secondary")?;
attr.finish()?;
Expand Down
8 changes: 1 addition & 7 deletions godot-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,13 +988,7 @@ where
let input2 = TokenStream2::from(input);
let meta2 = TokenStream2::from(meta);

// Hack because venial doesn't support direct meta parsing yet
let input = quote! {
#[#self_name(#meta2)]
#input2
};

let result2 = venial::parse_item(input)
let result2 = util::venial_parse_meta(&meta2, self_name, &input2)
.and_then(transform)
.unwrap_or_else(|e| e.to_compile_error());

Expand Down
18 changes: 18 additions & 0 deletions godot-macros/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,21 @@ pub fn safe_ident(s: &str) -> Ident {
_ => ident(s)
}
}

// ----------------------------------------------------------------------------------------------------------------------------------------------

/// Parses a `meta` TokenStream, that is, the tokens in parameter position of a proc-macro (between the braces).
/// Because venial can't actually parse a meta item directly, this is done by reconstructing the full macro attribute on top of some content and then parsing *that*.
pub fn venial_parse_meta(
meta: &TokenStream,
self_name: Ident,
content: &TokenStream,
) -> Result<venial::Item, venial::Error> {
// Hack because venial doesn't support direct meta parsing yet
let input = quote! {
#[#self_name(#meta)]
#content
};

venial::parse_item(input)
}

0 comments on commit bfb578f

Please sign in to comment.