nostr_sdk/client/
builder.rs1use std::sync::Arc;
8
9use nostr::signer::{IntoNostrSigner, NostrSigner};
10use nostr_database::memory::MemoryDatabase;
11use nostr_database::{IntoNostrDatabase, NostrDatabase};
12use nostr_gossip::{IntoNostrGossip, NostrGossip};
13use nostr_relay_pool::monitor::Monitor;
14use nostr_relay_pool::policy::AdmitPolicy;
15use nostr_relay_pool::transport::websocket::{
16 DefaultWebsocketTransport, IntoWebSocketTransport, WebSocketTransport,
17};
18
19use crate::client::options::ClientOptions;
20use crate::client::Client;
21
22#[derive(Debug, Clone)]
24pub struct ClientBuilder {
25 pub signer: Option<Arc<dyn NostrSigner>>,
27 pub websocket_transport: Arc<dyn WebSocketTransport>,
29 pub admit_policy: Option<Arc<dyn AdmitPolicy>>,
31 pub database: Arc<dyn NostrDatabase>,
33 pub gossip: Option<Arc<dyn NostrGossip>>,
35 pub monitor: Option<Monitor>,
37 pub opts: ClientOptions,
39}
40
41impl Default for ClientBuilder {
42 fn default() -> Self {
43 Self {
44 signer: None,
45 websocket_transport: Arc::new(DefaultWebsocketTransport),
46 admit_policy: None,
47 database: Arc::new(MemoryDatabase::default()),
48 gossip: None,
49 monitor: None,
50 opts: ClientOptions::default(),
51 }
52 }
53}
54
55impl ClientBuilder {
56 #[inline]
58 pub fn new() -> Self {
59 Self::default()
60 }
61
62 #[inline]
73 pub fn signer<T>(mut self, signer: T) -> Self
74 where
75 T: IntoNostrSigner,
76 {
77 self.signer = Some(signer.into_nostr_signer());
78 self
79 }
80
81 #[inline]
85 pub fn websocket_transport<T>(mut self, transport: T) -> Self
86 where
87 T: IntoWebSocketTransport,
88 {
89 self.websocket_transport = transport.into_transport();
90 self
91 }
92
93 #[inline]
95 pub fn admit_policy<T>(mut self, policy: T) -> Self
96 where
97 T: AdmitPolicy + 'static,
98 {
99 self.admit_policy = Some(Arc::new(policy));
100 self
101 }
102
103 #[inline]
105 pub fn database<D>(mut self, database: D) -> Self
106 where
107 D: IntoNostrDatabase,
108 {
109 self.database = database.into_nostr_database();
110 self
111 }
112
113 #[inline]
115 pub fn gossip<T>(mut self, gossip: T) -> Self
116 where
117 T: IntoNostrGossip,
118 {
119 self.gossip = Some(gossip.into_nostr_gossip());
120 self
121 }
122
123 #[inline]
125 pub fn monitor(mut self, monitor: Monitor) -> Self {
126 self.monitor = Some(monitor);
127 self
128 }
129
130 #[inline]
132 pub fn opts(mut self, opts: ClientOptions) -> Self {
133 self.opts = opts;
134 self
135 }
136
137 #[inline]
139 pub fn build(self) -> Client {
140 Client::from_builder(self)
141 }
142}