notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

commit 049bb3e8bbe5666bc0159d9bc017016149233949
parent d22dd9ed31567a10b44b72cb2755099f57f5e148
Author: kernelkind <kernelkind@gmail.com>
Date:   Thu, 17 Jul 2025 19:01:51 -0400

use `NwcError` instead of nwc::Error

need to clone

Signed-off-by: kernelkind <kernelkind@gmail.com>

Diffstat:
Mcrates/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 @@ -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<RwLock<NWC>>, - balance: Option<Promise<Result<u64, nwc::Error>>>, + balance: Option<Promise<Result<u64, NwcError>>>, } #[derive(Clone)] @@ -95,7 +95,7 @@ impl Wallet { }) } - pub fn get_balance(&mut self) -> Option<&Result<u64, nwc::Error>> { + pub fn get_balance(&mut self) -> Option<&Result<u64, NwcError>> { 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<RwLock<NWC>>) -> Promise<Result<u64, nwc::Error>> { +#[derive(Clone)] +pub enum NwcError { + /// NIP47 error + NIP47(String), + /// Relay + Relay(String), + /// Premature exit + PrematureExit, + /// Request timeout + Timeout, +} + +impl From<nwc::Error> 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<RwLock<NWC>>) -> Promise<Result<u64, NwcError>> { 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