From aff6fcee20bcca2204363bcbe926e3b2849484e7 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Tue, 25 Jun 2024 15:39:39 -0400 Subject: [PATCH] pool: merge all outputs in a single `Output` struct Signed-off-by: Yuki Kishimoto --- CHANGELOG.md | 2 +- bindings/nostr-sdk-ffi/src/pool/result.rs | 40 +++++++--- bindings/nostr-sdk-js/src/pool/result.rs | 40 +++++++--- crates/nostr-relay-pool/src/lib.rs | 2 +- crates/nostr-relay-pool/src/pool/internal.rs | 83 ++++++++++--------- crates/nostr-relay-pool/src/pool/mod.rs | 34 ++++---- crates/nostr-relay-pool/src/pool/result.rs | 62 ++++----------- crates/nostr-sdk/examples/client.rs | 4 +- crates/nostr-sdk/examples/negentropy.rs | 6 +- crates/nostr-sdk/examples/nostr-connect.rs | 6 +- crates/nostr-sdk/examples/subscriptions.rs | 2 +- crates/nostr-sdk/src/client/mod.rs | 84 ++++++++++---------- 12 files changed, 190 insertions(+), 175 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b15921e0e..cb91ae601 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,7 +48,7 @@ * nostr: add `EventBuilder::interest_set` ([Yuki Kishimoto]) * nostr: add `title`, `image` and `description` constructors to `Tag` ([Yuki Kishimoto]) * nostr: add `Timestamp::zero` and `Timestamp::is_zero` methods ([Yuki Kishimoto]) -* pool: add `Output` and `SendEventOutput` structs ([Yuki Kishimoto]) +* pool: add `Output` struct ([Yuki Kishimoto]) * pool: return `ReconciliationOutput` for all `reconcile*` methods ([Yuki Kishimoto]) * signer: add `NostrSigner::unwrap_gift_wrap` method ([Yuki Kishimoto]) * sdk: add `Client::unwrap_gift_wrap` method ([Yuki Kishimoto]) diff --git a/bindings/nostr-sdk-ffi/src/pool/result.rs b/bindings/nostr-sdk-ffi/src/pool/result.rs index af595073a..c03617298 100644 --- a/bindings/nostr-sdk-ffi/src/pool/result.rs +++ b/bindings/nostr-sdk-ffi/src/pool/result.rs @@ -6,7 +6,7 @@ use std::collections::HashMap; use std::sync::Arc; use nostr_ffi::EventId; -use nostr_sdk::pool; +use nostr_sdk::{pool, SubscriptionId}; use uniffi::Record; /// Output @@ -20,11 +20,11 @@ pub struct Output { pub failed: HashMap>, } -impl From for Output { - fn from(value: pool::Output) -> Self { +impl From> for Output { + fn from(output: pool::Output<()>) -> Self { Self { - success: value.success.into_iter().map(|u| u.to_string()).collect(), - failed: value + success: output.success.into_iter().map(|u| u.to_string()).collect(), + failed: output .failed .into_iter() .map(|(u, e)| (u.to_string(), e)) @@ -42,11 +42,18 @@ pub struct SendEventOutput { pub output: Output, } -impl From for SendEventOutput { - fn from(value: pool::SendEventOutput) -> Self { +impl From> for SendEventOutput { + fn from(output: pool::Output) -> Self { Self { - id: Arc::new(value.id.into()), - output: value.output.into(), + id: Arc::new(output.val.into()), + output: Output { + success: output.success.into_iter().map(|u| u.to_string()).collect(), + failed: output + .failed + .into_iter() + .map(|(u, e)| (u.to_string(), e)) + .collect(), + }, } } } @@ -60,11 +67,18 @@ pub struct SubscribeOutput { pub output: Output, } -impl From for SubscribeOutput { - fn from(value: pool::SubscribeOutput) -> Self { +impl From> for SubscribeOutput { + fn from(output: pool::Output) -> Self { Self { - id: value.id.to_string(), - output: value.output.into(), + id: output.val.to_string(), + output: Output { + success: output.success.into_iter().map(|u| u.to_string()).collect(), + failed: output + .failed + .into_iter() + .map(|(u, e)| (u.to_string(), e)) + .collect(), + }, } } } diff --git a/bindings/nostr-sdk-js/src/pool/result.rs b/bindings/nostr-sdk-js/src/pool/result.rs index 0fad36da3..624e8cc22 100644 --- a/bindings/nostr-sdk-js/src/pool/result.rs +++ b/bindings/nostr-sdk-js/src/pool/result.rs @@ -29,8 +29,8 @@ pub struct JsOutput { pub failed: Vec, } -impl From for JsOutput { - fn from(value: Output) -> Self { +impl From> for JsOutput { + fn from(value: Output<()>) -> Self { Self { success: value.success.into_iter().map(|u| u.to_string()).collect(), failed: value @@ -55,11 +55,21 @@ pub struct JsSendEventOutput { pub output: JsOutput, } -impl From for JsSendEventOutput { - fn from(value: SendEventOutput) -> Self { +impl From> for JsSendEventOutput { + fn from(output: Output) -> Self { Self { - id: value.id.into(), - output: value.output.into(), + id: output.val.into(), + output: JsOutput { + success: output.success.into_iter().map(|u| u.to_string()).collect(), + failed: output + .failed + .into_iter() + .map(|(u, e)| JsFailedOutputItem { + url: u.to_string(), + error: e, + }) + .collect(), + }, } } } @@ -75,11 +85,21 @@ pub struct JsSubscribeOutput { pub output: JsOutput, } -impl From for JsSubscribeOutput { - fn from(value: SubscribeOutput) -> Self { +impl From> for JsSubscribeOutput { + fn from(output: Output) -> Self { Self { - id: value.id.to_string(), - output: value.output.into(), + id: output.val.to_string(), + output: JsOutput { + success: output.success.into_iter().map(|u| u.to_string()).collect(), + failed: output + .failed + .into_iter() + .map(|(u, e)| JsFailedOutputItem { + url: u.to_string(), + error: e, + }) + .collect(), + }, } } } diff --git a/crates/nostr-relay-pool/src/lib.rs b/crates/nostr-relay-pool/src/lib.rs index 3fed4395a..c92fa3406 100644 --- a/crates/nostr-relay-pool/src/lib.rs +++ b/crates/nostr-relay-pool/src/lib.rs @@ -16,7 +16,7 @@ pub mod relay; mod util; pub use self::pool::options::RelayPoolOptions; -pub use self::pool::{Output, RelayPool, RelayPoolNotification, SendEventOutput, SubscribeOutput}; +pub use self::pool::{Output, RelayPool, RelayPoolNotification}; pub use self::relay::flags::{AtomicRelayServiceFlags, RelayServiceFlags}; pub use self::relay::limits::RelayLimits; pub use self::relay::options::{ diff --git a/crates/nostr-relay-pool/src/pool/internal.rs b/crates/nostr-relay-pool/src/pool/internal.rs index a38c6f226..7912ac8cd 100644 --- a/crates/nostr-relay-pool/src/pool/internal.rs +++ b/crates/nostr-relay-pool/src/pool/internal.rs @@ -18,7 +18,7 @@ use nostr_database::{DynNostrDatabase, IntoNostrDatabase, Order}; use tokio::sync::{broadcast, Mutex, RwLock}; use super::options::RelayPoolOptions; -use super::{Error, Output, RelayPoolNotification, SendEventOutput, SubscribeOutput}; +use super::{Error, Output, RelayPoolNotification}; use crate::relay::options::{FilterOptions, NegentropyOptions, RelayOptions, RelaySendOptions}; use crate::relay::{Relay, RelayBlacklist}; use crate::{util, SubscribeOptions}; @@ -199,7 +199,7 @@ impl InternalRelayPool { &self, msg: ClientMessage, opts: RelaySendOptions, - ) -> Result { + ) -> Result, Error> { let relays = self.relays().await; self.send_msg_to(relays.into_keys(), msg, opts).await } @@ -208,7 +208,7 @@ impl InternalRelayPool { &self, msgs: Vec, opts: RelaySendOptions, - ) -> Result { + ) -> Result, Error> { let relays = self.relays().await; self.batch_msg_to(relays.into_keys(), msgs, opts).await } @@ -218,7 +218,7 @@ impl InternalRelayPool { urls: I, msg: ClientMessage, opts: RelaySendOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -232,7 +232,7 @@ impl InternalRelayPool { urls: I, msgs: Vec, opts: RelaySendOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -275,12 +275,12 @@ impl InternalRelayPool { return Err(Error::RelayNotFound); } - let result: Arc> = Arc::new(Mutex::new(Output::default())); + let result: Arc>> = Arc::new(Mutex::new(Output::default())); let mut handles: Vec> = Vec::with_capacity(urls.len()); for (url, relay) in relays.into_iter().filter(|(url, ..)| urls.contains(url)) { let msgs: Vec = msgs.clone(); - let result: Arc> = result.clone(); + let result: Arc>> = result.clone(); let handle: JoinHandle<()> = thread::spawn(async move { match relay.batch_msg(msgs, opts).await { Ok(_) => { @@ -304,7 +304,7 @@ impl InternalRelayPool { handle.join().await?; } - let result: Output = util::take_mutex_ownership(result).await; + let result: Output<()> = util::take_mutex_ownership(result).await; if result.success.is_empty() { return Err(Error::MsgNotSent); @@ -318,7 +318,7 @@ impl InternalRelayPool { &self, event: Event, opts: RelaySendOptions, - ) -> Result { + ) -> Result, Error> { let relays: HashMap = self.relays().await; self.send_event_to(relays.into_keys(), event, opts).await } @@ -327,7 +327,7 @@ impl InternalRelayPool { &self, events: Vec, opts: RelaySendOptions, - ) -> Result { + ) -> Result, Error> { let relays = self.relays().await; self.batch_event_to(relays.into_keys(), events, opts).await } @@ -337,15 +337,18 @@ impl InternalRelayPool { urls: I, event: Event, opts: RelaySendOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, Error: From<::Err>, { - Ok(SendEventOutput { - id: event.id, - output: self.batch_event_to(urls, vec![event], opts).await?, + let event_id: EventId = event.id; + let output: Output<()> = self.batch_event_to(urls, vec![event], opts).await?; + Ok(Output { + val: event_id, + success: output.success, + failed: output.failed, }) } @@ -354,7 +357,7 @@ impl InternalRelayPool { urls: I, events: Vec, opts: RelaySendOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -395,12 +398,12 @@ impl InternalRelayPool { return Err(Error::RelayNotFound); } - let result: Arc> = Arc::new(Mutex::new(Output::default())); + let result: Arc>> = Arc::new(Mutex::new(Output::default())); let mut handles = Vec::with_capacity(urls.len()); for (url, relay) in relays.into_iter().filter(|(url, ..)| urls.contains(url)) { let events: Vec = events.clone(); - let result: Arc> = result.clone(); + let result: Arc>> = result.clone(); let handle = thread::spawn(async move { match relay.batch_event(events, opts).await { Ok(_) => { @@ -424,7 +427,7 @@ impl InternalRelayPool { handle.join().await?; } - let result: Output = util::take_mutex_ownership(result).await; + let result: Output<()> = util::take_mutex_ownership(result).await; if result.success.is_empty() { return Err(Error::EventNotPublished); @@ -438,10 +441,14 @@ impl InternalRelayPool { &self, filters: Vec, opts: SubscribeOptions, - ) -> Result { + ) -> Result, Error> { let id: SubscriptionId = SubscriptionId::generate(); - let output: Output = self.subscribe_with_id(id.clone(), filters, opts).await?; - Ok(SubscribeOutput { id, output }) + let output: Output<()> = self.subscribe_with_id(id.clone(), filters, opts).await?; + Ok(Output { + val: id, + success: output.success, + failed: output.failed, + }) } pub async fn subscribe_with_id( @@ -449,7 +456,7 @@ impl InternalRelayPool { id: SubscriptionId, filters: Vec, opts: SubscribeOptions, - ) -> Result { + ) -> Result, Error> { // Check if isn't auto-closing subscription if !opts.is_auto_closing() { // Update pool subscriptions @@ -470,17 +477,21 @@ impl InternalRelayPool { urls: I, filters: Vec, opts: SubscribeOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, Error: From<::Err>, { let id: SubscriptionId = SubscriptionId::generate(); - let output: Output = self + let output: Output<()> = self .subscribe_with_id_to(urls, id.clone(), filters, opts) .await?; - Ok(SubscribeOutput { id, output }) + Ok(Output { + val: id, + success: output.success, + failed: output.failed, + }) } pub async fn subscribe_with_id_to( @@ -489,7 +500,7 @@ impl InternalRelayPool { id: SubscriptionId, filters: Vec, opts: SubscribeOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -526,14 +537,14 @@ impl InternalRelayPool { return Err(Error::RelayNotFound); } - let result: Arc> = Arc::new(Mutex::new(Output::default())); + let result: Arc>> = Arc::new(Mutex::new(Output::default())); let mut handles: Vec> = Vec::with_capacity(urls.len()); // Subscribe for (url, relay) in relays.into_iter().filter(|(url, ..)| urls.contains(url)) { let id: SubscriptionId = id.clone(); let filters: Vec = filters.clone(); - let result: Arc> = result.clone(); + let result: Arc>> = result.clone(); let handle: JoinHandle<()> = thread::spawn(async move { match relay.subscribe_with_id(id, filters, opts).await { Ok(_) => { @@ -557,7 +568,7 @@ impl InternalRelayPool { handle.join().await?; } - let result: Output = util::take_mutex_ownership(result).await; + let result: Output<()> = util::take_mutex_ownership(result).await; if result.success.is_empty() { return Err(Error::NotSubscribed); @@ -718,7 +729,7 @@ impl InternalRelayPool { &self, filter: Filter, opts: NegentropyOptions, - ) -> Result { + ) -> Result, Error> { let relays: HashMap = self.relays().await; self.reconcile_with(relays.into_keys(), filter, opts).await } @@ -729,7 +740,7 @@ impl InternalRelayPool { urls: I, filter: Filter, opts: NegentropyOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -746,7 +757,7 @@ impl InternalRelayPool { filter: Filter, items: Vec<(EventId, Timestamp)>, opts: NegentropyOptions, - ) -> Result { + ) -> Result, Error> { let relays: HashMap = self.relays().await; self.reconcile_advanced(relays.into_keys(), filter, items, opts) .await @@ -758,7 +769,7 @@ impl InternalRelayPool { filter: Filter, items: Vec<(EventId, Timestamp)>, opts: NegentropyOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -788,12 +799,12 @@ impl InternalRelayPool { } // Filter relays and start query - let result: Arc> = Arc::new(Mutex::new(Output::default())); + let result: Arc>> = Arc::new(Mutex::new(Output::default())); let mut handles: Vec> = Vec::with_capacity(urls.len()); for (url, relay) in relays.into_iter().filter(|(url, ..)| urls.contains(url)) { let filter: Filter = filter.clone(); let my_items: Vec<(EventId, Timestamp)> = items.clone(); - let result: Arc> = result.clone(); + let result: Arc>> = result.clone(); let handle: JoinHandle<()> = thread::spawn(async move { match relay.reconcile_with_items(filter, my_items, opts).await { Ok(_) => { @@ -817,7 +828,7 @@ impl InternalRelayPool { handle.join().await?; } - let result: Output = util::take_mutex_ownership(result).await; + let result: Output<()> = util::take_mutex_ownership(result).await; if result.success.is_empty() { return Err(Error::NegentropyReconciliationFailed); diff --git a/crates/nostr-relay-pool/src/pool/mod.rs b/crates/nostr-relay-pool/src/pool/mod.rs index 8a70d97d7..462a08b5e 100644 --- a/crates/nostr-relay-pool/src/pool/mod.rs +++ b/crates/nostr-relay-pool/src/pool/mod.rs @@ -25,7 +25,7 @@ mod result; pub use self::error::Error; use self::internal::InternalRelayPool; pub use self::options::RelayPoolOptions; -pub use self::result::{Output, SendEventOutput, SubscribeOutput}; +pub use self::result::Output; use crate::relay::options::{FilterOptions, NegentropyOptions, RelayOptions, RelaySendOptions}; use crate::relay::{Relay, RelayBlacklist, RelayStatus}; use crate::SubscribeOptions; @@ -216,7 +216,7 @@ impl RelayPool { &self, msg: ClientMessage, opts: RelaySendOptions, - ) -> Result { + ) -> Result, Error> { self.inner.send_msg(msg, opts).await } @@ -226,7 +226,7 @@ impl RelayPool { &self, msgs: Vec, opts: RelaySendOptions, - ) -> Result { + ) -> Result, Error> { self.inner.batch_msg(msgs, opts).await } @@ -239,7 +239,7 @@ impl RelayPool { urls: I, msg: ClientMessage, opts: RelaySendOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -257,7 +257,7 @@ impl RelayPool { urls: I, msgs: Vec, opts: RelaySendOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -272,7 +272,7 @@ impl RelayPool { &self, event: Event, opts: RelaySendOptions, - ) -> Result { + ) -> Result, Error> { self.inner.send_event(event, opts).await } @@ -282,7 +282,7 @@ impl RelayPool { &self, events: Vec, opts: RelaySendOptions, - ) -> Result { + ) -> Result, Error> { self.inner.batch_event(events, opts).await } @@ -293,7 +293,7 @@ impl RelayPool { urls: I, event: Event, opts: RelaySendOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -309,7 +309,7 @@ impl RelayPool { urls: I, events: Vec, opts: RelaySendOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -330,7 +330,7 @@ impl RelayPool { &self, filters: Vec, opts: SubscribeOptions, - ) -> Result { + ) -> Result, Error> { self.inner.subscribe(filters, opts).await } @@ -347,7 +347,7 @@ impl RelayPool { id: SubscriptionId, filters: Vec, opts: SubscribeOptions, - ) -> Result { + ) -> Result, Error> { self.inner.subscribe_with_id(id, filters, opts).await } @@ -362,7 +362,7 @@ impl RelayPool { urls: I, filters: Vec, opts: SubscribeOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -383,7 +383,7 @@ impl RelayPool { id: SubscriptionId, filters: Vec, opts: SubscribeOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -448,7 +448,7 @@ impl RelayPool { &self, filter: Filter, opts: NegentropyOptions, - ) -> Result { + ) -> Result, Error> { self.inner.reconcile(filter, opts).await } @@ -459,7 +459,7 @@ impl RelayPool { urls: I, filter: Filter, opts: NegentropyOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -475,7 +475,7 @@ impl RelayPool { filter: Filter, items: Vec<(EventId, Timestamp)>, opts: NegentropyOptions, - ) -> Result { + ) -> Result, Error> { self.inner.reconcile_with_items(filter, items, opts).await } @@ -487,7 +487,7 @@ impl RelayPool { filter: Filter, items: Vec<(EventId, Timestamp)>, opts: NegentropyOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, diff --git a/crates/nostr-relay-pool/src/pool/result.rs b/crates/nostr-relay-pool/src/pool/result.rs index dac9371f1..f3f2d82ac 100644 --- a/crates/nostr-relay-pool/src/pool/result.rs +++ b/crates/nostr-relay-pool/src/pool/result.rs @@ -3,77 +3,47 @@ // Distributed under the MIT software license use std::collections::{HashMap, HashSet}; -use std::fmt; -use std::fmt::Formatter; +use std::fmt::Debug; use std::ops::Deref; -use nostr::{EventId, SubscriptionId, Url}; +use nostr::Url; /// Output /// /// Send or negentropy reconciliation output // TODO: use a better name? #[derive(Debug, Clone, Default, PartialEq, Eq)] -pub struct Output { +pub struct Output +where + T: Debug, +{ + /// Value + pub val: T, /// Set of relays that success pub success: HashSet, /// Map of relays that failed, with related errors. pub failed: HashMap>, } -impl Output { +impl Output<()> { pub(super) fn success(url: Url) -> Self { let mut success: HashSet = HashSet::with_capacity(1); success.insert(url); Self { + val: (), success, failed: HashMap::new(), } } } -/// Send event output -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct SendEventOutput { - /// Event ID - pub id: EventId, - /// Output - pub output: Output, -} - -impl Deref for SendEventOutput { - type Target = EventId; +impl Deref for Output +where + T: Debug, +{ + type Target = T; fn deref(&self) -> &Self::Target { - &self.id - } -} - -impl fmt::Display for SendEventOutput { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.id) - } -} - -/// Subscribe output -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct SubscribeOutput { - /// Subscription ID - pub id: SubscriptionId, - /// Output - pub output: Output, -} - -impl Deref for SubscribeOutput { - type Target = SubscriptionId; - - fn deref(&self) -> &Self::Target { - &self.id - } -} - -impl fmt::Display for SubscribeOutput { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.id) + &self.val } } diff --git a/crates/nostr-sdk/examples/client.rs b/crates/nostr-sdk/examples/client.rs index a85e0664f..d685a7e6c 100644 --- a/crates/nostr-sdk/examples/client.rs +++ b/crates/nostr-sdk/examples/client.rs @@ -21,8 +21,8 @@ async fn main() -> Result<()> { client.connect().await; // Publish a text note - let SendEventOutput { id, output } = client.publish_text_note("Hello world", []).await?; - println!("Event ID: {}", id.to_bech32()?); + let output = client.publish_text_note("Hello world", []).await?; + println!("Event ID: {}", output.to_bech32()?); println!("Sent to: {:?}", output.success); println!("Not sent to: {:?}", output.failed); diff --git a/crates/nostr-sdk/examples/negentropy.rs b/crates/nostr-sdk/examples/negentropy.rs index 1b10ec141..c324003b0 100644 --- a/crates/nostr-sdk/examples/negentropy.rs +++ b/crates/nostr-sdk/examples/negentropy.rs @@ -22,9 +22,9 @@ async fn main() -> Result<()> { let my_items = Vec::new(); let filter = Filter::new().author(public_key).limit(10); let opts = NegentropyOptions::default(); - let Output { success, failed } = client.reconcile_with_items(filter, my_items, opts).await?; - println!("Success: {success:?}"); - println!("Failed: {failed:?}"); + let output = client.reconcile_with_items(filter, my_items, opts).await?; + println!("Success: {:?}", output.success); + println!("Failed: {:?}", output.failed); Ok(()) } diff --git a/crates/nostr-sdk/examples/nostr-connect.rs b/crates/nostr-sdk/examples/nostr-connect.rs index df6d221b2..4443f96a1 100644 --- a/crates/nostr-sdk/examples/nostr-connect.rs +++ b/crates/nostr-sdk/examples/nostr-connect.rs @@ -39,14 +39,14 @@ async fn main() -> Result<()> { let output = client .publish_text_note("Testing rust-nostr NIP46 signer [bunker]", []) .await?; - println!("Published text note: {output}\n"); + println!("Published text note: {}\n", output.val); let receiver = PublicKey::from_bech32("npub1drvpzev3syqt0kjrls50050uzf25gehpz9vgdw08hvex7e0vgfeq0eseet")?; - client + let output = client .send_private_msg(receiver, "Hello from rust-nostr", None) .await?; - println!("Sent DM: {output}"); + println!("Sent DM: {}", output.val); Ok(()) } diff --git a/crates/nostr-sdk/examples/subscriptions.rs b/crates/nostr-sdk/examples/subscriptions.rs index 87ba4d124..4a67397b8 100644 --- a/crates/nostr-sdk/examples/subscriptions.rs +++ b/crates/nostr-sdk/examples/subscriptions.rs @@ -29,7 +29,7 @@ async fn main() -> Result<()> { .since(Timestamp::now()); // Subscribe (auto generate subscription ID) - let SubscribeOutput { id: sub_id_1, .. } = client.subscribe(vec![subscription], None).await?; + let Output { val: sub_id_1, .. } = client.subscribe(vec![subscription], None).await?; // Subscribe with custom ID let sub_id_2 = SubscriptionId::new("other-id"); diff --git a/crates/nostr-sdk/src/client/mod.rs b/crates/nostr-sdk/src/client/mod.rs index 85e15e347..b33e17d8b 100644 --- a/crates/nostr-sdk/src/client/mod.rs +++ b/crates/nostr-sdk/src/client/mod.rs @@ -642,15 +642,15 @@ impl Client { /// .since(Timestamp::now()); /// /// // Subscribe - /// let sub_id = client.subscribe(vec![subscription], None).await?; - /// println!("Subscription ID: {sub_id}"); + /// let output = client.subscribe(vec![subscription], None).await?; + /// println!("Subscription ID: {}", output.val); /// /// // Auto-closing subscription /// let id = SubscriptionId::generate(); /// let subscription = Filter::new().kind(Kind::TextNote).limit(10); /// let opts = SubscribeAutoCloseOptions::default().filter(FilterOptions::ExitOnEOSE); - /// let sub_id = client.subscribe(vec![subscription], Some(opts)).await?; - /// println!("Subscription ID: {sub_id} [auto-closing]"); + /// let output = client.subscribe(vec![subscription], Some(opts)).await?; + /// println!("Subscription ID: {} [auto-closing]", output.val); /// # Ok(()) /// # } /// ``` @@ -658,7 +658,7 @@ impl Client { &self, filters: Vec, opts: Option, - ) -> Result { + ) -> Result, Error> { let send_opts: RelaySendOptions = self.opts.get_wait_for_subscription(); let opts: SubscribeOptions = SubscribeOptions::default() .close_on(opts) @@ -708,7 +708,7 @@ impl Client { id: SubscriptionId, filters: Vec, opts: Option, - ) -> Result { + ) -> Result, Error> { let send_opts: RelaySendOptions = self.opts.get_wait_for_subscription(); let opts: SubscribeOptions = SubscribeOptions::default() .close_on(opts) @@ -730,7 +730,7 @@ impl Client { urls: I, filters: Vec, opts: Option, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -755,7 +755,7 @@ impl Client { id: SubscriptionId, filters: Vec, opts: Option, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -858,7 +858,7 @@ impl Client { /// Send client message to **all relays** #[inline] - pub async fn send_msg(&self, msg: ClientMessage) -> Result { + pub async fn send_msg(&self, msg: ClientMessage) -> Result, Error> { let opts: RelaySendOptions = self.opts.get_wait_for_send(); Ok(self.pool.send_msg(msg, opts).await?) } @@ -869,13 +869,13 @@ impl Client { &self, msgs: Vec, opts: RelaySendOptions, - ) -> Result { + ) -> Result, Error> { Ok(self.pool.batch_msg(msgs, opts).await?) } /// Send client message to a **specific relays** #[inline] - pub async fn send_msg_to(&self, urls: I, msg: ClientMessage) -> Result + pub async fn send_msg_to(&self, urls: I, msg: ClientMessage) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -892,7 +892,7 @@ impl Client { urls: I, msgs: Vec, opts: RelaySendOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -906,7 +906,7 @@ impl Client { /// This method will wait for the `OK` message from the relay. /// If you not want to wait for the `OK` message, use `send_msg` method instead. #[inline] - pub async fn send_event(&self, event: Event) -> Result { + pub async fn send_event(&self, event: Event) -> Result, Error> { let opts: RelaySendOptions = self.opts.get_wait_for_send(); Ok(self.pool.send_event(event, opts).await?) } @@ -917,7 +917,7 @@ impl Client { &self, events: Vec, opts: RelaySendOptions, - ) -> Result { + ) -> Result, Error> { Ok(self.pool.batch_event(events, opts).await?) } @@ -926,7 +926,7 @@ impl Client { /// This method will wait for the `OK` message from the relay. /// If you not want to wait for the `OK` message, use `send_msg` method instead. #[inline] - pub async fn send_event_to(&self, urls: I, event: Event) -> Result + pub async fn send_event_to(&self, urls: I, event: Event) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -943,7 +943,7 @@ impl Client { urls: I, events: Vec, opts: RelaySendOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -974,7 +974,7 @@ impl Client { pub async fn send_event_builder( &self, builder: EventBuilder, - ) -> Result { + ) -> Result, Error> { let event: Event = self.sign_event_builder(builder).await?; self.send_event(event).await } @@ -987,7 +987,7 @@ impl Client { &self, urls: I, builder: EventBuilder, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -1035,7 +1035,7 @@ impl Client { /// # } /// ``` #[inline] - pub async fn set_metadata(&self, metadata: &Metadata) -> Result { + pub async fn set_metadata(&self, metadata: &Metadata) -> Result, Error> { let builder = EventBuilder::metadata(metadata); self.send_event_builder(builder).await } @@ -1044,7 +1044,7 @@ impl Client { /// /// #[inline] - pub async fn set_relay_list(&self, relays: I) -> Result + pub async fn set_relay_list(&self, relays: I) -> Result, Error> where I: IntoIterator)>, { @@ -1075,7 +1075,7 @@ impl Client { &self, content: S, tags: I, - ) -> Result + ) -> Result, Error> where S: Into, I: IntoIterator, @@ -1088,7 +1088,7 @@ impl Client { /// /// #[inline] - pub async fn set_contact_list(&self, list: I) -> Result + pub async fn set_contact_list(&self, list: I) -> Result, Error> where I: IntoIterator, { @@ -1206,7 +1206,7 @@ impl Client { receiver: PublicKey, msg: S, reply_to: Option, - ) -> Result + ) -> Result, Error> where S: Into, { @@ -1234,7 +1234,7 @@ impl Client { receiver: PublicKey, message: S, reply_to: Option, - ) -> Result + ) -> Result, Error> where S: Into, { @@ -1248,7 +1248,7 @@ impl Client { &self, event: &Event, relay_url: Option, - ) -> Result { + ) -> Result, Error> { let builder = EventBuilder::repost(event, relay_url); self.send_event_builder(builder).await } @@ -1257,7 +1257,7 @@ impl Client { /// /// #[inline] - pub async fn delete_event(&self, id: T) -> Result + pub async fn delete_event(&self, id: T) -> Result, Error> where T: Into, { @@ -1287,7 +1287,7 @@ impl Client { /// # } /// ``` #[inline] - pub async fn like(&self, event: &Event) -> Result { + pub async fn like(&self, event: &Event) -> Result, Error> { self.reaction(event, "+").await } @@ -1313,7 +1313,7 @@ impl Client { /// # } /// ``` #[inline] - pub async fn dislike(&self, event: &Event) -> Result { + pub async fn dislike(&self, event: &Event) -> Result, Error> { self.reaction(event, "-").await } @@ -1339,7 +1339,7 @@ impl Client { /// # } /// ``` #[inline] - pub async fn reaction(&self, event: &Event, reaction: S) -> Result + pub async fn reaction(&self, event: &Event, reaction: S) -> Result, Error> where S: Into, { @@ -1351,7 +1351,7 @@ impl Client { /// /// #[inline] - pub async fn new_channel(&self, metadata: &Metadata) -> Result { + pub async fn new_channel(&self, metadata: &Metadata) -> Result, Error> { let builder = EventBuilder::channel(metadata); self.send_event_builder(builder).await } @@ -1365,7 +1365,7 @@ impl Client { channel_id: EventId, relay_url: Option, metadata: &Metadata, - ) -> Result { + ) -> Result, Error> { let builder = EventBuilder::channel_metadata(channel_id, relay_url, metadata); self.send_event_builder(builder).await } @@ -1379,7 +1379,7 @@ impl Client { channel_id: EventId, relay_url: Url, msg: S, - ) -> Result + ) -> Result, Error> where S: Into, { @@ -1395,7 +1395,7 @@ impl Client { &self, message_id: EventId, reason: Option, - ) -> Result + ) -> Result, Error> where S: Into, { @@ -1411,7 +1411,7 @@ impl Client { &self, pubkey: PublicKey, reason: Option, - ) -> Result + ) -> Result, Error> where S: Into, { @@ -1425,7 +1425,7 @@ impl Client { /// /// #[inline] - pub async fn auth(&self, challenge: S, relay: Url) -> Result + pub async fn auth(&self, challenge: S, relay: Url) -> Result, Error> where S: Into, { @@ -1443,7 +1443,7 @@ impl Client { bolt11: S, preimage: Option, zap_request: &Event, - ) -> Result + ) -> Result, Error> where S: Into, { @@ -1475,7 +1475,7 @@ impl Client { receiver: PublicKey, rumor: EventBuilder, expiration: Option, - ) -> Result { + ) -> Result, Error> { // Compose rumor let signer: NostrSigner = self.signer().await?; let public_key: PublicKey = signer.public_key().await?; @@ -1515,7 +1515,7 @@ impl Client { &self, description: S, metadata: FileMetadata, - ) -> Result + ) -> Result, Error> where S: Into, { @@ -1531,7 +1531,7 @@ impl Client { &self, filter: Filter, opts: NegentropyOptions, - ) -> Result { + ) -> Result, Error> { Ok(self.pool.reconcile(filter, opts).await?) } @@ -1544,7 +1544,7 @@ impl Client { urls: I, filter: Filter, opts: NegentropyOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl, @@ -1560,7 +1560,7 @@ impl Client { filter: Filter, items: Vec<(EventId, Timestamp)>, opts: NegentropyOptions, - ) -> Result { + ) -> Result, Error> { Ok(self.pool.reconcile_with_items(filter, items, opts).await?) } @@ -1574,7 +1574,7 @@ impl Client { filter: Filter, items: Vec<(EventId, Timestamp)>, opts: NegentropyOptions, - ) -> Result + ) -> Result, Error> where I: IntoIterator, U: TryIntoUrl,