nostr_zapper/
lib.rs

1// Copyright (c) 2022-2023 Yuki Kishimoto
2// Copyright (c) 2023-2024 Rust Nostr Developers
3// Distributed under the MIT software license
4
5//! Nostr Zapper
6
7#![forbid(unsafe_code)]
8#![warn(missing_docs)]
9#![warn(rustdoc::bare_urls)]
10#![allow(unknown_lints)]
11#![allow(clippy::arc_with_non_send_sync)]
12
13use std::fmt;
14use std::sync::Arc;
15
16pub extern crate nostr;
17
18use async_trait::async_trait;
19use nostr::prelude::*;
20
21pub mod error;
22pub mod prelude;
23#[cfg(feature = "webln")]
24mod webln;
25
26pub use self::error::ZapperError;
27#[cfg(feature = "webln")]
28pub use self::webln::WebLNZapper;
29
30/// Backend
31#[derive(Debug, Clone, PartialEq, Eq)]
32pub enum ZapperBackend {
33    /// WebLN
34    WebLN,
35    /// Nostr Wallet Connect
36    NWC,
37    /// Custom
38    Custom(String),
39}
40
41/// A type-erased [`NostrZapper`].
42pub type DynNostrZapper = dyn NostrZapper;
43
44/// A type that can be type-erased into `Arc<dyn NostrZapper>`.
45pub trait IntoNostrZapper {
46    #[doc(hidden)]
47    fn into_nostr_zapper(self) -> Arc<DynNostrZapper>;
48}
49
50impl IntoNostrZapper for Arc<DynNostrZapper> {
51    fn into_nostr_zapper(self) -> Arc<DynNostrZapper> {
52        self
53    }
54}
55
56impl<T> IntoNostrZapper for T
57where
58    T: NostrZapper + Sized + 'static,
59{
60    fn into_nostr_zapper(self) -> Arc<DynNostrZapper> {
61        Arc::new(self)
62    }
63}
64
65impl<T> IntoNostrZapper for Arc<T>
66where
67    T: NostrZapper + 'static,
68{
69    fn into_nostr_zapper(self) -> Arc<DynNostrZapper> {
70        self
71    }
72}
73
74/// Nostr Database
75#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
76#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
77pub trait NostrZapper: fmt::Debug + Send + Sync {
78    /// Name of the backend zapper used (ex. WebLN, NWC, ...)
79    fn backend(&self) -> ZapperBackend;
80
81    /// Pay invoice
82    async fn pay(&self, invoice: String) -> Result<(), ZapperError>;
83}