@@ -5,57 +5,40 @@ use quote::quote;
5
5
use std:: collections:: HashMap ;
6
6
use syn:: { Ident , ItemImpl , Lit } ;
7
7
8
- use crate :: function:: { Args , CallType , Function , MethodReceiver } ;
9
- use crate :: helpers:: get_docs;
8
+ use crate :: function:: { Args , CallType , FnArgs , Function , MethodReceiver } ;
9
+ use crate :: helpers:: { get_docs, GetDocs , Rename , RenameRule } ;
10
10
use crate :: prelude:: * ;
11
11
12
- #[ derive( Debug , Copy , Clone , FromMeta , Default ) ]
13
- pub enum RenameRule {
14
- /// Methods won't be renamed.
15
- #[ darling( rename = "none" ) ]
16
- None ,
17
- /// Methods will be converted to camelCase.
18
- #[ darling( rename = "camelCase" ) ]
19
- #[ default]
20
- Camel ,
21
- /// Methods will be converted to snake_case.
22
- #[ darling( rename = "snake_case" ) ]
23
- Snake ,
12
+ const MAGIC_METHOD : [ & ' static str ; 17 ] = [
13
+ "__construct" ,
14
+ "__destruct" ,
15
+ "__call" ,
16
+ "__call_static" ,
17
+ "__get" ,
18
+ "__set" ,
19
+ "__isset" ,
20
+ "__unset" ,
21
+ "__sleep" ,
22
+ "__wakeup" ,
23
+ "__serialize" ,
24
+ "__unserialize" ,
25
+ "__to_string" ,
26
+ "__invoke" ,
27
+ "__set_state" ,
28
+ "__clone" ,
29
+ "__debug_info" ,
30
+ ] ;
31
+
32
+ trait RenameMethod {
33
+ fn rename_method ( self , rule : & RenameRule ) -> String ;
24
34
}
25
35
26
- impl RenameRule {
27
- /// Change case of an identifier.
28
- ///
29
- /// Magic methods are handled specially to make sure they're always cased
30
- /// correctly.
31
- pub fn rename ( & self , name : impl AsRef < str > ) -> String {
32
- let name = name. as_ref ( ) ;
33
- match self {
34
- RenameRule :: None => name. to_string ( ) ,
35
- rule => match name {
36
- "__construct" => "__construct" . to_string ( ) ,
37
- "__destruct" => "__destruct" . to_string ( ) ,
38
- "__call" => "__call" . to_string ( ) ,
39
- "__call_static" => "__callStatic" . to_string ( ) ,
40
- "__get" => "__get" . to_string ( ) ,
41
- "__set" => "__set" . to_string ( ) ,
42
- "__isset" => "__isset" . to_string ( ) ,
43
- "__unset" => "__unset" . to_string ( ) ,
44
- "__sleep" => "__sleep" . to_string ( ) ,
45
- "__wakeup" => "__wakeup" . to_string ( ) ,
46
- "__serialize" => "__serialize" . to_string ( ) ,
47
- "__unserialize" => "__unserialize" . to_string ( ) ,
48
- "__to_string" => "__toString" . to_string ( ) ,
49
- "__invoke" => "__invoke" . to_string ( ) ,
50
- "__set_state" => "__set_state" . to_string ( ) ,
51
- "__clone" => "__clone" . to_string ( ) ,
52
- "__debug_info" => "__debugInfo" . to_string ( ) ,
53
- field => match rule {
54
- Self :: Camel => ident_case:: RenameRule :: CamelCase . apply_to_field ( field) ,
55
- Self :: Snake => ident_case:: RenameRule :: SnakeCase . apply_to_field ( field) ,
56
- Self :: None => unreachable ! ( ) ,
57
- } ,
58
- } ,
36
+ impl < T : AsRef < str > > RenameMethod for T {
37
+ fn rename_method ( self , rule : & RenameRule ) -> String {
38
+ if MAGIC_METHOD . contains ( & self . as_ref ( ) ) {
39
+ self . as_ref ( ) . to_string ( )
40
+ } else {
41
+ self . as_ref ( ) . to_string ( ) . renmae ( rule)
59
42
}
60
43
}
61
44
}
@@ -280,8 +263,9 @@ impl<'a> ParsedImpl<'a> {
280
263
} ) ;
281
264
}
282
265
syn:: ImplItem :: Fn ( method) => {
283
- let name = self . rename . rename ( method. sig . ident . to_string ( ) ) ;
284
- let docs = get_docs ( & method. attrs ) ;
266
+ let name = method. sig . ident . to_string ( ) . renmae ( & self . rename ) ;
267
+ let docs = method. attrs . as_slice ( ) . get_docs ( ) ;
268
+
285
269
let mut opts = MethodArgs :: new ( name) ;
286
270
opts. parse ( & mut method. attrs ) ?;
287
271
@@ -396,40 +380,24 @@ impl quote::ToTokens for FnBuilder {
396
380
397
381
#[ cfg( test) ]
398
382
mod tests {
383
+ use super :: RenameMethod ;
399
384
use super :: RenameRule ;
385
+ use super :: MAGIC_METHOD ;
400
386
401
387
#[ test]
402
388
fn test_rename_magic ( ) {
403
- for & ( magic, expected) in & [
404
- ( "__construct" , "__construct" ) ,
405
- ( "__destruct" , "__destruct" ) ,
406
- ( "__call" , "__call" ) ,
407
- ( "__call_static" , "__callStatic" ) ,
408
- ( "__get" , "__get" ) ,
409
- ( "__set" , "__set" ) ,
410
- ( "__isset" , "__isset" ) ,
411
- ( "__unset" , "__unset" ) ,
412
- ( "__sleep" , "__sleep" ) ,
413
- ( "__wakeup" , "__wakeup" ) ,
414
- ( "__serialize" , "__serialize" ) ,
415
- ( "__unserialize" , "__unserialize" ) ,
416
- ( "__to_string" , "__toString" ) ,
417
- ( "__invoke" , "__invoke" ) ,
418
- ( "__set_state" , "__set_state" ) ,
419
- ( "__clone" , "__clone" ) ,
420
- ( "__debug_info" , "__debugInfo" ) ,
421
- ] {
422
- assert_eq ! ( magic, RenameRule :: None . rename( magic) ) ;
423
- assert_eq ! ( expected, RenameRule :: Camel . rename( magic) ) ;
424
- assert_eq ! ( expected, RenameRule :: Snake . rename( magic) ) ;
389
+ for magic in MAGIC_METHOD {
390
+ assert_eq ! ( magic, magic. rename_method( & RenameRule :: None ) ) ;
391
+ assert_eq ! ( magic, magic. rename_method( & RenameRule :: Camel ) ) ;
392
+ assert_eq ! ( magic, magic. rename_method( & RenameRule :: Snake ) ) ;
425
393
}
426
394
}
427
395
428
396
#[ test]
429
397
fn test_rename_php_methods ( ) {
430
398
let & ( original, camel, snake) = & ( "get_name" , "getName" , "get_name" ) ;
431
- assert_eq ! ( original, RenameRule :: None . rename ( original ) ) ;
432
- assert_eq ! ( camel, RenameRule :: Camel . rename ( original ) ) ;
433
- assert_eq ! ( snake, RenameRule :: Snake . rename ( original ) ) ;
399
+ assert_eq ! ( original, original . rename_method ( & RenameRule :: None ) ) ;
400
+ assert_eq ! ( camel, original . rename_method ( & RenameRule :: Camel ) ) ;
401
+ assert_eq ! ( snake, original . rename_method ( & RenameRule :: Snake ) ) ;
434
402
}
435
403
}
0 commit comments