1
1
use syn:: { spanned:: Spanned , FnArg , ImplItem , ItemImpl , Pat , PatIdent , Signature , Type } ;
2
2
3
3
use proc_macro:: TokenStream ;
4
+ use quote:: { quote, ToTokens } ;
4
5
use std:: boxed:: Box ;
5
6
7
+ #[ derive( Clone , Eq , PartialEq , Ord , PartialOrd , Hash , Debug ) ]
8
+ pub enum RpcMode {
9
+ Disabled ,
10
+ Remote ,
11
+ RemoteSync ,
12
+ Master ,
13
+ Puppet ,
14
+ MasterSync ,
15
+ PuppetSync ,
16
+ }
17
+
18
+ impl Default for RpcMode {
19
+ fn default ( ) -> Self {
20
+ RpcMode :: Disabled
21
+ }
22
+ }
23
+
24
+ impl ToTokens for RpcMode {
25
+ fn to_tokens ( & self , tokens : & mut proc_macro2:: TokenStream ) {
26
+ match self {
27
+ RpcMode :: Disabled => tokens. extend ( quote ! ( RpcMode :: Disabled ) ) ,
28
+ RpcMode :: Remote => tokens. extend ( quote ! ( RpcMode :: Remote ) ) ,
29
+ RpcMode :: RemoteSync => tokens. extend ( quote ! ( RpcMode :: RemoteSync ) ) ,
30
+ RpcMode :: Master => tokens. extend ( quote ! ( RpcMode :: Master ) ) ,
31
+ RpcMode :: Puppet => tokens. extend ( quote ! ( RpcMode :: Puppet ) ) ,
32
+ RpcMode :: MasterSync => tokens. extend ( quote ! ( RpcMode :: MasterSync ) ) ,
33
+ RpcMode :: PuppetSync => tokens. extend ( quote ! ( RpcMode :: PuppetSync ) ) ,
34
+ }
35
+ }
36
+ }
37
+
6
38
pub ( crate ) struct ClassMethodExport {
7
39
pub ( crate ) class_ty : Box < Type > ,
8
40
pub ( crate ) methods : Vec < ExportMethod > ,
@@ -12,12 +44,12 @@ pub(crate) struct ClassMethodExport {
12
44
pub ( crate ) struct ExportMethod {
13
45
pub ( crate ) sig : Signature ,
14
46
pub ( crate ) args : ExportArgs ,
15
- pub ( crate ) rpc : String ,
16
47
}
17
48
18
49
#[ derive( Clone , Eq , PartialEq , Ord , PartialOrd , Hash , Debug , Default ) ]
19
50
pub ( crate ) struct ExportArgs {
20
51
pub ( crate ) optional_args : Option < usize > ,
52
+ pub ( crate ) rpc_mode : RpcMode ,
21
53
}
22
54
23
55
pub ( crate ) fn derive_methods ( meta : TokenStream , input : TokenStream ) -> TokenStream {
@@ -34,7 +66,7 @@ pub(crate) fn derive_methods(meta: TokenStream, input: TokenStream) -> TokenStre
34
66
let methods = export
35
67
. methods
36
68
. into_iter ( )
37
- . map ( |ExportMethod { sig, args, rpc } | {
69
+ . map ( |ExportMethod { sig, args } | {
38
70
let sig_span = sig. ident . span ( ) ;
39
71
40
72
let name = sig. ident ;
@@ -70,6 +102,8 @@ pub(crate) fn derive_methods(meta: TokenStream, input: TokenStream) -> TokenStre
70
102
None => 0 ,
71
103
} ;
72
104
105
+ let rpc = args. rpc_mode ;
106
+
73
107
let args = sig. inputs . iter ( ) . enumerate ( ) . map ( |( n, arg) | {
74
108
let span = arg. span ( ) ;
75
109
if n < arg_count - optional_args {
@@ -86,7 +120,7 @@ pub(crate) fn derive_methods(meta: TokenStream, input: TokenStream) -> TokenStre
86
120
fn #name ( #( #args ) * ) -> #ret_ty
87
121
) ;
88
122
89
- #builder. add_method ( #name_string, method, #rpc) ;
123
+ #builder. add_method_with_rpc_mode ( #name_string, method, #rpc) ;
90
124
}
91
125
)
92
126
} )
@@ -154,7 +188,7 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
154
188
let items = match func {
155
189
ImplItem :: Method ( mut method) => {
156
190
let mut export_args = None ;
157
- let mut rpc = "disabled" ;
191
+ let mut rpc = RpcMode :: Disabled ;
158
192
159
193
let mut errors = vec ! [ ] ;
160
194
@@ -176,7 +210,6 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
176
210
if let Some ( "export" ) = last_seg. as_deref ( ) {
177
211
let _export_args = export_args. get_or_insert_with ( ExportArgs :: default) ;
178
212
if !attr. tokens . is_empty ( ) {
179
- use quote:: ToTokens ;
180
213
use syn:: { Meta , MetaNameValue , NestedMeta } ;
181
214
182
215
let meta = match attr. parse_meta ( ) {
@@ -253,31 +286,31 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
253
286
254
287
match value. as_str ( ) {
255
288
"remote" => {
256
- rpc = "remote" ;
289
+ rpc = RpcMode :: Remote ;
257
290
return false ;
258
291
}
259
- "remotesync " => {
260
- rpc = "remotesync" ;
292
+ "remote_sync " => {
293
+ rpc = RpcMode :: RemoteSync ;
261
294
return false ;
262
295
}
263
296
"master" => {
264
- rpc = "master" ;
297
+ rpc = RpcMode :: Master ;
265
298
return false ;
266
299
}
267
300
"puppet" => {
268
- rpc = "puppet" ;
301
+ rpc = RpcMode :: Puppet ;
269
302
return false ;
270
303
}
271
304
"disabled" => {
272
- rpc = "disabled" ;
305
+ rpc = RpcMode :: Disabled ;
273
306
return false ;
274
307
}
275
- "mastersync " => {
276
- rpc = "mastersync" ;
308
+ "master_sync " => {
309
+ rpc = RpcMode :: MasterSync ;
277
310
return false ;
278
311
}
279
- "puppetsync " => {
280
- rpc = "puppetsync" ;
312
+ "puppet_sync " => {
313
+ rpc = RpcMode :: PuppetSync ;
281
314
return false ;
282
315
}
283
316
_ => {
@@ -347,11 +380,11 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
347
380
}
348
381
349
382
export_args. optional_args = optional_args;
383
+ export_args. rpc_mode = rpc;
350
384
351
385
methods_to_export. push ( ExportMethod {
352
386
sig : method. sig . clone ( ) ,
353
387
args : export_args,
354
- rpc : rpc. to_string ( ) ,
355
388
} ) ;
356
389
}
357
390
0 commit comments