@@ -6,56 +6,39 @@ use std::collections::HashMap;
6
6
use syn:: { Ident , ItemImpl , Lit } ;
7
7
8
8
use crate :: function:: { Args , CallType , Function , MethodReceiver } ;
9
- use crate :: helpers:: get_docs;
9
+ use crate :: helpers:: { get_docs, 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,7 +263,7 @@ 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 ( ) ) ;
266
+ let name = method. sig . ident . to_string ( ) . renmae ( & self . rename ) ;
284
267
let docs = get_docs ( & method. attrs ) ;
285
268
let mut opts = MethodArgs :: new ( name) ;
286
269
opts. parse ( & mut method. attrs ) ?;
@@ -396,40 +379,24 @@ impl quote::ToTokens for FnBuilder {
396
379
397
380
#[ cfg( test) ]
398
381
mod tests {
382
+ use super :: RenameMethod ;
399
383
use super :: RenameRule ;
384
+ use super :: MAGIC_METHOD ;
400
385
401
386
#[ test]
402
387
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) ) ;
388
+ for magic in MAGIC_METHOD {
389
+ assert_eq ! ( magic, magic. rename_method( & RenameRule :: None ) ) ;
390
+ assert_eq ! ( magic, magic. rename_method( & RenameRule :: Camel ) ) ;
391
+ assert_eq ! ( magic, magic. rename_method( & RenameRule :: Snake ) ) ;
425
392
}
426
393
}
427
394
428
395
#[ test]
429
396
fn test_rename_php_methods ( ) {
430
397
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 ) ) ;
398
+ assert_eq ! ( original, original . rename_method ( & RenameRule :: None ) ) ;
399
+ assert_eq ! ( camel, original . rename_method ( & RenameRule :: Camel ) ) ;
400
+ assert_eq ! ( snake, original . rename_method ( & RenameRule :: Snake ) ) ;
434
401
}
435
402
}
0 commit comments