From ec790f4e31c77c5cb0816a3d263b420ac3bc8a6b Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Wed, 27 Nov 2024 12:35:18 +0100 Subject: [PATCH] nostr: refactor NIP19 relay URL parsing logic Update NIP19 relay URL parsing to use `str::from_utf8` and handle potential parsing errors gracefully. This improves code efficiency and error handling by eliminating unnecessary allocation. Closes https://github.com/rust-nostr/nostr/issues/649 Signed-off-by: Yuki Kishimoto --- crates/nostr/src/nips/nip19.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/crates/nostr/src/nips/nip19.rs b/crates/nostr/src/nips/nip19.rs index 7bef4bd88..868cf3aea 100644 --- a/crates/nostr/src/nips/nip19.rs +++ b/crates/nostr/src/nips/nip19.rs @@ -595,9 +595,10 @@ impl Nip19Profile { } } RELAY => { - let url = String::from_utf8(bytes.to_vec())?; - let url = RelayUrl::parse(&url)?; - relays.push(url); + let url: &str = str::from_utf8(bytes)?; + if let Ok(url) = RelayUrl::parse(url) { + relays.push(url); + } } _ => (), }; @@ -673,8 +674,10 @@ impl Coordinate { } } RELAY => { - let url = String::from_utf8(bytes.to_vec())?; - relays.push(RelayUrl::parse(url)?); + let url: &str = str::from_utf8(bytes)?; + if let Ok(url) = RelayUrl::parse(url) { + relays.push(url); + } } AUTHOR => { if pubkey.is_none() {