|
1 | 1 | use anyhow::{anyhow, bail, Result}; |
| 2 | +use ident_case::RenameRule; |
2 | 3 | use quote::ToTokens; |
3 | 4 | use std::collections::HashMap; |
4 | 5 |
|
@@ -34,6 +35,29 @@ pub struct Method { |
34 | 35 | pub visibility: Visibility, |
35 | 36 | } |
36 | 37 |
|
| 38 | +fn camel_case_identifier(field: &str) -> String { |
| 39 | + match field { |
| 40 | + "__construct" => "__construct".to_string(), |
| 41 | + "__destruct" => "__destruct".to_string(), |
| 42 | + "__call" => "__call".to_string(), |
| 43 | + "__call_static" => "__callStatic".to_string(), |
| 44 | + "__get" => "__get".to_string(), |
| 45 | + "__set" => "__set".to_string(), |
| 46 | + "__isset" => "__isset".to_string(), |
| 47 | + "__unset" => "__unset".to_string(), |
| 48 | + "__sleep" => "__sleep".to_string(), |
| 49 | + "__wakeup" => "__wakeup".to_string(), |
| 50 | + "__serialize" => "__serialize".to_string(), |
| 51 | + "__unserialize" => "__unserialize".to_string(), |
| 52 | + "__to_string" => "__toString".to_string(), |
| 53 | + "__invoke" => "__invoke".to_string(), |
| 54 | + "__set_state" => "__set_state".to_string(), |
| 55 | + "__clone" => "__clone".to_string(), |
| 56 | + "__debug_info" => "__debugInfo".to_string(), |
| 57 | + field => RenameRule::CamelCase.apply_to_field(field), |
| 58 | + } |
| 59 | +} |
| 60 | + |
37 | 61 | pub fn parser(input: &mut ImplItemMethod) -> Result<(TokenStream, Method)> { |
38 | 62 | let mut defaults = HashMap::new(); |
39 | 63 | let mut optional = None; |
@@ -93,7 +117,7 @@ pub fn parser(input: &mut ImplItemMethod) -> Result<(TokenStream, Method)> { |
93 | 117 | }; |
94 | 118 |
|
95 | 119 | let method = Method { |
96 | | - name: ident.to_string(), |
| 120 | + name: camel_case_identifier(&ident.to_string()), |
97 | 121 | ident: internal_ident.to_string(), |
98 | 122 | args, |
99 | 123 | optional, |
@@ -247,3 +271,34 @@ impl Method { |
247 | 271 | .to_token_stream() |
248 | 272 | } |
249 | 273 | } |
| 274 | + |
| 275 | +#[cfg(test)] |
| 276 | +mod tests { |
| 277 | + use super::camel_case_identifier; |
| 278 | + |
| 279 | + #[test] |
| 280 | + fn test_rename_php_methods() { |
| 281 | + for &(original, expected) in &[ |
| 282 | + ("__construct", "__construct"), |
| 283 | + ("__destruct", "__destruct"), |
| 284 | + ("__call", "__call"), |
| 285 | + ("__call_static", "__callStatic"), |
| 286 | + ("__get", "__get"), |
| 287 | + ("__set", "__set"), |
| 288 | + ("__isset", "__isset"), |
| 289 | + ("__unset", "__unset"), |
| 290 | + ("__sleep", "__sleep"), |
| 291 | + ("__wakeup", "__wakeup"), |
| 292 | + ("__serialize", "__serialize"), |
| 293 | + ("__unserialize", "__unserialize"), |
| 294 | + ("__to_string", "__toString"), |
| 295 | + ("__invoke", "__invoke"), |
| 296 | + ("__set_state", "__set_state"), |
| 297 | + ("__clone", "__clone"), |
| 298 | + ("__debug_info", "__debugInfo"), |
| 299 | + ("get_name", "getName"), |
| 300 | + ] { |
| 301 | + assert_eq!(camel_case_identifier(original), expected); |
| 302 | + } |
| 303 | + } |
| 304 | +} |
0 commit comments