Skip to content

Commit

Permalink
ffi: simplify error conversions in NostrSdkError
Browse files Browse the repository at this point in the history
Consolidate numerous `impl From` conversions into a single generic implementation.

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Nov 27, 2024
1 parent 406ddf2 commit cd0e1f6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 263 deletions.
258 changes: 5 additions & 253 deletions bindings/nostr-sdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, E = NostrSdkError> = std::result::Result<T, E>;
Expand All @@ -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 {
Expand All @@ -27,254 +22,11 @@ impl fmt::Display for NostrSdkError {
}
}

impl From<std::num::ParseIntError> for NostrSdkError {
fn from(e: std::num::ParseIntError) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<std::char::ParseCharError> for NostrSdkError {
fn from(e: std::char::ParseCharError) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::signer::SignerError> for NostrSdkError {
fn from(e: nostr::signer::SignerError) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::key::Error> for NostrSdkError {
fn from(e: nostr::key::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::key::vanity::Error> for NostrSdkError {
fn from(e: nostr::key::vanity::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<MessageHandleError> for NostrSdkError {
fn from(e: MessageHandleError) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::types::metadata::Error> for NostrSdkError {
fn from(e: nostr::types::metadata::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::event::Error> for NostrSdkError {
fn from(e: nostr::event::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::event::builder::Error> for NostrSdkError {
fn from(e: nostr::event::builder::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::event::unsigned::Error> for NostrSdkError {
fn from(e: nostr::event::unsigned::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::event::tag::Error> for NostrSdkError {
fn from(e: nostr::event::tag::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::nips::nip01::Error> for NostrSdkError {
fn from(e: nostr::nips::nip01::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::nips::nip04::Error> for NostrSdkError {
fn from(e: nostr::nips::nip04::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::nips::nip05::Error> for NostrSdkError {
fn from(e: nostr::nips::nip05::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::nips::nip06::Error> for NostrSdkError {
fn from(e: nostr::nips::nip06::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::nips::nip11::Error> for NostrSdkError {
fn from(e: nostr::nips::nip11::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::nips::nip19::Error> for NostrSdkError {
fn from(e: nostr::nips::nip19::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::nips::nip21::Error> for NostrSdkError {
fn from(e: nostr::nips::nip21::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::nips::nip26::Error> for NostrSdkError {
fn from(e: nostr::nips::nip26::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::nips::nip44::Error> for NostrSdkError {
fn from(e: nostr::nips::nip44::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::nips::nip46::Error> for NostrSdkError {
fn from(e: nostr::nips::nip46::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::nips::nip47::Error> for NostrSdkError {
fn from(e: nostr::nips::nip47::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::nips::nip49::Error> for NostrSdkError {
fn from(e: nostr::nips::nip49::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::nips::nip53::Error> for NostrSdkError {
fn from(e: nostr::nips::nip53::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::nips::nip57::Error> for NostrSdkError {
fn from(e: nostr::nips::nip57::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::nips::nip59::Error> for NostrSdkError {
fn from(e: nostr::nips::nip59::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::nips::nip90::Error> for NostrSdkError {
fn from(e: nostr::nips::nip90::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::secp256k1::Error> for NostrSdkError {
fn from(e: nostr::secp256k1::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::types::url::Error> for NostrSdkError {
fn from(e: nostr::types::url::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::types::url::ParseError> for NostrSdkError {
fn from(e: nostr::types::url::ParseError) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::hashes::hex::HexToArrayError> for NostrSdkError {
fn from(e: nostr::hashes::hex::HexToArrayError) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::serde_json::Error> for NostrSdkError {
fn from(e: nostr::serde_json::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr::event::id::Error> for NostrSdkError {
fn from(e: nostr::event::id::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<AddrParseError> for NostrSdkError {
fn from(e: AddrParseError) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<SetGlobalDefaultError> for NostrSdkError {
fn from(e: SetGlobalDefaultError) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr_sdk::client::Error> for NostrSdkError {
fn from(e: nostr_sdk::client::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr_sdk::pool::relay::Error> for NostrSdkError {
fn from(e: nostr_sdk::pool::relay::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr_sdk::pool::pool::Error> for NostrSdkError {
fn from(e: nostr_sdk::pool::pool::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr_sdk::prelude::DatabaseError> for NostrSdkError {
fn from(e: nostr_sdk::prelude::DatabaseError) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr_connect::error::Error> for NostrSdkError {
fn from(e: nostr_connect::error::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr_sdk::nwc::Error> for NostrSdkError {
fn from(e: nostr_sdk::nwc::Error) -> NostrSdkError {
Self::Generic(e.to_string())
}
}

impl From<nostr_relay_builder::error::Error> for NostrSdkError {
fn from(e: nostr_relay_builder::error::Error) -> NostrSdkError {
impl<T> From<T> for NostrSdkError
where
T: std::error::Error,
{
fn from(e: T) -> NostrSdkError {
Self::Generic(e.to_string())
}
}
31 changes: 21 additions & 10 deletions bindings/nostr-sdk-ffi/src/protocol/signer/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,26 @@ impl fmt::Debug for IntermediateCustomNostrSigner {
}

mod inner {
use std::fmt;
use std::ops::Deref;
use std::sync::Arc;

use async_trait::async_trait;
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 {
Expand All @@ -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",
)))
})?;
Expand All @@ -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",
)))
})?;
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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())))
}
}
}

0 comments on commit cd0e1f6

Please sign in to comment.