tauri-apps / tauri-docs

The source for all Tauri project documentation.
https://tauri.app
MIT License
800 stars 593 forks source link

[feat] Deeply Rename Properties before/after invoke #2732

Open bradleat opened 1 month ago

bradleat commented 1 month ago

Describe the problem

I have to manually rename objects properties from camelCase to snake_case to pass them to tauri invoke (also on the way back).

For instance, if I have rust code like this:

struct Example {
   pub one_thing: f64;
}

#[tauri::command]
fn echo_example(example_thing: Example) => Result<Example, String> {
   OK(example_thing)
}

Then in javascript I have to do this:


response = await invoke('echo_example`, {
   exampleThing: {
      one_thing: 10.5
   }
})

// format response again

formattedResponse = {
  oneThing: response.one_thing
}

You can imagine this is annnoying for deeply nested structs, objects, esp involving arrays/vecs

Describe the solution you'd like

I'd like to have the case conversion deeply applied.

Alternatives considered

https://serde.rs/attr-rename.html

Additional context

Perhaps the Usage of Serde should be suggested in the documentation. No response

amrbashir commented 2 weeks ago

Just add #[serde(rename_all = "camelCase")] on your struct

I believe this is more of a documentation issue, so I will just move to tauri-docs