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
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");
}
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();
}
