#Rust question.

12 messages · Page 1 of 1 (latest)

lament pilot
#

Why does generating the handler like this not work?

    let _handler = tauri::generate_handler![];
    let app = tauri::Builder::default()
        .plugin(tauri_plugin_window_state::Builder::default().build())
        .invoke_handler(_handler)
        .build(tauri::generate_context!())
        .expect("error while running tauri application");
dense sandal
#

The technical reason is that parts of what it requires is not public (accessible outside the crate). The reason for this is in the docs: https://docs.rs/tauri/latest/tauri/macro.generate_handler.html#stability.

The output of this macro is managed internally by Tauri, and should not be accessed directly on normal applications. It may have breaking changes in the future.
There's a lot going on behind the scenes with that macro. Hiding it away like that means that apps shouldn't break with every minor change to commands.

lament pilot
#

I see

dense sandal
#

There's probably a better way to achieve what you're trying to do by referencing command handles.

lament pilot
#

I was looking for a decent way

#

to get rid of the handlers being referenced in the main file.

#

I ended up doing something like this

#

commands.rs:

use sha2::{Digest, Sha256};
use tauri::{Builder, Wry};

// Exports a function for the tauri app instance to use and register all commands defined as frontend IPC command handlers.
pub fn register_commands(builder: Builder<Wry>) -> Builder<Wry> {
    builder.invoke_handler(tauri::generate_handler![hash256sum, called_from_js])
}

// Some example commands
#[tauri::command]
fn called_from_js() -> String {
    "Hi from Tauri".to_owned()
}

#[tauri::command]
fn hash256sum(hash_input: String) -> String {
    let mut hasher = Sha256::new();
    hasher.update(hash_input.as_bytes());
    let result = hasher.finalize();
    format!("{:X}", result)
}
#

main.rs

#![cfg_attr(
    all(not(debug_assertions), target_os = "windows"),
    windows_subsystem = "windows"
)]

use commands::register_commands;
use prelude::*;

use tauri::RunEvent;
mod commands;
mod error;
mod prelude;

fn main() {
    let app =
        tauri::Builder::default().plugin(tauri_plugin_window_state::Builder::default().build());

    let app = register_commands(app)
        .build(tauri::generate_context!())
        .expect("error while running tauri application");

    app.run(|_, e| match e {
        RunEvent::Ready => {
            println!("Window is ready");
        }
        _ => {}
    })
}
#

Is that a decent way to do it?

#

or would this be an anti-pattern

dense sandal
#

It's fine to do it that way and I don't see a reason why it would be an anti-pattern.