Skip to content

Commit

Permalink
nostr: cleanup error enums
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Jan 10, 2025
1 parent df94974 commit 1d266a3
Show file tree
Hide file tree
Showing 28 changed files with 248 additions and 430 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
* nostr: bump `bip39` to 2.1 ([Yuki Kishimoto])
* nostr: move `types::filter` to `crate::filter` ([Yuki Kishimoto])
* nostr: move `Metadata` struct to `nip01` module ([Yuki Kishimoto])
* nostr: cleanup error enums ([Yuki Kishimoto])
* pool: update `Error::WebSocket` variant inner type ([Yuki Kishimoto])
* lmdb: use `EventBorrow` instead of `DatabaseEvent` ([Yuki Kishimoto])
* ndb: refactor note-to-event conversion ([Yuki Kishimoto])
Expand Down
17 changes: 7 additions & 10 deletions crates/nostr/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ impl fmt::Display for WrongKindError {
}
}

/// [`EventBuilder`] error
/// Event builder error
#[derive(Debug)]
pub enum Error {
/// Signer error
Signer(SignerError),
/// Unsigned event error
Unsigned(unsigned::Error),
Event(super::Error),
/// OpenTimestamps error
#[cfg(feature = "nip03")]
OpenTimestamps(nostr_ots::Error),
Expand Down Expand Up @@ -75,8 +73,7 @@ impl std::error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Signer(e) => write!(f, "{e}"),
Self::Unsigned(e) => write!(f, "{e}"),
Self::Event(e) => write!(f, "{e}"),
#[cfg(feature = "nip03")]
Self::OpenTimestamps(e) => write!(f, "{e}"),
#[cfg(feature = "nip04")]
Expand All @@ -95,13 +92,13 @@ impl fmt::Display for Error {

impl From<SignerError> for Error {
fn from(e: SignerError) -> Self {
Self::Signer(e)
Self::Event(super::Error::Signer(e.to_string()))
}
}

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

Expand Down
60 changes: 60 additions & 0 deletions crates/nostr/src/event/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use alloc::string::{String, ToString};
use core::fmt;

use crate::signer::SignerError;
use crate::util::hex;

/// Event error
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
/// Error serializing or deserializing JSON data
Json(String),
/// Signer error
Signer(String),
/// Hex decode error
Hex(hex::Error),
/// Unknown JSON event key
UnknownKey(String),
/// Invalid event ID
InvalidId,
/// Invalid signature
InvalidSignature,
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Json(e) => write!(f, "{e}"),
Self::Signer(e) => write!(f, "{e}"),
Self::Hex(e) => write!(f, "{e}"),
Self::UnknownKey(key) => write!(f, "Unknown key: {key}"),
Self::InvalidId => write!(f, "Invalid event ID"),
Self::InvalidSignature => write!(f, "Invalid signature"),
}
}
}

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

impl From<SignerError> for Error {
fn from(e: SignerError) -> Self {
Self::Signer(e.to_string())
}
}

impl From<hex::Error> for Error {
fn from(e: hex::Error) -> Self {
Self::Hex(e)
}
}
32 changes: 3 additions & 29 deletions crates/nostr/src/event/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,14 @@ use hashes::Hash;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::{json, Value};

use super::error::Error;
use super::{Kind, Tag};
use crate::nips::nip13;
use crate::nips::nip19::FromBech32;
use crate::nips::nip21::NostrURI;
use crate::util::hex;
use crate::{PublicKey, Timestamp};

/// [`EventId`] error
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
/// Hex decode error
Hex(hex::Error),
/// Invalid event ID
InvalidEventId,
}

#[cfg(feature = "std")]
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::InvalidEventId => write!(f, "Invalid event ID"),
}
}
}

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

/// Event ID
///
/// 32-bytes lowercase hex-encoded sha256 of the serialized event data
Expand Down Expand Up @@ -108,7 +82,7 @@ impl EventId {
return Ok(id);
}

Err(Error::InvalidEventId)
Err(Error::InvalidId)
}

/// Parse from hex string
Expand All @@ -122,7 +96,7 @@ impl EventId {
pub fn from_slice(slice: &[u8]) -> Result<Self, Error> {
// Check len
if slice.len() != Self::LEN {
return Err(Error::InvalidEventId);
return Err(Error::InvalidId);
}

// Copy bytes
Expand Down
35 changes: 2 additions & 33 deletions crates/nostr/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use serde_json::Value;

pub mod borrow;
pub mod builder;
mod error;
pub mod id;
pub mod kind;
pub mod partial;
Expand All @@ -29,6 +30,7 @@ pub mod tag;
pub mod unsigned;

pub use self::builder::EventBuilder;
pub use self::error::Error;
pub use self::id::EventId;
pub use self::kind::Kind;
pub use self::partial::{MissingPartialEvent, PartialEvent};
Expand All @@ -50,39 +52,6 @@ const TAGS: &str = "tags";
const CONTENT: &str = "content";
const SIG: &str = "sig";

/// [`Event`] error
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
/// Error serializing or deserializing JSON data
Json(String),
/// Unknown JSON event key
UnknownKey(String),
/// Invalid event ID
InvalidId,
/// Invalid signature
InvalidSignature,
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Json(e) => write!(f, "{e}"),
Self::UnknownKey(key) => write!(f, "Unknown JSON event key: {key}"),
Self::InvalidId => write!(f, "Invalid event id"),
Self::InvalidSignature => write!(f, "Invalid signature"),
}
}
}

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

/// Nostr event
#[derive(Clone)]
pub struct Event {
Expand Down
20 changes: 10 additions & 10 deletions crates/nostr/src/event/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ use core::str::FromStr;

use secp256k1::schnorr::Signature;

use super::{id, tag};
use crate::{key, Event, EventId, JsonUtil, Kind, PartialEvent, PublicKey, Tag, Timestamp};
use super::tag;
use crate::{event, key, Event, EventId, JsonUtil, Kind, PartialEvent, PublicKey, Tag, Timestamp};

/// [`RawEvent`] error
#[derive(Debug)]
/// Raw event error
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
/// Secp256k1 error
Secp256k1(secp256k1::Error),
/// EventId error
EventId(id::Error),
/// Event error
Event(event::Error),
/// Keys error
Keys(key::Error),
/// Tag error
Expand All @@ -35,7 +35,7 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Secp256k1(e) => write!(f, "{e}"),
Self::EventId(e) => write!(f, "{e}"),
Self::Event(e) => write!(f, "{e}"),
Self::Keys(e) => write!(f, "{e}"),
Self::Tag(e) => write!(f, "{e}"),
}
Expand All @@ -48,9 +48,9 @@ impl From<secp256k1::Error> for Error {
}
}

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

Expand Down
10 changes: 0 additions & 10 deletions crates/nostr/src/event/tag/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use core::num::ParseIntError;

use hashes::hex::HexToArrayError;

use crate::event::id;
use crate::nips::{nip01, nip10, nip26, nip39, nip53, nip65, nip98};
use crate::types::image;
use crate::types::url::{Error as RelayUrlError, ParseError};
Expand All @@ -28,8 +27,6 @@ pub enum Error {
RelayUrl(RelayUrlError),
/// Url parse error
Url(ParseError),
/// EventId error
EventId(id::Error),
/// NIP01 error
NIP01(nip01::Error),
/// NIP10 error
Expand Down Expand Up @@ -68,7 +65,6 @@ impl fmt::Display for Error {
Self::Hex(e) => write!(f, "{e}"),
Self::RelayUrl(e) => write!(f, "{e}"),
Self::Url(e) => write!(f, "{e}"),
Self::EventId(e) => write!(f, "{e}"),
Self::NIP01(e) => write!(f, "{e}"),
Self::NIP10(e) => write!(f, "{e}"),
Self::NIP26(e) => write!(f, "{e}"),
Expand Down Expand Up @@ -121,12 +117,6 @@ impl From<ParseError> for Error {
}
}

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

impl From<nip01::Error> for Error {
fn from(e: nip01::Error) -> Self {
Self::NIP01(e)
Expand Down
Loading

0 comments on commit 1d266a3

Please sign in to comment.