Skip to content

Commit 0c765f8

Browse files
committed
nostr: impl PartialOrd, Ord and Hash for missing structs and enums
1 parent ee22b20 commit 0c765f8

File tree

18 files changed

+50
-37
lines changed

18 files changed

+50
-37
lines changed

crates/nostr/src/event/kind.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33

44
//! Kind
55
6-
use std::fmt;
7-
use std::num::ParseIntError;
8-
use std::str::FromStr;
6+
use core::fmt;
7+
use core::hash::{Hash, Hasher};
8+
use core::num::ParseIntError;
9+
use core::str::FromStr;
910

1011
use serde::de::{Deserialize, Deserializer, Error, Visitor};
1112
use serde::ser::{Serialize, Serializer};
1213

1314
/// Event [`Kind`]
14-
#[derive(Debug, Copy, Clone, Eq, Ord, PartialOrd)]
15+
#[derive(Debug, Clone, Copy, Eq, PartialOrd, Ord)]
1516
pub enum Kind {
1617
/// Metadata (NIP01 and NIP05)
1718
Metadata,
@@ -207,6 +208,15 @@ impl PartialEq<Kind> for Kind {
207208
}
208209
}
209210

211+
impl Hash for Kind {
212+
fn hash<H>(&self, state: &mut H)
213+
where
214+
H: Hasher,
215+
{
216+
self.as_u64().hash(state);
217+
}
218+
}
219+
210220
impl Serialize for Kind {
211221
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
212222
where

crates/nostr/src/event/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl From<nostr_ots::Error> for Error {
8282
}
8383

8484
/// [`Event`] struct
85-
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
85+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
8686
pub struct Event {
8787
/// Id
8888
pub id: EventId,

crates/nostr/src/event/tag.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl From<crate::event::Error> for Error {
110110
}
111111

112112
/// Marker
113-
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
113+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
114114
pub enum Marker {
115115
/// Root
116116
Root,
@@ -145,7 +145,7 @@ where
145145
}
146146

147147
/// Report
148-
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
148+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
149149
pub enum Report {
150150
/// Depictions of nudity, porn, etc
151151
Nudity,
@@ -188,7 +188,7 @@ impl TryFrom<&str> for Report {
188188
}
189189

190190
/// Tag kind
191-
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
191+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
192192
pub enum TagKind {
193193
/// Public key
194194
P,
@@ -319,7 +319,7 @@ where
319319
}
320320

321321
#[allow(missing_docs)]
322-
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord)]
322+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
323323
pub enum Tag {
324324
Generic(TagKind, Vec<String>),
325325
Event(EventId, Option<UncheckedUrl>, Option<Marker>),
@@ -499,7 +499,7 @@ where
499499
TagKind::P => {
500500
let pubkey = XOnlyPublicKey::from_str(&tag[1])?;
501501
if tag[2].is_empty() {
502-
Ok(Self::PubKey(pubkey, Some(UncheckedUrl::default())))
502+
Ok(Self::PubKey(pubkey, Some(UncheckedUrl::empty())))
503503
} else {
504504
match Report::try_from(tag[2].as_str()) {
505505
Ok(report) => Ok(Self::PubKeyReport(pubkey, report)),
@@ -513,7 +513,7 @@ where
513513
TagKind::E => {
514514
let event_id = EventId::from_hex(&tag[1])?;
515515
if tag[2].is_empty() {
516-
Ok(Self::Event(event_id, Some(UncheckedUrl::default()), None))
516+
Ok(Self::Event(event_id, Some(UncheckedUrl::empty()), None))
517517
} else {
518518
match Report::try_from(tag[2].as_str()) {
519519
Ok(report) => Ok(Self::EventReport(event_id, report)),
@@ -895,7 +895,7 @@ mod tests {
895895
EventId::from_hex(
896896
"378f145897eea948952674269945e88612420db35791784abf0616b4fed56ef7"
897897
)?,
898-
Some(UncheckedUrl::default()),
898+
Some(UncheckedUrl::empty()),
899899
None
900900
)
901901
.as_vec()
@@ -1121,7 +1121,7 @@ mod tests {
11211121
EventId::from_hex(
11221122
"378f145897eea948952674269945e88612420db35791784abf0616b4fed56ef7"
11231123
)?,
1124-
Some(UncheckedUrl::default()),
1124+
Some(UncheckedUrl::empty()),
11251125
None
11261126
)
11271127
);

crates/nostr/src/event/unsigned.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl From<super::Error> for Error {
6262
}
6363

6464
/// [`UnsignedEvent`] struct
65-
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
65+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
6666
pub struct UnsignedEvent {
6767
/// Id
6868
pub id: EventId,

crates/nostr/src/key/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub trait FromPkStr: Sized {
7676
}
7777

7878
/// Keys
79-
#[derive(Debug, Clone, Eq, PartialEq)]
79+
#[derive(Debug, Clone, PartialEq, Eq)]
8080
pub struct Keys {
8181
public_key: XOnlyPublicKey,
8282
key_pair: Option<KeyPair>,

crates/nostr/src/message/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use super::{Filter, MessageHandleError, SubscriptionId};
1212
use crate::Event;
1313

1414
/// Messages sent by clients, received by relays
15-
#[derive(Debug, Clone, Eq, PartialEq)]
15+
#[derive(Debug, Clone, PartialEq, Eq)]
1616
pub enum ClientMessage {
1717
/// Event
1818
Event(Box<Event>),

crates/nostr/src/message/relay.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{Event, EventId, SubscriptionId};
1313

1414
/// Messages sent by relays, received by clients
1515
#[allow(missing_docs)]
16-
#[derive(Debug, Clone, Eq, PartialEq)]
16+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1717
pub enum RelayMessage {
1818
Event {
1919
subscription_id: SubscriptionId,

crates/nostr/src/message/subscription.rs

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

55
//! Subscription filters
66
7-
use std::fmt;
7+
use core::fmt;
88

99
use bitcoin_hashes::sha256::Hash as Sha256Hash;
1010
use bitcoin_hashes::Hash;
@@ -19,7 +19,7 @@ use serde_json::{json, Map, Value};
1919
use crate::{EventId, Kind, Timestamp};
2020

2121
/// Subscription ID
22-
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
22+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
2323
pub struct SubscriptionId(String);
2424

2525
impl SubscriptionId {
@@ -47,7 +47,7 @@ impl fmt::Display for SubscriptionId {
4747
}
4848

4949
/// Subscription filters
50-
#[derive(Debug, Clone, Eq, PartialEq)]
50+
#[derive(Debug, Clone, PartialEq, Eq)]
5151
pub struct Filter {
5252
/// List of event ids or prefixes
5353
pub ids: Option<Vec<String>>,

crates/nostr/src/nips/nip11.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl From<reqwest::Error> for Error {
5252
}
5353

5454
/// Relay information document
55-
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
55+
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
5656
pub struct RelayInformationDocument {
5757
/// Name
5858
pub name: Option<String>,

crates/nostr/src/nips/nip19.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl ToBech32 for EventId {
205205
}
206206
}
207207

208-
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
208+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
209209
pub struct Nip19Event {
210210
pub event_id: EventId,
211211
pub relays: Vec<String>,
@@ -286,7 +286,7 @@ impl ToBech32 for Nip19Event {
286286
}
287287
}
288288

289-
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
289+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
290290
pub struct ParameterizedReplaceableEvent {
291291
pub kind: Kind,
292292
pub pubkey: XOnlyPublicKey,

0 commit comments

Comments
 (0)