wip: refactor request.rs to allow type safety

This commit is contained in:
binarybaron 2024-08-07 22:51:18 +02:00
parent 757183e857
commit 472e3a57b3
No known key found for this signature in database
GPG key ID: 99B75D3E1476A26E
16 changed files with 955 additions and 743 deletions

View file

@ -1,24 +1,24 @@
[package]
name = "unstoppableswap-gui-rs"
version = "0.0.0"
description = "A Tauri App"
authors = ["you"]
authors = [ "you" ]
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
description = "A Tauri App"
[lib]
name = "unstoppableswap_gui_rs_lib"
crate-type = ["lib", "cdylib", "staticlib"]
crate-type = [ "lib", "cdylib", "staticlib" ]
[build-dependencies]
tauri-build = { version = "2.0.0-beta", features = [] }
tauri-build = { version = "2.0.0-beta", features = [ ] }
[dependencies]
tauri = { version = "2.0.0-beta", features = [] }
tauri-plugin-shell = "2.0.0-beta"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
anyhow = "1"
once_cell = "1"
swap = { path= "../swap" }
serde = { version = "1", features = [ "derive" ] }
serde_json = "1"
swap = { path = "../swap" }
tauri = { version = "2.0.0-beta", features = [ ] }
tauri-plugin-shell = "2.0.0-beta"

View file

@ -1,17 +1,29 @@
use std::sync::Arc;
use once_cell::sync::OnceCell;
use swap::{api::{request::{Method, Request}, Context}, cli::command::{Bitcoin, Monero}};
use swap::{
api::{
request::{get_balance, BalanceArgs, BalanceResponse},
Context,
},
cli::command::{Bitcoin, Monero},
};
// Lazy load the Context
static CONTEXT: OnceCell<Arc<Context>> = OnceCell::new();
#[tauri::command]
async fn balance() -> String {
async fn balance() -> Result<BalanceResponse, String> {
let context = CONTEXT.get().unwrap();
let request = Request::new(Method::Balance { force_refresh: true });
let response = request.call(context.clone()).await.unwrap();
response.to_string()
get_balance(
BalanceArgs {
force_refresh: true,
},
context.clone(),
)
.await
.map_err(|e| e.to_string())
}
fn setup<'a>(app: &'a mut tauri::App) -> Result<(), Box<dyn std::error::Error>> {
@ -34,7 +46,9 @@ fn setup<'a>(app: &'a mut tauri::App) -> Result<(), Box<dyn std::error::Error>>
.await
.unwrap();
CONTEXT.set(Arc::new(context)).expect("Failed to initialize cli context");
CONTEXT
.set(Arc::new(context))
.expect("Failed to initialize cli context");
});
Ok(())
}