#Vector to store commands with specta macros

13 messages · Page 1 of 1 (latest)

willow stump
#

Trying to make my life easier, the idea is just to store everything in a constant vector that will not be changed after compile the code.
I'm still a beginner in rust, I genuinely don't know what I could achieve something like spreading like in tyescript since collect_types and generate_handler take both vectors of fn if I'm not mistaken

main.rs

fn main() {
  #[cfg(debug_assertions)]
  tauri_specta::ts::export(specta::collect_types![COMMANDS], "../src/tools/commands.ts").unwrap();

  tauri::Builder::default()
  .setup(|app| {
    let identifier_path = data_dir().unwrap().display().to_string() + &app.config().tauri.bundle.identifier;

    let data_dir = match app_data_dir(&app.config()) {
      None => std::fs::create_dir(identifier_path),
      Some(dir) => {
        let data_path = dir.join(DATA_FILE);

        if !data_path.exists() {
          let file = std::fs::File::create(data_path).unwrap();
          let mut writer = std::io::BufWriter::new(file);
          serde_json::to_writer(&mut writer, &Vec::<()>::new()).unwrap();

          Ok(())
        } else { Ok(()) }
      }
    };

    Ok(())
  })
    .invoke_handler(tauri::generate_handler![COMMANDS])
    .run(tauri::generate_context!())
    .expect("Error while running tauri application");
}

commands.rs

use crate::utils::{get_todos_file, ITodo};

pub const COMMANDS = vec![add_todo, clear_completed_todos, remove_todo, fetch_todos];

#[tauri::command]
#[specta::specta]
fn add_todo (todo: ITodo) -> Result<(), ()> {
  let file = get_todos_file();

  serde_json::to_writer(&file, &todo).unwrap();
  Ok(())
}

#[tauri::command]
#[specta::specta]
fn clear_completed_todos () { todo!() }

#[tauri::command]
#[specta::specta]
fn remove_todo (todo: ITodo) { todo!() }

#[tauri::command]
#[specta::specta]
fn fetch_todos () -> Vec<ITodo> {
  let reader = std::io::BufReader::new(get_todos_file());
  return serde_json::from_reader(reader).unwrap();
}
#

Errors

mismatched types
expected fn item `fn(ITodo) -> Result<(), ()> {add_todo}`
   found fn item `fn() {clear_completed_todos}`rustc
todoux::commands
fn clear_completed_todos()
cannot find macro `__specta__fn__COMMANDS` in this scoperustcC
todoux::commands
pub const COMMANDS: {error} = vec![add_todo, clear_completed_todos, remove_todo, fetch_todos]
jovial imp
#

And then we just pass it into invoke_handler

#

It works bc tauri_handlers expands to a block of code that first runs export, and then returns generate_handler

willow stump
#

Oh ok, now that makes sense

#

Tsym sparklecolors

balmy vine
#

Hi @jovial imp, I have a handlers.rs file, that looks like this

#
use crate::atoms;
use tauri::Wry;

#[inline]
fn invoke_tauri_handlers<F, R: tauri::Runtime>(handler: F, invoke: tauri::Invoke<R>)
where
    F: Fn(tauri::Invoke<R>) + Send + Sync + 'static,
{
    handler(invoke);
}

pub fn invoke_handler(invoke: tauri::Invoke<Wry>) {
    invoke_tauri_handlers(
        tauri::generate_handler![...],
        invoke,
    )
}
#

I have lots of commands

#

Can you help me, how can I generate the commands using specta without the need to rewrite all commands names

jovial imp
balmy vine
#

Thanks