I have a basic Tauri GUI-application. Initially I had all the Tauri-commands - the hooks which can be called from the front-end - in my main.rs like this:
//...
fn new_project_if_none() -> std::io::Result<()> {
//...
}
fn counter(tx: mpsc::Sender<u64>, is_playing: Arc<(Mutex<bool>, Condvar)>, reset: Arc<Mutex<bool>>) {
// ...
}
#[tauri::command]
fn play(app_handle: tauri::AppHandle, is_playing: State<IsPlayingState>, first_click: State<FirstClickState>, seconds_state: State<SecondsState>, reset_state: State<ResetState>) {
// ...
}
#[tauri::command]
fn save(task_description: &str, seconds_state: State<SecondsState>, reset_state: State<ResetState>, app_handle: tauri::AppHandle) -> Result<(), String> {
//...
}
#[tauri::command]
fn update_finished_tasks(app_handle: tauri::AppHandle) -> Result<(), String> {
//...
}
// I have more Tauri-commands but you get the idea.
fn main() {
// ...
tauri::Builder::default()
.manage(tauri_commands::IsPlayingState(is_playing))
.manage(FirstClickState(first_click))
.manage(SecondsState(seconds))
.manage(ResetState(reset))
.invoke_handler(tauri::generate_handler![play, save, update_finished_tasks, delete_task, create_new_project, load_projects, delete_project, select_project, exit])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Now I have made a new file called tauri_commands and placed my Tauri-commands there. Then I did mod tauri_commands
in my main.rs and call the functions like tauri_commands::play(...)
.
Is this considered a good practice in Rust to structure my project more and to not bloat the main.rs-file?
I have the idea that, unlike other languages like e.g Java where every class need it's own separate file, Rust gives a lot of freedom in how you structure your project and there aren't clear instructions. I like this freedom but it sometimes makes it hard to know how to structure your project. I have also read that in Rust it isn't always adviced to make a lot of separate modules unless really necessary.
Edit: I also moved the functions counter
and new_project_if_none
in a module called utils.rs