From e84949524cb1d5a7ca090a941e7d941bfecf0279 Mon Sep 17 00:00:00 2001 From: chip Date: Sun, 9 May 2021 11:14:48 -0700 Subject: [PATCH] fix(core): convert command arguments to camel case (#1753) --- core/tauri-macros/src/command/wrapper.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/core/tauri-macros/src/command/wrapper.rs b/core/tauri-macros/src/command/wrapper.rs index b86d302ba..11156fc8c 100644 --- a/core/tauri-macros/src/command/wrapper.rs +++ b/core/tauri-macros/src/command/wrapper.rs @@ -176,6 +176,8 @@ fn parse_arg(command: &Ident, arg: &FnArg) -> syn::Result { )); } + let key = snake_case_to_camel_case(key); + Ok(quote!(::tauri::command::CommandArg::from_command( ::tauri::command::CommandItem { name: stringify!(#command), @@ -184,3 +186,24 @@ fn parse_arg(command: &Ident, arg: &FnArg) -> syn::Result { } ))) } + +fn snake_case_to_camel_case(s: String) -> String { + if s.as_str().contains('_') { + let mut camel = String::with_capacity(s.len()); + let mut to_upper = false; + for c in s.chars() { + match c { + '_' => to_upper = true, + c if to_upper => { + camel.push(c.to_ascii_uppercase()); + to_upper = false; + } + c => camel.push(c), + } + } + + camel + } else { + s + } +}