refactor(core): clearer case conversion + docs (#1754)

This commit is contained in:
chip 2021-05-09 14:02:26 -07:00 committed by GitHub
parent 665ec1d4a1
commit 2f4fca7567
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 21 deletions

View File

@ -154,10 +154,10 @@ fn parse_arg(command: &Ident, arg: &FnArg) -> syn::Result<TokenStream2> {
}
};
// we only support patterns that allow us to extract some sort of keyed identifier.
let key = match &mut arg {
// we only support patterns that allow us to extract some sort of keyed identifier
let mut key = match &mut arg {
Pat::Ident(arg) => arg.ident.to_string(),
Pat::Wild(_) => "_".into(),
Pat::Wild(_) => "".into(), // we always convert to camelCase, so "_" will end up empty anyways
Pat::Struct(s) => super::path_to_command(&mut s.path).ident.to_string(),
Pat::TupleStruct(s) => super::path_to_command(&mut s.path).ident.to_string(),
err => {
@ -176,7 +176,10 @@ fn parse_arg(command: &Ident, arg: &FnArg) -> syn::Result<TokenStream2> {
));
}
let key = snake_case_to_camel_case(key);
// snake_case -> camelCase
if key.as_str().contains('_') {
key = snake_case_to_camel_case(key.as_str());
}
Ok(quote!(::tauri::command::CommandArg::from_command(
::tauri::command::CommandItem {
@ -187,23 +190,18 @@ fn parse_arg(command: &Ident, arg: &FnArg) -> syn::Result<TokenStream2> {
)))
}
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),
}
}
/// Convert a snake_case string into camelCase, no underscores will be left.
fn snake_case_to_camel_case(key: &str) -> String {
let mut camel = String::with_capacity(key.len());
let mut to_upper = false;
camel
} else {
s
for c in key.chars() {
match c {
'_' => to_upper = true,
c if std::mem::take(&mut to_upper) => camel.push(c.to_ascii_uppercase()),
c => camel.push(c),
}
}
camel
}

View File

@ -59,6 +59,13 @@ macro_rules! pass {
fn $fn<V: Visitor<'de>>(self, $($arg: $argt),*) -> Result<V::Value, Self::Error> {
use serde::de::Error;
if self.key.is_empty() {
return Err(serde_json::Error::custom(format!(
"command {} has an argument with no name with a non-optional value",
self.name
)))
}
match self.message.payload.get(self.key) {
Some(value) => value.$fn($($arg),*),
None => {