From f4ada78929acff09b884a6a437ac398ba2f0030f Mon Sep 17 00:00:00 2001 From: Einliterflasche Date: Wed, 28 Aug 2024 12:25:30 +0200 Subject: [PATCH] refactor(cli): introduce tauri feature flag to allow the project to be compiled without tauri --- src-tauri/Cargo.toml | 2 +- src-tauri/src/lib.rs | 5 +++-- swap/Cargo.toml | 5 ++++- swap/src/cli/api.rs | 12 ++++++------ swap/src/cli/api/tauri_bindings.rs | 22 ++++++++++++++++------ 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 7fa632b3..73e9bb9d 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -19,5 +19,5 @@ anyhow = "1" once_cell = "1" serde = { version = "1", features = [ "derive" ] } serde_json = "1" -swap = { path = "../swap" } +swap = { path = "../swap", features = [ "tauri" ] } tauri = { version = "2.0.0-rc.1", features = [ "config-json5" ] } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index b58e9030..2f255236 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -6,6 +6,7 @@ use swap::cli::{ BalanceArgs, BuyXmrArgs, GetHistoryArgs, GetSwapInfosAllArgs, ResumeSwapArgs, SuspendCurrentSwapArgs, WithdrawBtcArgs, }, + tauri_bindings::TauriHandle, Context, ContextBuilder, }, command::{Bitcoin, Monero}, @@ -86,7 +87,7 @@ tauri_command!(suspend_current_swap, SuspendCurrentSwapArgs, no_args); tauri_command!(get_swap_infos_all, GetSwapInfosAllArgs, no_args); tauri_command!(get_history, GetHistoryArgs, no_args); -fn setup<'a>(app: &'a mut tauri::App) -> Result<(), Box> { +fn setup(app: &mut tauri::App) -> Result<(), Box> { tauri::async_runtime::block_on(async { let context = ContextBuilder::new(true) .with_bitcoin(Bitcoin { @@ -98,7 +99,7 @@ fn setup<'a>(app: &'a mut tauri::App) -> Result<(), Box> }) .with_json(true) .with_debug(true) - .with_tauri(app.app_handle().to_owned()) + .with_tauri(TauriHandle::new(app.app_handle().to_owned())) .build() .await .expect("failed to create context"); diff --git a/swap/Cargo.toml b/swap/Cargo.toml index df47942b..2d90465f 100644 --- a/swap/Cargo.toml +++ b/swap/Cargo.toml @@ -8,6 +8,9 @@ description = "XMR/BTC trustless atomic swaps." [lib] name = "swap" +[features] +tauri = [ "dep:tauri" ] + [dependencies] anyhow = "1" async-compression = { version = "0.3", features = [ "bzip2", "tokio" ] } @@ -86,7 +89,7 @@ sqlx = { version = "0.6.3", features = [ ] } structopt = "0.3" strum = { version = "0.26", features = [ "derive" ] } -tauri = { version = "2.0.0-rc.1", features = [ "config-json5" ] } +tauri = { version = "2.0.0-rc.1", features = [ "config-json5" ], optional = true } thiserror = "1" time = "0.3" tokio = { version = "1", features = [ diff --git a/swap/src/cli/api.rs b/swap/src/cli/api.rs index 8386b43e..b9e5e9e6 100644 --- a/swap/src/cli/api.rs +++ b/swap/src/cli/api.rs @@ -1,5 +1,6 @@ pub mod request; pub mod tauri_bindings; + use crate::cli::command::{Bitcoin, Monero, Tor}; use crate::database::open_db; use crate::env::{Config as EnvConfig, GetConfig, Mainnet, Testnet}; @@ -14,7 +15,6 @@ use std::fmt; use std::future::Future; use std::path::PathBuf; use std::sync::{Arc, Mutex as SyncMutex, Once}; -use tauri::AppHandle; use tauri_bindings::TauriHandle; use tokio::sync::{broadcast, broadcast::Sender, Mutex as TokioMutex, RwLock}; use tokio::task::JoinHandle; @@ -193,7 +193,7 @@ pub struct ContextBuilder { is_testnet: bool, debug: bool, json: bool, - tauri_handle: Option, + tauri_handle: Option, } impl ContextBuilder { @@ -246,7 +246,7 @@ impl ContextBuilder { } /// Attach a handle to Tauri to the Context for emitting events etc. - pub fn with_tauri(mut self, tauri: impl Into>) -> Self { + pub fn with_tauri(mut self, tauri: impl Into>) -> Self { self.tauri_handle = tauri.into(); self } @@ -330,7 +330,7 @@ impl ContextBuilder { }, swap_lock: Arc::new(SwapLock::new()), tasks: Arc::new(PendingTaskList::default()), - tauri_handle: self.tauri_handle.map(TauriHandle::new), + tauri_handle: self.tauri_handle, }; Ok(context) @@ -338,8 +338,8 @@ impl ContextBuilder { } impl Context { - pub fn with_tauri_handle(mut self, tauri_handle: AppHandle) -> Self { - self.tauri_handle = Some(TauriHandle::new(tauri_handle)); + pub fn with_tauri_handle(mut self, tauri_handle: impl Into>) -> Self { + self.tauri_handle = tauri_handle.into(); self } diff --git a/swap/src/cli/api/tauri_bindings.rs b/swap/src/cli/api/tauri_bindings.rs index f0788fa4..5a3585ad 100644 --- a/swap/src/cli/api/tauri_bindings.rs +++ b/swap/src/cli/api/tauri_bindings.rs @@ -4,8 +4,6 @@ use anyhow::Result; use bitcoin::Txid; use serde::Serialize; -use std::sync::Arc; -use tauri::{AppHandle, Emitter}; use typeshare::typeshare; use uuid::Uuid; @@ -14,15 +12,27 @@ use crate::{monero, network::quote::BidQuote}; static SWAP_PROGRESS_EVENT_NAME: &str = "swap-progress-update"; #[derive(Debug, Clone)] -pub struct TauriHandle(Arc); +pub struct TauriHandle( + #[cfg(feature = "tauri")] + #[cfg_attr(feature = "tauri", allow(unused))] + std::sync::Arc, +); impl TauriHandle { - pub fn new(tauri_handle: AppHandle) -> Self { - Self(Arc::new(tauri_handle)) + #[cfg(feature = "tauri")] + pub fn new(tauri_handle: tauri::AppHandle) -> Self { + Self( + #[cfg(feature = "tauri")] + std::sync::Arc::new(tauri_handle), + ) } + #[allow(unused_variables)] pub fn emit_tauri_event(&self, event: &str, payload: S) -> Result<()> { - self.0.emit(event, payload).map_err(|e| e.into()) + #[cfg(tauri)] + self.0.emit(event, payload).map_err(|e| e.into())?; + + Ok(()) } }