From 8cb1e8aff09e4559adfb910dbb894a61beb171b6 Mon Sep 17 00:00:00 2001 From: binarybaron Date: Fri, 9 Aug 2024 14:41:26 +0200 Subject: [PATCH] wip: use trait to convert Result into Result for Tauri command response --- src-tauri/src/lib.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 1f78c45f..3732725a 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,6 +1,7 @@ use std::sync::Arc; use once_cell::sync::OnceCell; +use std::result::Result; use swap::{ api::{ request::{ @@ -15,6 +16,20 @@ use swap::{ // Lazy load the Context static CONTEXT: OnceCell> = OnceCell::new(); +trait ToStringResult { + fn to_string_result(self) -> Result; +} + +// Implement the trait for Result +impl ToStringResult for Result { + fn to_string_result(self) -> Result { + match self { + Ok(value) => Ok(value), + Err(err) => Err(err.to_string()), + } + } +} + #[tauri::command] async fn get_balance() -> Result { let context = CONTEXT.get().unwrap(); @@ -26,7 +41,7 @@ async fn get_balance() -> Result { context.clone(), ) .await - .map_err(|e| e.to_string()) + .to_string_result() } #[tauri::command] @@ -35,7 +50,7 @@ async fn get_swap_infos_all() -> Result, String> { get_swap_infos_all_impl(context.clone()) .await - .map_err(|e| e.to_string()) + .to_string_result() } fn setup<'a>(app: &'a mut tauri::App) -> Result<(), Box> {