11use darling:: FromMeta ;
22
3+ const MAGIC_METHOD : [ & str ; 17 ] = [
4+ "__construct" ,
5+ "__destruct" ,
6+ "__call" ,
7+ "__call_static" ,
8+ "__get" ,
9+ "__set" ,
10+ "__isset" ,
11+ "__unset" ,
12+ "__sleep" ,
13+ "__wakeup" ,
14+ "__serialize" ,
15+ "__unserialize" ,
16+ "__to_string" ,
17+ "__invoke" ,
18+ "__set_state" ,
19+ "__clone" ,
20+ "__debug_info" ,
21+ ] ;
22+
323#[ derive( Debug , FromMeta ) ]
424pub enum Visibility {
525 #[ darling( rename = "public" ) ]
@@ -10,6 +30,14 @@ pub enum Visibility {
1030 Protected ,
1131}
1232
33+ pub trait Rename {
34+ fn rename ( & self , rule : & RenameRule ) -> String ;
35+ }
36+
37+ pub trait MethodRename : Rename {
38+ fn rename_method ( & self , rule : & RenameRule ) -> String ;
39+ }
40+
1341#[ derive( FromMeta , Debug , Default ) ]
1442#[ darling( default ) ]
1543pub struct PhpRename {
@@ -24,7 +52,7 @@ impl PhpRename {
2452 let name = name. as_ref ( ) ;
2553 self . rename
2654 . as_ref ( )
27- . map_or_else ( || name. to_string ( ) , |r| r . rename ( name ) )
55+ . map_or_else ( || name. to_string ( ) , |r| name . rename ( r ) )
2856 } ,
2957 ToString :: to_string,
3058 )
@@ -52,52 +80,58 @@ pub enum RenameRule {
5280}
5381
5482impl RenameRule {
55- /// Change case of an identifier.
56- ///
57- /// Magic methods are handled specially to make sure they're always cased
58- /// correctly.
59- pub fn rename ( self , name : impl AsRef < str > ) -> String {
60- let name = name. as_ref ( ) ;
61- match self {
62- RenameRule :: None => name. to_string ( ) ,
63- rule => match name {
64- "__construct" => "__construct" . to_string ( ) ,
65- "__destruct" => "__destruct" . to_string ( ) ,
66- "__call" => "__call" . to_string ( ) ,
67- "__call_static" => "__callStatic" . to_string ( ) ,
68- "__get" => "__get" . to_string ( ) ,
69- "__set" => "__set" . to_string ( ) ,
70- "__isset" => "__isset" . to_string ( ) ,
71- "__unset" => "__unset" . to_string ( ) ,
72- "__sleep" => "__sleep" . to_string ( ) ,
73- "__wakeup" => "__wakeup" . to_string ( ) ,
74- "__serialize" => "__serialize" . to_string ( ) ,
75- "__unserialize" => "__unserialize" . to_string ( ) ,
76- "__to_string" => "__toString" . to_string ( ) ,
77- "__invoke" => "__invoke" . to_string ( ) ,
78- "__set_state" => "__set_state" . to_string ( ) ,
79- "__clone" => "__clone" . to_string ( ) ,
80- "__debug_info" => "__debugInfo" . to_string ( ) ,
81- field => match rule {
82- Self :: Camel => ident_case:: RenameRule :: CamelCase . apply_to_field ( field) ,
83- Self :: Pascal => ident_case:: RenameRule :: PascalCase . apply_to_field ( field) ,
84- Self :: Snake => ident_case:: RenameRule :: SnakeCase . apply_to_field ( field) ,
85- Self :: ScreamingSnakeCase => {
86- ident_case:: RenameRule :: ScreamingSnakeCase . apply_to_field ( field)
87- }
88- Self :: None => unreachable ! ( ) ,
89- } ,
90- } ,
83+ fn rename ( & self , value : impl AsRef < str > ) -> String {
84+ match * self {
85+ Self :: None => value. as_ref ( ) . to_string ( ) ,
86+ Self :: Camel => ident_case:: RenameRule :: CamelCase . apply_to_field ( value. as_ref ( ) ) ,
87+ Self :: Pascal => ident_case:: RenameRule :: PascalCase . apply_to_field ( value. as_ref ( ) ) ,
88+ Self :: Snake => ident_case:: RenameRule :: SnakeCase . apply_to_field ( value. as_ref ( ) ) ,
89+ Self :: ScreamingSnakeCase => {
90+ ident_case:: RenameRule :: ScreamingSnakeCase . apply_to_field ( value. as_ref ( ) )
91+ }
92+ }
93+ }
94+ }
95+
96+ impl Rename for & str {
97+ fn rename ( & self , rule : & RenameRule ) -> String {
98+ rule. rename ( self )
99+ }
100+ }
101+
102+ impl Rename for syn:: Ident {
103+ fn rename ( & self , rule : & RenameRule ) -> String {
104+ let s = self . to_string ( ) ;
105+ rule. rename ( s)
106+ }
107+ }
108+
109+ impl MethodRename for syn:: Ident {
110+ fn rename_method ( & self , rule : & RenameRule ) -> String {
111+ self . to_string ( ) . as_str ( ) . rename_method ( rule)
112+ }
113+ }
114+
115+ impl MethodRename for & str {
116+ fn rename_method ( & self , rule : & RenameRule ) -> String {
117+ let s = self . to_string ( ) ;
118+
119+ if MAGIC_METHOD . contains ( & s. as_str ( ) ) {
120+ s
121+ } else {
122+ self . rename ( rule)
91123 }
92124 }
93125}
94126
95127#[ cfg( test) ]
96128mod tests {
97- use super :: { PhpRename , RenameRule } ;
129+ use crate :: parsing:: { MethodRename , Rename } ;
130+
131+ use super :: { PhpRename , RenameRule , MAGIC_METHOD } ;
98132
99133 #[ test]
100- fn test_php_rename ( ) {
134+ fn php_rename ( ) {
101135 let rename = PhpRename {
102136 name : Some ( "test" . to_string ( ) ) ,
103137 rename : None ,
@@ -132,45 +166,41 @@ mod tests {
132166 }
133167
134168 #[ test]
135- fn test_rename_magic ( ) {
136- for & ( magic, expected) in & [
137- ( "__construct" , "__construct" ) ,
138- ( "__destruct" , "__destruct" ) ,
139- ( "__call" , "__call" ) ,
140- ( "__call_static" , "__callStatic" ) ,
141- ( "__get" , "__get" ) ,
142- ( "__set" , "__set" ) ,
143- ( "__isset" , "__isset" ) ,
144- ( "__unset" , "__unset" ) ,
145- ( "__sleep" , "__sleep" ) ,
146- ( "__wakeup" , "__wakeup" ) ,
147- ( "__serialize" , "__serialize" ) ,
148- ( "__unserialize" , "__unserialize" ) ,
149- ( "__to_string" , "__toString" ) ,
150- ( "__invoke" , "__invoke" ) ,
151- ( "__set_state" , "__set_state" ) ,
152- ( "__clone" , "__clone" ) ,
153- ( "__debug_info" , "__debugInfo" ) ,
154- ] {
155- assert_eq ! ( magic, RenameRule :: None . rename( magic) ) ;
156- assert_eq ! ( expected, RenameRule :: Camel . rename( magic) ) ;
157- assert_eq ! ( expected, RenameRule :: Pascal . rename( magic) ) ;
158- assert_eq ! ( expected, RenameRule :: Snake . rename( magic) ) ;
159- assert_eq ! ( expected, RenameRule :: ScreamingSnakeCase . rename( magic) ) ;
169+ fn rename_magic_method ( ) {
170+ for magic in MAGIC_METHOD {
171+ assert_eq ! ( magic, magic. rename_method( & RenameRule :: None ) ) ;
172+ assert_eq ! ( magic, magic. rename_method( & RenameRule :: Camel ) ) ;
173+ assert_eq ! ( magic, magic. rename_method( & RenameRule :: Pascal ) ) ;
174+ assert_eq ! ( magic, magic. rename_method( & RenameRule :: Snake ) ) ;
175+ assert_eq ! ( magic, magic. rename_method( & RenameRule :: ScreamingSnakeCase ) ) ;
160176 }
161177 }
162178
163179 #[ test]
164- fn test_rename_php_methods ( ) {
180+ fn rename_method ( ) {
181+ let & ( original, camel, snake, pascal, screaming_snake) =
182+ & ( "get_name" , "getName" , "get_name" , "GetName" , "GET_NAME" ) ;
183+ assert_eq ! ( original, original. rename_method( & RenameRule :: None ) ) ;
184+ assert_eq ! ( camel, original. rename_method( & RenameRule :: Camel ) ) ;
185+ assert_eq ! ( pascal, original. rename_method( & RenameRule :: Pascal ) ) ;
186+ assert_eq ! ( snake, original. rename_method( & RenameRule :: Snake ) ) ;
187+ assert_eq ! (
188+ screaming_snake,
189+ original. rename_method( & RenameRule :: ScreamingSnakeCase )
190+ ) ;
191+ }
192+
193+ #[ test]
194+ fn rename ( ) {
165195 let & ( original, camel, snake, pascal, screaming_snake) =
166196 & ( "get_name" , "getName" , "get_name" , "GetName" , "GET_NAME" ) ;
167- assert_eq ! ( original, RenameRule :: None . rename ( original ) ) ;
168- assert_eq ! ( camel, RenameRule :: Camel . rename ( original ) ) ;
169- assert_eq ! ( pascal, RenameRule :: Pascal . rename ( original ) ) ;
170- assert_eq ! ( snake, RenameRule :: Snake . rename ( original ) ) ;
197+ assert_eq ! ( original, original . rename ( & RenameRule :: None ) ) ;
198+ assert_eq ! ( camel, original . rename ( & RenameRule :: Camel ) ) ;
199+ assert_eq ! ( pascal, original . rename ( & RenameRule :: Pascal ) ) ;
200+ assert_eq ! ( snake, original . rename ( & RenameRule :: Snake ) ) ;
171201 assert_eq ! (
172202 screaming_snake,
173- RenameRule :: ScreamingSnakeCase . rename ( original )
203+ original . rename ( & RenameRule :: ScreamingSnakeCase )
174204 ) ;
175205 }
176206}
0 commit comments