Skip to content

Commit

Permalink
[nostr] Remove thiserror dep
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed May 11, 2023
1 parent 2b50507 commit dd15e2f
Show file tree
Hide file tree
Showing 20 changed files with 763 additions and 212 deletions.
1 change: 0 additions & 1 deletion crates/nostr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ reqwest = { version = "0.11", default-features = false, features = ["json", "rus
secp256k1 = { version = "0.27", features = ["global-context", "rand-std", "serde"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0" }
thiserror = "1.0"
url = { version = "2", features = ["serde"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
66 changes: 54 additions & 12 deletions crates/nostr/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

//! Event builder
use core::fmt;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;

Expand All @@ -24,24 +25,65 @@ use crate::nips::nip46::Message as NostrConnectMessage;
use crate::types::{ChannelId, Contact, Metadata, Timestamp};

/// [`EventBuilder`] error
#[derive(Debug, thiserror::Error)]
#[derive(Debug)]
pub enum Error {
/// Key error
#[error(transparent)]
Key(#[from] key::Error),
/// Secp256k1 error
#[error(transparent)]
Secp256k1(#[from] secp256k1::Error),
Key(key::Error),
/// JSON error
#[error(transparent)]
Json(#[from] serde_json::Error),
Json(serde_json::Error),
/// Secp256k1 error
Secp256k1(secp256k1::Error),
/// Unsigned event error
#[error(transparent)]
Unsigned(#[from] super::unsigned::Error),
Unsigned(super::unsigned::Error),
/// NIP04 error
#[cfg(feature = "nip04")]
#[error(transparent)]
NIP04(#[from] nip04::Error),
NIP04(nip04::Error),
}

impl std::error::Error for Error {}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Key(e) => write!(f, "{e}"),
Self::Json(e) => write!(f, "{e}"),
Self::Secp256k1(e) => write!(f, "{e}"),
Self::Unsigned(e) => write!(f, "{e}"),
#[cfg(feature = "nip04")]
Self::NIP04(e) => write!(f, "{e}"),
}
}
}

impl From<key::Error> for Error {
fn from(e: key::Error) -> Self {
Self::Key(e)
}
}

impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Self::Json(e)
}
}

impl From<secp256k1::Error> for Error {
fn from(e: secp256k1::Error) -> Self {
Self::Secp256k1(e)
}
}

impl From<super::unsigned::Error> for Error {
fn from(e: super::unsigned::Error) -> Self {
Self::Unsigned(e)
}
}

#[cfg(feature = "nip04")]
impl From<nip04::Error> for Error {
fn from(e: nip04::Error) -> Self {
Self::NIP04(e)
}
}

/// [`Event`] builder
Expand Down
35 changes: 28 additions & 7 deletions crates/nostr/src/event/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

//! Event Id
use std::fmt;
use std::str::FromStr;
use core::fmt;
use core::str::FromStr;

use bitcoin_hashes::sha256::Hash as Sha256Hash;
use bitcoin_hashes::Hash;
Expand All @@ -16,14 +16,35 @@ use super::{Kind, Tag};
use crate::Timestamp;

/// [`EventId`] error
#[derive(Debug, PartialEq, Eq, thiserror::Error)]
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
/// Hex error
#[error(transparent)]
Hex(#[from] bitcoin_hashes::hex::Error),
Hex(bitcoin_hashes::hex::Error),
/// Hash error
#[error(transparent)]
Hash(#[from] bitcoin_hashes::Error),
Hash(bitcoin_hashes::Error),
}

impl std::error::Error for Error {}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Hex(e) => write!(f, "{e}"),
Self::Hash(e) => write!(f, "{e}"),
}
}
}

impl From<bitcoin_hashes::hex::Error> for Error {
fn from(e: bitcoin_hashes::hex::Error) -> Self {
Self::Hex(e)
}
}

impl From<bitcoin_hashes::Error> for Error {
fn from(e: bitcoin_hashes::Error) -> Self {
Self::Hash(e)
}
}

/// Event Id
Expand Down
58 changes: 47 additions & 11 deletions crates/nostr/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

//! Event
use std::str::FromStr;
use core::fmt;
use core::str::FromStr;

use secp256k1::schnorr::Signature;
use secp256k1::{Message, XOnlyPublicKey};
Expand All @@ -25,24 +26,59 @@ pub use self::unsigned::UnsignedEvent;
use crate::{Timestamp, SECP256K1};

/// [`Event`] error
#[derive(Debug, thiserror::Error)]
#[derive(Debug)]
pub enum Error {
/// Invalid signature
#[error("invalid signature")]
InvalidSignature,
/// Error serializing or deserializing JSON data
#[error(transparent)]
Json(#[from] serde_json::Error),
Json(serde_json::Error),
/// Secp256k1 error
#[error(transparent)]
Secp256k1(#[from] secp256k1::Error),
Secp256k1(secp256k1::Error),
/// Hex decoding error
#[error(transparent)]
Hex(#[from] bitcoin_hashes::hex::Error),
Hex(bitcoin_hashes::hex::Error),
/// OpenTimestamps error
#[cfg(feature = "nip03")]
#[error(transparent)]
OpenTimestamps(#[from] nostr_ots::Error),
OpenTimestamps(nostr_ots::Error),
}

impl std::error::Error for Error {}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidSignature => write!(f, "Invalid signature"),
Self::Json(e) => write!(f, "{e}"),
Self::Secp256k1(e) => write!(f, "{e}"),
Self::Hex(e) => write!(f, "{e}"),
#[cfg(feature = "nip03")]
Self::OpenTimestamps(e) => write!(f, "{e}"),
}
}
}

impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Self::Json(e)
}
}

impl From<secp256k1::Error> for Error {
fn from(e: secp256k1::Error) -> Self {
Self::Secp256k1(e)
}
}

impl From<bitcoin_hashes::hex::Error> for Error {
fn from(e: bitcoin_hashes::hex::Error) -> Self {
Self::Hex(e)
}
}

#[cfg(feature = "nip03")]
impl From<nostr_ots::Error> for Error {
fn from(e: nostr_ots::Error) -> Self {
Self::OpenTimestamps(e)
}
}

/// [`Event`] struct
Expand Down
103 changes: 77 additions & 26 deletions crates/nostr/src/event/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

//! Tag
use std::fmt;
use std::num::ParseIntError;
use std::str::FromStr;
use core::fmt;
use core::num::ParseIntError;
use core::str::FromStr;

use secp256k1::schnorr::Signature;
use secp256k1::XOnlyPublicKey;
Expand All @@ -14,48 +14,99 @@ use serde::ser::SerializeSeq;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use super::id::{self, EventId};
use crate::nips::nip26::Conditions;
use crate::nips::nip26::{Conditions, Error as Nip26Error};
use crate::{Kind, Timestamp, UncheckedUrl};

/// [`Tag`] error
#[derive(Debug, thiserror::Error)]
#[derive(Debug)]
pub enum Error {
/// Impossible to parse [`Marker`]
#[error("impossible to parse marker")]
MarkerParseError,
/// Unknown [`Report`]
#[error("unknown report type")]
UnknownReportType,
/// Impossible to find tag kind
#[error("impossible to find tag kind")]
KindNotFound,
/// Invalid length
#[error("invalid length")]
InvalidLength,
/// Invalid Zap Request
InvalidZapRequest,
/// Impossible to parse integer
#[error(transparent)]
ParseIntError(#[from] ParseIntError),
ParseIntError(ParseIntError),
/// Secp256k1
#[error(transparent)]
Secp256k1(#[from] secp256k1::Error),
Secp256k1(secp256k1::Error),
/// Hex decoding error
#[error(transparent)]
Hex(#[from] bitcoin_hashes::hex::Error),
Hex(bitcoin_hashes::hex::Error),
/// Url parse error
#[error("invalid url: {0}")]
Url(#[from] url::ParseError),
Url(url::ParseError),
/// EventId error
#[error(transparent)]
EventId(#[from] id::Error),
EventId(id::Error),
/// NIP26 error
#[error(transparent)]
Nip26(#[from] crate::nips::nip26::Error),
NIP26(Nip26Error),
/// Event Error
#[error(transparent)]
Event(#[from] crate::event::Error),
/// Invalid Zap Request
#[error("Invalid Zap request")]
InvalidZapRequest,
Event(crate::event::Error),
}

impl std::error::Error for Error {}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MarkerParseError => write!(f, "impossible to parse marker"),
Self::UnknownReportType => write!(f, "unknown report type"),
Self::KindNotFound => write!(f, "impossible to find tag kind"),
Self::InvalidLength => write!(f, "invalid length"),
Self::InvalidZapRequest => write!(f, "invalid Zap request"),
Self::ParseIntError(e) => write!(f, "{e}"),
Self::Secp256k1(e) => write!(f, "{e}"),
Self::Hex(e) => write!(f, "{e}"),
Self::Url(e) => write!(f, "{e}"),
Self::EventId(e) => write!(f, "{e}"),
Self::NIP26(e) => write!(f, "{e}"),
Self::Event(e) => write!(f, "{e}"),
}
}
}

impl From<ParseIntError> for Error {
fn from(e: ParseIntError) -> Self {
Self::ParseIntError(e)
}
}

impl From<secp256k1::Error> for Error {
fn from(e: secp256k1::Error) -> Self {
Self::Secp256k1(e)
}
}

impl From<bitcoin_hashes::hex::Error> for Error {
fn from(e: bitcoin_hashes::hex::Error) -> Self {
Self::Hex(e)
}
}

impl From<url::ParseError> for Error {
fn from(e: url::ParseError) -> Self {
Self::Url(e)
}
}

impl From<id::Error> for Error {
fn from(e: id::Error) -> Self {
Self::EventId(e)
}
}

impl From<Nip26Error> for Error {
fn from(e: Nip26Error) -> Self {
Self::NIP26(e)
}
}

impl From<crate::event::Error> for Error {
fn from(e: crate::event::Error) -> Self {
Self::Event(e)
}
}

/// Marker
Expand Down
Loading

0 comments on commit dd15e2f

Please sign in to comment.