#![warn(missing_docs)]
#![warn(rustdoc::bare_urls)]
#![allow(unknown_lints)]
use core::fmt;
use std::sync::Arc;
pub extern crate nostr;
pub use async_trait::async_trait;
use nostr::prelude::*;
pub mod error;
pub mod prelude;
pub use self::error::ZapperError;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ZapperBackend {
WebLN,
NWC,
Custom(String),
}
pub type DynNostrZapper = dyn NostrZapper<Err = ZapperError>;
pub trait IntoNostrZapper {
#[doc(hidden)]
fn into_nostr_zapper(self) -> Arc<DynNostrZapper>;
}
impl IntoNostrZapper for Arc<DynNostrZapper> {
fn into_nostr_zapper(self) -> Arc<DynNostrZapper> {
self
}
}
impl<T> IntoNostrZapper for T
where
T: NostrZapper + Sized + 'static,
{
fn into_nostr_zapper(self) -> Arc<DynNostrZapper> {
Arc::new(EraseNostrZapperError(self))
}
}
impl<T> IntoNostrZapper for Arc<T>
where
T: NostrZapper + 'static,
{
fn into_nostr_zapper(self) -> Arc<DynNostrZapper> {
let ptr: *const T = Arc::into_raw(self);
let ptr_erased = ptr as *const EraseNostrZapperError<T>;
unsafe { Arc::from_raw(ptr_erased) }
}
}
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait NostrZapper: AsyncTraitDeps {
type Err: From<ZapperError> + Into<ZapperError>;
fn backend(&self) -> ZapperBackend;
async fn pay(&self, invoice: String) -> Result<(), Self::Err>;
}
#[repr(transparent)]
struct EraseNostrZapperError<T>(T);
impl<T: fmt::Debug> fmt::Debug for EraseNostrZapperError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl<T: NostrZapper> NostrZapper for EraseNostrZapperError<T> {
type Err = ZapperError;
fn backend(&self) -> ZapperBackend {
self.0.backend()
}
async fn pay(&self, invoice: String) -> Result<(), Self::Err> {
self.0.pay(invoice).await.map_err(Into::into)
}
}
#[cfg(not(target_arch = "wasm32"))]
pub trait SendOutsideWasm: Send {}
#[cfg(not(target_arch = "wasm32"))]
impl<T: Send> SendOutsideWasm for T {}
#[cfg(target_arch = "wasm32")]
pub trait SendOutsideWasm {}
#[cfg(target_arch = "wasm32")]
impl<T> SendOutsideWasm for T {}
#[cfg(not(target_arch = "wasm32"))]
pub trait SyncOutsideWasm: Sync {}
#[cfg(not(target_arch = "wasm32"))]
impl<T: Sync> SyncOutsideWasm for T {}
#[cfg(target_arch = "wasm32")]
pub trait SyncOutsideWasm {}
#[cfg(target_arch = "wasm32")]
impl<T> SyncOutsideWasm for T {}
pub trait AsyncTraitDeps: std::fmt::Debug + SendOutsideWasm + SyncOutsideWasm {}
impl<T: std::fmt::Debug + SendOutsideWasm + SyncOutsideWasm> AsyncTraitDeps for T {}