@@ -46,16 +46,20 @@ pub struct PhpRename {
4646}
4747
4848impl PhpRename {
49- pub fn rename ( & self , name : impl AsRef < str > ) -> String {
50- self . name . as_ref ( ) . map_or_else (
51- || {
52- let name = name. as_ref ( ) ;
53- self . rename
54- . as_ref ( )
55- . map_or_else ( || name. to_string ( ) , |r| name. rename ( * r) )
56- } ,
57- ToString :: to_string,
58- )
49+ pub fn rename ( & self , name : impl AsRef < str > , default : RenameRule ) -> String {
50+ if let Some ( name) = self . name . as_ref ( ) {
51+ return name. to_string ( ) ;
52+ }
53+
54+ name. as_ref ( ) . rename ( self . rename . unwrap_or ( default) )
55+ }
56+
57+ pub fn rename_method ( & self , name : impl AsRef < str > , default : RenameRule ) -> String {
58+ if let Some ( name) = self . name . as_ref ( ) {
59+ return name. to_string ( ) ;
60+ }
61+
62+ name. as_ref ( ) . rename_method ( self . rename . unwrap_or ( default) )
5963 }
6064}
6165
@@ -93,32 +97,26 @@ impl RenameRule {
9397 }
9498}
9599
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 {
100+ impl < T > Rename for T
101+ where
102+ T : ToString ,
103+ {
103104 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)
105+ rule. rename ( self . to_string ( ) )
112106 }
113107}
114108
115- impl MethodRename for & str {
109+ impl < T > MethodRename for T
110+ where
111+ T : ToString + Rename ,
112+ {
116113 fn rename_method ( & self , rule : RenameRule ) -> String {
114+ let original = self . to_string ( ) ;
117115 match rule {
118- RenameRule :: None => ( * self ) . to_string ( ) ,
116+ RenameRule :: None => original ,
119117 _ => {
120- if MAGIC_METHOD . contains ( self ) {
121- match * self {
118+ if MAGIC_METHOD . contains ( & original . as_str ( ) ) {
119+ match original . as_str ( ) {
122120 "__to_string" => "__toString" . to_string ( ) ,
123121 "__debug_info" => "__debugInfo" . to_string ( ) ,
124122 "__call_static" => "__callStatic" . to_string ( ) ,
@@ -144,33 +142,33 @@ mod tests {
144142 name : Some ( "test" . to_string ( ) ) ,
145143 rename : None ,
146144 } ;
147- assert_eq ! ( rename. rename( "test" ) , "test" ) ;
148- assert_eq ! ( rename. rename( "Test" ) , "test" ) ;
149- assert_eq ! ( rename. rename( "TEST" ) , "test" ) ;
145+ assert_eq ! ( rename. rename( "test" , RenameRule :: None ) , "test" ) ;
146+ assert_eq ! ( rename. rename( "Test" , RenameRule :: None ) , "test" ) ;
147+ assert_eq ! ( rename. rename( "TEST" , RenameRule :: None ) , "test" ) ;
150148
151149 let rename = PhpRename {
152150 name : None ,
153151 rename : Some ( RenameRule :: ScreamingSnakeCase ) ,
154152 } ;
155- assert_eq ! ( rename. rename( "test" ) , "TEST" ) ;
156- assert_eq ! ( rename. rename( "Test" ) , "TEST" ) ;
157- assert_eq ! ( rename. rename( "TEST" ) , "TEST" ) ;
153+ assert_eq ! ( rename. rename( "test" , RenameRule :: None ) , "TEST" ) ;
154+ assert_eq ! ( rename. rename( "Test" , RenameRule :: None ) , "TEST" ) ;
155+ assert_eq ! ( rename. rename( "TEST" , RenameRule :: None ) , "TEST" ) ;
158156
159157 let rename = PhpRename {
160158 name : Some ( "test" . to_string ( ) ) ,
161159 rename : Some ( RenameRule :: ScreamingSnakeCase ) ,
162160 } ;
163- assert_eq ! ( rename. rename( "test" ) , "test" ) ;
164- assert_eq ! ( rename. rename( "Test" ) , "test" ) ;
165- assert_eq ! ( rename. rename( "TEST" ) , "test" ) ;
161+ assert_eq ! ( rename. rename( "test" , RenameRule :: None ) , "test" ) ;
162+ assert_eq ! ( rename. rename( "Test" , RenameRule :: None ) , "test" ) ;
163+ assert_eq ! ( rename. rename( "TEST" , RenameRule :: None ) , "test" ) ;
166164
167165 let rename = PhpRename {
168166 name : None ,
169167 rename : None ,
170168 } ;
171- assert_eq ! ( rename. rename( "test" ) , "test" ) ;
172- assert_eq ! ( rename. rename( "Test" ) , "Test" ) ;
173- assert_eq ! ( rename. rename( "TEST" ) , "TEST" ) ;
169+ assert_eq ! ( rename. rename( "test" , RenameRule :: None ) , "test" ) ;
170+ assert_eq ! ( rename. rename( "Test" , RenameRule :: None ) , "Test" ) ;
171+ assert_eq ! ( rename. rename( "TEST" , RenameRule :: None ) , "TEST" ) ;
174172 }
175173
176174 #[ test]
0 commit comments