From d54f5c6c77556639d79acde4c214b8a4bd9eba98 Mon Sep 17 00:00:00 2001 From: binarybaron Date: Mon, 26 Aug 2024 15:26:21 +0200 Subject: [PATCH] refactor(tauri): Use macro and new Request trait for command generation --- src-tauri/capabilities/default.json | 10 ++-- src-tauri/src/lib.rs | 87 +++++++++++++---------------- src-tauri/tauri.conf.json | 58 +++++++++---------- 3 files changed, 73 insertions(+), 82 deletions(-) diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.json index d6686571..034e1738 100644 --- a/src-tauri/capabilities/default.json +++ b/src-tauri/capabilities/default.json @@ -1,7 +1,7 @@ { - "$schema": "../gen/schemas/desktop-schema.json", - "identifier": "default", - "description": "Capability for the main window", - "windows": ["main"], - "permissions": [] + "$schema": "../gen/schemas/desktop-schema.json", + "identifier": "default", + "description": "Capability for the main window", + "windows": ["main"], + "permissions": ["core:event:allow-emit", "core:event:default"] } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 2655e3eb..98462d57 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -3,15 +3,14 @@ use std::sync::Arc; use swap::{ api::{ request::{ - get_balance as get_balance_impl, get_swap_infos_all as get_swap_infos_all_impl, - withdraw_btc as withdraw_btc_impl, BalanceArgs, BalanceResponse, GetSwapInfoResponse, - WithdrawBtcArgs, WithdrawBtcResponse, + BalanceArgs, BuyXmrArgs, GetHistoryArgs, ResumeSwapArgs, SuspendCurrentSwapArgs, + WithdrawBtcArgs, }, Context, }, cli::command::{Bitcoin, Monero}, }; -use tauri::{Manager, RunEvent, State}; +use tauri::{Manager, RunEvent}; trait ToStringResult { fn to_string_result(self) -> Result; @@ -20,57 +19,44 @@ trait ToStringResult { // 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()), - } + self.map_err(|e| e.to_string()) } } -#[tauri::command] -async fn get_balance(context: State<'_, Arc>) -> Result { - get_balance_impl( - BalanceArgs { - force_refresh: true, - }, - context.inner().clone(), - ) - .await - .to_string_result() -} - -#[tauri::command] -async fn get_swap_infos_all( - context: State<'_, Arc>, -) -> Result, String> { - get_swap_infos_all_impl(context.inner().clone()) - .await - .to_string_result() -} - -/*macro_rules! tauri_command { - ($command_name:ident, $command_args:ident, $command_response:ident) => { +/// This macro is used to create boilerplate functions as tauri commands +/// that simply delegate handling to the respective request type. +/// +/// # Example +/// ```ignored +/// tauri_command!(get_balance, BalanceArgs); +/// ``` +/// will resolve to +/// ```ignored +/// #[tauri::command] +/// async fn get_balance(context: tauri::State<'...>, args: BalanceArgs) -> Result { +/// args.handle(context.inner().clone()).await.to_string_result() +/// } +/// ``` +macro_rules! tauri_command { + ($fn_name:ident, $request_name:ident) => { #[tauri::command] - async fn $command_name( - context: State<'_, Context>, - args: $command_args, - ) -> Result<$command_response, String> { - swap::api::request::$command_name(args, context) + async fn $fn_name( + context: tauri::State<'_, Arc>, + args: $request_name, + ) -> Result<<$request_name as swap::api::request::Request>::Response, String> { + <$request_name as swap::api::request::Request>::request(args, context.inner().clone()) .await .to_string_result() } }; -}*/ - -#[tauri::command] -async fn withdraw_btc( - context: State<'_, Arc>, - args: WithdrawBtcArgs, -) -> Result { - withdraw_btc_impl(args, context.inner().clone()) - .await - .to_string_result() } +tauri_command!(get_balance, BalanceArgs); +tauri_command!(get_swap_infos_all, BalanceArgs); +tauri_command!(buy_xmr, BuyXmrArgs); +tauri_command!(get_history, GetHistoryArgs); +tauri_command!(resume_swap, ResumeSwapArgs); +tauri_command!(withdraw_btc, WithdrawBtcArgs); +tauri_command!(suspend_current_swap, SuspendCurrentSwapArgs); fn setup<'a>(app: &'a mut tauri::App) -> Result<(), Box> { tauri::async_runtime::block_on(async { @@ -90,7 +76,8 @@ fn setup<'a>(app: &'a mut tauri::App) -> Result<(), Box> None, ) .await - .unwrap(); + .unwrap() + .with_tauri_handle(app.app_handle().to_owned()); app.manage(Arc::new(context)); }); @@ -104,7 +91,11 @@ pub fn run() { .invoke_handler(tauri::generate_handler![ get_balance, get_swap_infos_all, - withdraw_btc + withdraw_btc, + buy_xmr, + resume_swap, + get_history, + suspend_current_swap ]) .setup(setup) .build(tauri::generate_context!()) diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index ffe6d3ee..a1f1b566 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,32 +1,32 @@ { - "productName": "unstoppableswap-gui-rs", - "version": "0.1.0", - "identifier": "net.unstoppableswap.gui", - "build": { - "devUrl": "http://localhost:1420", - "frontendDist": "../src-gui/dist" - }, - "app": { - "windows": [ - { - "title": "unstoppableswap-gui-rs", - "width": 800, - "height": 600 - } - ], - "security": { - "csp": "default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; script-src 'self'; connect-src 'self' http://localhost:1234" - } - }, - "bundle": { - "active": true, - "targets": "all", - "icon": [ - "icons/32x32.png", - "icons/128x128.png", - "icons/128x128@2x.png", - "icons/icon.icns", - "icons/icon.ico" - ] + "productName": "unstoppableswap-gui-rs", + "version": "0.1.0", + "identifier": "net.unstoppableswap.gui", + "build": { + "devUrl": "http://localhost:1420", + "frontendDist": "../src-gui/dist" + }, + "app": { + "windows": [ + { + "title": "unstoppableswap-gui-rs", + "width": 800, + "height": 600 + } + ], + "security": { + "csp": "default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; script-src 'self'; connect-src 'self' http://localhost:1234 https://api.unstoppableswap.net" } + }, + "bundle": { + "active": true, + "targets": "all", + "icon": [ + "icons/32x32.png", + "icons/128x128.png", + "icons/128x128@2x.png", + "icons/icon.icns", + "icons/icon.ico" + ] + } }