From 049bb3e8bbe5666bc0159d9bc017016149233949 Mon Sep 17 00:00:00 2001 From: kernelkind Date: Thu, 17 Jul 2025 19:01:51 -0400 Subject: [PATCH] use `NwcError` instead of nwc::Error need to clone Signed-off-by: kernelkind --- crates/notedeck/src/wallet.rs | 50 +++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/crates/notedeck/src/wallet.rs b/crates/notedeck/src/wallet.rs index ff372894..4fae0627 100644 --- a/crates/notedeck/src/wallet.rs +++ b/crates/notedeck/src/wallet.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::{fmt::Display, sync::Arc}; use nwc::{ nostr::nips::nip47::{NostrWalletConnectURI, PayInvoiceRequest, PayInvoiceResponse}, @@ -57,7 +57,7 @@ pub enum WalletError { pub struct Wallet { pub uri: String, wallet: Arc>, - balance: Option>>, + balance: Option>>, } #[derive(Clone)] @@ -95,7 +95,7 @@ impl Wallet { }) } - pub fn get_balance(&mut self) -> Option<&Result> { + pub fn get_balance(&mut self) -> Option<&Result> { if self.balance.is_none() { self.balance = Some(get_balance(self.wallet.clone())); return None; @@ -117,11 +117,51 @@ impl Wallet { } } -fn get_balance(nwc: Arc>) -> Promise> { +#[derive(Clone)] +pub enum NwcError { + /// NIP47 error + NIP47(String), + /// Relay + Relay(String), + /// Premature exit + PrematureExit, + /// Request timeout + Timeout, +} + +impl From for NwcError { + fn from(value: nwc::Error) -> Self { + match value { + nwc::error::Error::NIP47(error) => NwcError::NIP47(error.to_string()), + nwc::error::Error::Relay(error) => NwcError::Relay(error.to_string()), + nwc::error::Error::PrematureExit => NwcError::PrematureExit, + nwc::error::Error::Timeout => NwcError::Timeout, + } + } +} + +impl Display for NwcError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + NwcError::NIP47(err) => write!(f, "NIP47 error: {}", err), + NwcError::Relay(err) => write!(f, "Relay error: {}", err), + NwcError::PrematureExit => write!(f, "Premature exit"), + NwcError::Timeout => write!(f, "Request timed out"), + } + } +} + +fn get_balance(nwc: Arc>) -> Promise> { let (sender, promise) = Promise::new(); tokio::spawn(async move { - sender.send(nwc.read().await.get_balance().await); + sender.send( + nwc.read() + .await + .get_balance() + .await + .map_err(nwc::Error::into), + ); }); promise