|
| 1 | +use darling::{util::Flag, FromDeriveInput, FromMeta}; |
| 2 | +use proc_macro2::Ident; |
| 3 | +use std::ops::{Deref, DerefMut}; |
| 4 | +use syn::{spanned::Spanned, visit_mut::VisitMut, Attribute, Field, TraitItemFn, Variant}; |
| 5 | + |
| 6 | +#[derive(FromMeta)] |
| 7 | +pub struct BMSCorePath(pub syn::Path); |
| 8 | + |
| 9 | +impl Default for BMSCorePath { |
| 10 | + fn default() -> Self { |
| 11 | + Self(syn::parse_quote!(bevy_mod_scripting::core)) |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(FromMeta)] |
| 16 | +pub struct BMSLuaPath(pub syn::Path); |
| 17 | + |
| 18 | +impl Default for BMSLuaPath { |
| 19 | + fn default() -> Self { |
| 20 | + Self(syn::parse_quote!(bevy_mod_scripting::lua)) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(FromDeriveInput)] |
| 25 | +#[darling(attributes(proxy), forward_attrs(allow, doc, cfg))] |
| 26 | +pub struct ProxyInput { |
| 27 | + /// The name of the type for which we are generating a proxy (target type) |
| 28 | + pub ident: syn::Ident, |
| 29 | + /// The visibility of the target type |
| 30 | + pub vis: syn::Visibility, |
| 31 | + /// The generics on the target type |
| 32 | + pub generics: syn::Generics, |
| 33 | + /// The attributes on the target type |
| 34 | + pub attrs: Vec<Attribute>, |
| 35 | + |
| 36 | + /// The path to the type for which we are generating a proxy if it's a foreign type |
| 37 | + pub remote: Option<syn::Path>, |
| 38 | + |
| 39 | + /// if provided will call the function at this path to get the world callback access. Normally this is retrieved using a global variable. |
| 40 | + pub get_world_callback_access_fn: Option<syn::Path>, |
| 41 | + |
| 42 | + /// If set will use the given path as the type for the proxy instead of generating a new one |
| 43 | + /// Only used for the special world proxies, probably not useful for anything else, the macro assumes we have an inner ReflectReference in the wrapper |
| 44 | + pub proxy_as_type: Option<syn::Path>, |
| 45 | + |
| 46 | + /// The path to the bevy_mod_scripting_core crate |
| 47 | + #[darling(default)] |
| 48 | + pub bms_core_path: BMSCorePath, |
| 49 | + /// The path to the bevy_mod_scripting_lua crate |
| 50 | + #[darling(default)] |
| 51 | + pub bms_lua_path: BMSLuaPath, |
| 52 | + |
| 53 | + /// The name to use for the proxy type, if not provided the language derive macro |
| 54 | + /// will generate one using a standard prefix. |
| 55 | + #[darling(rename = "name")] |
| 56 | + pub proxy_name: Option<Ident>, |
| 57 | + |
| 58 | + /// The body of the type for which we are generating a proxy |
| 59 | + pub data: darling::ast::Data<Variant, Field>, |
| 60 | + |
| 61 | + /// A list of multi-lang function definitions to be generated on the proxy type |
| 62 | + #[darling(default)] |
| 63 | + pub functions: TraitItemFnsWrapper, |
| 64 | +} |
| 65 | + |
| 66 | +#[derive(Default)] |
| 67 | +pub struct TraitItemFnsWrapper(pub Vec<TraitItemFn>); |
| 68 | + |
| 69 | +impl FromMeta for TraitItemFnsWrapper { |
| 70 | + fn from_string(value: &str) -> darling::Result<Self> { |
| 71 | + let token_stream: proc_macro2::TokenStream = value.parse().map_err(syn::Error::from)?; |
| 72 | + let trait_items_vec = vec![syn::parse2(token_stream)?]; |
| 73 | + Ok(TraitItemFnsWrapper(trait_items_vec)) |
| 74 | + } |
| 75 | + |
| 76 | + fn from_list(items: &[darling::ast::NestedMeta]) -> darling::Result<Self> { |
| 77 | + Ok(TraitItemFnsWrapper( |
| 78 | + items |
| 79 | + .iter() |
| 80 | + .map(Self::from_nested_meta) |
| 81 | + .collect::<Result<Vec<_>, _>>()? |
| 82 | + .into_iter() |
| 83 | + .flat_map(|v| v.0.into_iter()) |
| 84 | + .collect::<Vec<_>>(), |
| 85 | + )) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl Deref for TraitItemFnsWrapper { |
| 90 | + type Target = Vec<TraitItemFn>; |
| 91 | + |
| 92 | + fn deref(&self) -> &Self::Target { |
| 93 | + &self.0 |
| 94 | + } |
| 95 | +} |
| 96 | +impl DerefMut for TraitItemFnsWrapper { |
| 97 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 98 | + &mut self.0 |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/// Replaces every occurence of an identifier with |
| 103 | +/// the given string while preserving the original span |
| 104 | +pub struct IdentifierRenamingVisitor<'a> { |
| 105 | + pub target: &'a str, |
| 106 | + pub replacement: &'a str, |
| 107 | +} |
| 108 | + |
| 109 | +impl VisitMut for IdentifierRenamingVisitor<'_> { |
| 110 | + fn visit_ident_mut(&mut self, i: &mut Ident) { |
| 111 | + if *i == self.target { |
| 112 | + *i = Ident::new(self.replacement, i.span()); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments