Skip to content

Commit dd15e2f

Browse files
committed
[nostr] Remove thiserror dep
1 parent 2b50507 commit dd15e2f

20 files changed

+763
-212
lines changed

crates/nostr/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ reqwest = { version = "0.11", default-features = false, features = ["json", "rus
3939
secp256k1 = { version = "0.27", features = ["global-context", "rand-std", "serde"] }
4040
serde = { version = "1.0", features = ["derive"] }
4141
serde_json = { version = "1.0" }
42-
thiserror = "1.0"
4342
url = { version = "2", features = ["serde"] }
4443

4544
[target.'cfg(target_arch = "wasm32")'.dependencies]

crates/nostr/src/event/builder.rs

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
//! Event builder
55
6+
use core::fmt;
67
#[cfg(not(target_arch = "wasm32"))]
78
use std::time::Instant;
89

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

2627
/// [`EventBuilder`] error
27-
#[derive(Debug, thiserror::Error)]
28+
#[derive(Debug)]
2829
pub enum Error {
2930
/// Key error
30-
#[error(transparent)]
31-
Key(#[from] key::Error),
32-
/// Secp256k1 error
33-
#[error(transparent)]
34-
Secp256k1(#[from] secp256k1::Error),
31+
Key(key::Error),
3532
/// JSON error
36-
#[error(transparent)]
37-
Json(#[from] serde_json::Error),
33+
Json(serde_json::Error),
34+
/// Secp256k1 error
35+
Secp256k1(secp256k1::Error),
3836
/// Unsigned event error
39-
#[error(transparent)]
40-
Unsigned(#[from] super::unsigned::Error),
37+
Unsigned(super::unsigned::Error),
4138
/// NIP04 error
4239
#[cfg(feature = "nip04")]
43-
#[error(transparent)]
44-
NIP04(#[from] nip04::Error),
40+
NIP04(nip04::Error),
41+
}
42+
43+
impl std::error::Error for Error {}
44+
45+
impl fmt::Display for Error {
46+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47+
match self {
48+
Self::Key(e) => write!(f, "{e}"),
49+
Self::Json(e) => write!(f, "{e}"),
50+
Self::Secp256k1(e) => write!(f, "{e}"),
51+
Self::Unsigned(e) => write!(f, "{e}"),
52+
#[cfg(feature = "nip04")]
53+
Self::NIP04(e) => write!(f, "{e}"),
54+
}
55+
}
56+
}
57+
58+
impl From<key::Error> for Error {
59+
fn from(e: key::Error) -> Self {
60+
Self::Key(e)
61+
}
62+
}
63+
64+
impl From<serde_json::Error> for Error {
65+
fn from(e: serde_json::Error) -> Self {
66+
Self::Json(e)
67+
}
68+
}
69+
70+
impl From<secp256k1::Error> for Error {
71+
fn from(e: secp256k1::Error) -> Self {
72+
Self::Secp256k1(e)
73+
}
74+
}
75+
76+
impl From<super::unsigned::Error> for Error {
77+
fn from(e: super::unsigned::Error) -> Self {
78+
Self::Unsigned(e)
79+
}
80+
}
81+
82+
#[cfg(feature = "nip04")]
83+
impl From<nip04::Error> for Error {
84+
fn from(e: nip04::Error) -> Self {
85+
Self::NIP04(e)
86+
}
4587
}
4688

4789
/// [`Event`] builder

crates/nostr/src/event/id.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
//! Event Id
55
6-
use std::fmt;
7-
use std::str::FromStr;
6+
use core::fmt;
7+
use core::str::FromStr;
88

99
use bitcoin_hashes::sha256::Hash as Sha256Hash;
1010
use bitcoin_hashes::Hash;
@@ -16,14 +16,35 @@ use super::{Kind, Tag};
1616
use crate::Timestamp;
1717

1818
/// [`EventId`] error
19-
#[derive(Debug, PartialEq, Eq, thiserror::Error)]
19+
#[derive(Debug, PartialEq, Eq)]
2020
pub enum Error {
2121
/// Hex error
22-
#[error(transparent)]
23-
Hex(#[from] bitcoin_hashes::hex::Error),
22+
Hex(bitcoin_hashes::hex::Error),
2423
/// Hash error
25-
#[error(transparent)]
26-
Hash(#[from] bitcoin_hashes::Error),
24+
Hash(bitcoin_hashes::Error),
25+
}
26+
27+
impl std::error::Error for Error {}
28+
29+
impl fmt::Display for Error {
30+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31+
match self {
32+
Self::Hex(e) => write!(f, "{e}"),
33+
Self::Hash(e) => write!(f, "{e}"),
34+
}
35+
}
36+
}
37+
38+
impl From<bitcoin_hashes::hex::Error> for Error {
39+
fn from(e: bitcoin_hashes::hex::Error) -> Self {
40+
Self::Hex(e)
41+
}
42+
}
43+
44+
impl From<bitcoin_hashes::Error> for Error {
45+
fn from(e: bitcoin_hashes::Error) -> Self {
46+
Self::Hash(e)
47+
}
2748
}
2849

2950
/// Event Id

crates/nostr/src/event/mod.rs

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
//! Event
66
7-
use std::str::FromStr;
7+
use core::fmt;
8+
use core::str::FromStr;
89

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

2728
/// [`Event`] error
28-
#[derive(Debug, thiserror::Error)]
29+
#[derive(Debug)]
2930
pub enum Error {
3031
/// Invalid signature
31-
#[error("invalid signature")]
3232
InvalidSignature,
3333
/// Error serializing or deserializing JSON data
34-
#[error(transparent)]
35-
Json(#[from] serde_json::Error),
34+
Json(serde_json::Error),
3635
/// Secp256k1 error
37-
#[error(transparent)]
38-
Secp256k1(#[from] secp256k1::Error),
36+
Secp256k1(secp256k1::Error),
3937
/// Hex decoding error
40-
#[error(transparent)]
41-
Hex(#[from] bitcoin_hashes::hex::Error),
38+
Hex(bitcoin_hashes::hex::Error),
4239
/// OpenTimestamps error
4340
#[cfg(feature = "nip03")]
44-
#[error(transparent)]
45-
OpenTimestamps(#[from] nostr_ots::Error),
41+
OpenTimestamps(nostr_ots::Error),
42+
}
43+
44+
impl std::error::Error for Error {}
45+
46+
impl fmt::Display for Error {
47+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48+
match self {
49+
Self::InvalidSignature => write!(f, "Invalid signature"),
50+
Self::Json(e) => write!(f, "{e}"),
51+
Self::Secp256k1(e) => write!(f, "{e}"),
52+
Self::Hex(e) => write!(f, "{e}"),
53+
#[cfg(feature = "nip03")]
54+
Self::OpenTimestamps(e) => write!(f, "{e}"),
55+
}
56+
}
57+
}
58+
59+
impl From<serde_json::Error> for Error {
60+
fn from(e: serde_json::Error) -> Self {
61+
Self::Json(e)
62+
}
63+
}
64+
65+
impl From<secp256k1::Error> for Error {
66+
fn from(e: secp256k1::Error) -> Self {
67+
Self::Secp256k1(e)
68+
}
69+
}
70+
71+
impl From<bitcoin_hashes::hex::Error> for Error {
72+
fn from(e: bitcoin_hashes::hex::Error) -> Self {
73+
Self::Hex(e)
74+
}
75+
}
76+
77+
#[cfg(feature = "nip03")]
78+
impl From<nostr_ots::Error> for Error {
79+
fn from(e: nostr_ots::Error) -> Self {
80+
Self::OpenTimestamps(e)
81+
}
4682
}
4783

4884
/// [`Event`] struct

crates/nostr/src/event/tag.rs

Lines changed: 77 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
//! Tag
55
6-
use std::fmt;
7-
use std::num::ParseIntError;
8-
use std::str::FromStr;
6+
use core::fmt;
7+
use core::num::ParseIntError;
8+
use core::str::FromStr;
99

1010
use secp256k1::schnorr::Signature;
1111
use secp256k1::XOnlyPublicKey;
@@ -14,48 +14,99 @@ use serde::ser::SerializeSeq;
1414
use serde::{Deserialize, Deserializer, Serialize, Serializer};
1515

1616
use super::id::{self, EventId};
17-
use crate::nips::nip26::Conditions;
17+
use crate::nips::nip26::{Conditions, Error as Nip26Error};
1818
use crate::{Kind, Timestamp, UncheckedUrl};
1919

2020
/// [`Tag`] error
21-
#[derive(Debug, thiserror::Error)]
21+
#[derive(Debug)]
2222
pub enum Error {
2323
/// Impossible to parse [`Marker`]
24-
#[error("impossible to parse marker")]
2524
MarkerParseError,
2625
/// Unknown [`Report`]
27-
#[error("unknown report type")]
2826
UnknownReportType,
2927
/// Impossible to find tag kind
30-
#[error("impossible to find tag kind")]
3128
KindNotFound,
3229
/// Invalid length
33-
#[error("invalid length")]
3430
InvalidLength,
31+
/// Invalid Zap Request
32+
InvalidZapRequest,
3533
/// Impossible to parse integer
36-
#[error(transparent)]
37-
ParseIntError(#[from] ParseIntError),
34+
ParseIntError(ParseIntError),
3835
/// Secp256k1
39-
#[error(transparent)]
40-
Secp256k1(#[from] secp256k1::Error),
36+
Secp256k1(secp256k1::Error),
4137
/// Hex decoding error
42-
#[error(transparent)]
43-
Hex(#[from] bitcoin_hashes::hex::Error),
38+
Hex(bitcoin_hashes::hex::Error),
4439
/// Url parse error
45-
#[error("invalid url: {0}")]
46-
Url(#[from] url::ParseError),
40+
Url(url::ParseError),
4741
/// EventId error
48-
#[error(transparent)]
49-
EventId(#[from] id::Error),
42+
EventId(id::Error),
5043
/// NIP26 error
51-
#[error(transparent)]
52-
Nip26(#[from] crate::nips::nip26::Error),
44+
NIP26(Nip26Error),
5345
/// Event Error
54-
#[error(transparent)]
55-
Event(#[from] crate::event::Error),
56-
/// Invalid Zap Request
57-
#[error("Invalid Zap request")]
58-
InvalidZapRequest,
46+
Event(crate::event::Error),
47+
}
48+
49+
impl std::error::Error for Error {}
50+
51+
impl fmt::Display for Error {
52+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53+
match self {
54+
Self::MarkerParseError => write!(f, "impossible to parse marker"),
55+
Self::UnknownReportType => write!(f, "unknown report type"),
56+
Self::KindNotFound => write!(f, "impossible to find tag kind"),
57+
Self::InvalidLength => write!(f, "invalid length"),
58+
Self::InvalidZapRequest => write!(f, "invalid Zap request"),
59+
Self::ParseIntError(e) => write!(f, "{e}"),
60+
Self::Secp256k1(e) => write!(f, "{e}"),
61+
Self::Hex(e) => write!(f, "{e}"),
62+
Self::Url(e) => write!(f, "{e}"),
63+
Self::EventId(e) => write!(f, "{e}"),
64+
Self::NIP26(e) => write!(f, "{e}"),
65+
Self::Event(e) => write!(f, "{e}"),
66+
}
67+
}
68+
}
69+
70+
impl From<ParseIntError> for Error {
71+
fn from(e: ParseIntError) -> Self {
72+
Self::ParseIntError(e)
73+
}
74+
}
75+
76+
impl From<secp256k1::Error> for Error {
77+
fn from(e: secp256k1::Error) -> Self {
78+
Self::Secp256k1(e)
79+
}
80+
}
81+
82+
impl From<bitcoin_hashes::hex::Error> for Error {
83+
fn from(e: bitcoin_hashes::hex::Error) -> Self {
84+
Self::Hex(e)
85+
}
86+
}
87+
88+
impl From<url::ParseError> for Error {
89+
fn from(e: url::ParseError) -> Self {
90+
Self::Url(e)
91+
}
92+
}
93+
94+
impl From<id::Error> for Error {
95+
fn from(e: id::Error) -> Self {
96+
Self::EventId(e)
97+
}
98+
}
99+
100+
impl From<Nip26Error> for Error {
101+
fn from(e: Nip26Error) -> Self {
102+
Self::NIP26(e)
103+
}
104+
}
105+
106+
impl From<crate::event::Error> for Error {
107+
fn from(e: crate::event::Error) -> Self {
108+
Self::Event(e)
109+
}
59110
}
60111

61112
/// Marker

0 commit comments

Comments
 (0)