Skip to main content

soma_tauri_shell/
command.rs

1//! Generic result/error helpers for Tauri command handlers.
2//!
3//! Tauri commands surface errors to the frontend as plain `String`s. Every
4//! handler in a `src-tauri` package ends up writing the same
5//! `.map_err(|err| err.to_string())` boilerplate; this module centralizes it.
6
7/// The result shape every `#[tauri::command]` handler in a Soma-derived
8/// desktop app should return: `Ok(T)` on success, or a display-formatted
9/// error string the frontend can show directly.
10pub type CommandResult<T> = Result<T, String>;
11
12/// Extension trait converting any `Result<T, E: Display>` into a
13/// [`CommandResult<T>`] without a manual `.map_err(|err| err.to_string())`
14/// at every call site.
15pub trait TauriResultExt<T> {
16    fn command_result(self) -> CommandResult<T>;
17}
18
19impl<T, E: std::fmt::Display> TauriResultExt<T> for Result<T, E> {
20    fn command_result(self) -> CommandResult<T> {
21        self.map_err(|err| err.to_string())
22    }
23}
24
25#[cfg(test)]
26#[path = "command_tests.rs"]
27mod tests;