#Use enums in tauri::command

19 messages · Page 1 of 1 (latest)

toxic spire
#

Sorry to hijack this post. How would I supply an enum that takes another enum as an argument to the invoke call in js?

#

Do I need to provide the json string representation?

worn tree
#

please give some example for better understand xD

toxic spire
#
pub enum HWRequest {
    Hello,
    ReqState,
    Initialise,
    Uninitialise,
    Cmd(HWCmd)
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum HWCmd {
    Laser(usize, LaserCmd),
    Test,
}```
#
fn hw_request(hwrequest: HWRequest, iswrapper: State<ISWrapper>) -> Result<(), Error> {
///
#
    const cmdstr = "Cmd(Test)"
    await invoke("hw_request", {hwrequest: cmdstr});
}```
#

I'd like to be able to do something like this in js.

#

But I suspect I need to craft the json in js.

worn tree
#

by default it's:

async function laser_on(idx) {
    await invoke("hw_request", {hwrequest: {"Cmd": "Test"});
}
toxic spire
#

Oh

#

How did I not try that.

#

Thank you very much. That works.

worn tree
#

for things like this, you could serialize it and then look how serde do, xD

worn tree
toxic spire
#

I was able to see how serde do and craft a json string that was deserialised. But I couldn't figure out how to pass the enums as arguments through invoke.

#

This is much more elegant solution.