Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Handle std::result::Result within macro #1

Open
shimunn opened this issue Apr 18, 2023 · 2 comments
Open

Handle std::result::Result within macro #1

shimunn opened this issue Apr 18, 2023 · 2 comments

Comments

@shimunn
Copy link

shimunn commented Apr 18, 2023

Hi,

the Tauri doc states:

if the command returns an error, the promise will reject, otherwise, it resolves:

Which means the promise https://github.com/ChangeCaps/tauri-invoke/blob/main/src/lib.rs#L65 will resolve to an u32 for the command

async fn example_invoke_result() -> Result<u32, ()>,

Therefore the macro generated function should be:

async fn example_invoke_result() -> InvokeResult<u32>,
@ChangeCaps
Copy link
Owner

I'm not quite sure I understand the problem.

Do you think you could maybe open a pull request that fixes it?

@shimunn
Copy link
Author

shimunn commented Apr 19, 2023

Do you think you could maybe open a pull request that fixes it?

I would have opened one but unfortunately I'm not familiar enough with macros to do so.

Let me try to paraphrase the problem differently.

So tauri converts Result<T, E> into JsPromise<T> which then fails to deserialize back into Result since the enum is erased.

To fix that problem, the macro should convert the function signature Result<T, E> to just T. But threat functions which do not return an result as it does atm.

The following code would need to compile

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn simple() {
        async fn async_scope() {
            invoke!(async fn example_invoke(foo: f32, bar: bool) -> String);
            let _: InvokeResult<String> = example_invoke(1.0, false).await;
        }
    }

    #[test]
    fn result() {
        async fn async_scope() {
            invoke!(async fn example_invoke(foo: f32, bar: bool) -> Result<u32, String>);
            let _: InvokeResult<u32> = example_invoke(1.0, true).await;
        }
    }
}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants