From cd0e1f699ab7c3903c9fc369a882081f98cb8151 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Wed, 27 Nov 2024 13:07:07 +0100 Subject: [PATCH] ffi: simplify error conversions in `NostrSdkError` Consolidate numerous `impl From` conversions into a single generic implementation. Signed-off-by: Yuki Kishimoto --- bindings/nostr-sdk-ffi/src/error.rs | 258 +----------------- .../src/protocol/signer/custom.rs | 31 ++- 2 files changed, 26 insertions(+), 263 deletions(-) diff --git a/bindings/nostr-sdk-ffi/src/error.rs b/bindings/nostr-sdk-ffi/src/error.rs index 2f5fa6505..450bd296d 100644 --- a/bindings/nostr-sdk-ffi/src/error.rs +++ b/bindings/nostr-sdk-ffi/src/error.rs @@ -3,10 +3,7 @@ // Distributed under the MIT software license use std::fmt; -use std::net::AddrParseError; -use nostr::message::MessageHandleError; -use tracing::subscriber::SetGlobalDefaultError; use uniffi::Error; pub type Result = std::result::Result; @@ -17,8 +14,6 @@ pub enum NostrSdkError { Generic(String), } -impl std::error::Error for NostrSdkError {} - impl fmt::Display for NostrSdkError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { @@ -27,254 +22,11 @@ impl fmt::Display for NostrSdkError { } } -impl From for NostrSdkError { - fn from(e: std::num::ParseIntError) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: std::char::ParseCharError) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::signer::SignerError) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::key::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::key::vanity::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: MessageHandleError) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::types::metadata::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::event::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::event::builder::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::event::unsigned::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::event::tag::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip01::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip04::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip05::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip06::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip11::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip19::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip21::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip26::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip44::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip46::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip47::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip49::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip53::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip57::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip59::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip90::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::secp256k1::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::types::url::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::types::url::ParseError) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::hashes::hex::HexToArrayError) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::serde_json::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::event::id::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: AddrParseError) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: SetGlobalDefaultError) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr_sdk::client::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr_sdk::pool::relay::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr_sdk::pool::pool::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr_sdk::prelude::DatabaseError) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr_connect::error::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr_sdk::nwc::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr_relay_builder::error::Error) -> NostrSdkError { +impl From for NostrSdkError +where + T: std::error::Error, +{ + fn from(e: T) -> NostrSdkError { Self::Generic(e.to_string()) } } diff --git a/bindings/nostr-sdk-ffi/src/protocol/signer/custom.rs b/bindings/nostr-sdk-ffi/src/protocol/signer/custom.rs index a82bbecc3..9b1138be0 100644 --- a/bindings/nostr-sdk-ffi/src/protocol/signer/custom.rs +++ b/bindings/nostr-sdk-ffi/src/protocol/signer/custom.rs @@ -50,6 +50,7 @@ impl fmt::Debug for IntermediateCustomNostrSigner { } mod inner { + use std::fmt; use std::ops::Deref; use std::sync::Arc; @@ -57,8 +58,18 @@ mod inner { use nostr::prelude::*; use super::IntermediateCustomNostrSigner; - use crate::error::NostrSdkError; - + + #[derive(Debug)] + struct Error(String); + + impl std::error::Error for Error {} + + impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) + } + } + #[async_trait] impl NostrSigner for IntermediateCustomNostrSigner { fn backend(&self) -> SignerBackend { @@ -70,9 +81,9 @@ mod inner { .inner .get_public_key() .await - .map_err(SignerError::backend)? + .map_err(|e| SignerError::backend(Error(e.to_string())))? .ok_or_else(|| { - SignerError::backend(NostrSdkError::Generic(String::from( + SignerError::backend(Error(String::from( "Received None instead of public key", ))) })?; @@ -85,9 +96,9 @@ mod inner { .inner .sign_event(unsigned) .await - .map_err(SignerError::backend)? + .map_err(|e| SignerError::backend(Error(e.to_string())))? .ok_or_else(|| { - SignerError::backend(NostrSdkError::Generic(String::from( + SignerError::backend(Error(String::from( "Received None instead of event", ))) })?; @@ -103,7 +114,7 @@ mod inner { self.inner .nip04_encrypt(public_key, content.to_string()) .await - .map_err(SignerError::backend) + .map_err(|e| SignerError::backend(Error(e.to_string()))) } async fn nip04_decrypt( @@ -115,7 +126,7 @@ mod inner { self.inner .nip04_decrypt(public_key, encrypted_content.to_string()) .await - .map_err(SignerError::backend) + .map_err(|e| SignerError::backend(Error(e.to_string()))) } async fn nip44_encrypt( @@ -127,7 +138,7 @@ mod inner { self.inner .nip44_encrypt(public_key, content.to_string()) .await - .map_err(SignerError::backend) + .map_err(|e| SignerError::backend(Error(e.to_string()))) } async fn nip44_decrypt( @@ -139,7 +150,7 @@ mod inner { self.inner .nip44_decrypt(public_key, payload.to_string()) .await - .map_err(SignerError::backend) + .map_err(|e| SignerError::backend(Error(e.to_string()))) } } }