Skip to content

Commit 9044094

Browse files
committed
nostr: addis_regular, is_replaceable, is_ephemeral and is_parameterized_replaceable methods to Kind and Event
1 parent f2b2e05 commit 9044094

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

crates/nostr/src/event/kind.rs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@
66
use core::fmt;
77
use core::hash::{Hash, Hasher};
88
use core::num::ParseIntError;
9+
use core::ops::Range;
910
use core::str::FromStr;
1011

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

15+
/// Regular range
16+
pub const REGULAR_RANGE: Range<u64> = 1_000..10_000;
17+
/// Replaceable range
18+
pub const REPLACEABLE_RANGE: Range<u64> = 10_000..20_000;
19+
/// Ephemeral range
20+
pub const EPHEMERAL_RANGE: Range<u64> = 20_000..30_000;
21+
/// Parameterized replaceable range
22+
pub const PARAMETERIZED_REPLACEABLE_RANGE: Range<u64> = 30_000..40_000;
23+
1424
/// Event [`Kind`]
1525
#[derive(Debug, Clone, Copy, Eq, PartialOrd, Ord)]
1626
pub enum Kind {
@@ -96,11 +106,11 @@ pub enum Kind {
96106
HttpAuth,
97107
/// Regular Events (must be between 1000 and <=9999)
98108
Regular(u16),
99-
/// Replacabe event (must be between 10000 and <20000)
109+
/// Replaceable event (must be between 10000 and <20000)
100110
Replaceable(u16),
101111
/// Ephemeral event (must be between 20000 and <30000)
102112
Ephemeral(u16),
103-
/// Parameterized Replacabe event (must be between 30000 and <40000)
113+
/// Parameterized replaceable event (must be between 30000 and <40000)
104114
ParameterizedReplaceable(u16),
105115
/// Custom
106116
Custom(u64),
@@ -116,6 +126,26 @@ impl Kind {
116126
pub fn as_u64(&self) -> u64 {
117127
(*self).into()
118128
}
129+
130+
/// Check if [`Kind`] is `Regular`
131+
pub fn is_regular(&self) -> bool {
132+
REGULAR_RANGE.contains(&self.as_u64())
133+
}
134+
135+
/// Check if [`Kind`] is `Replaceable`
136+
pub fn is_replaceable(&self) -> bool {
137+
REPLACEABLE_RANGE.contains(&self.as_u64())
138+
}
139+
140+
/// Check if [`Kind`] is `Ephemeral`
141+
pub fn is_ephemeral(&self) -> bool {
142+
EPHEMERAL_RANGE.contains(&self.as_u64())
143+
}
144+
145+
/// Check if [`Kind`] is `Parameterized replaceable`
146+
pub fn is_parameterized_replaceable(&self) -> bool {
147+
PARAMETERIZED_REPLACEABLE_RANGE.contains(&self.as_u64())
148+
}
119149
}
120150

121151
impl fmt::Display for Kind {
@@ -167,10 +197,12 @@ impl From<u64> for Kind {
167197
30078 => Self::ApplicationSpecificData,
168198
1063 => Self::FileMetadata,
169199
27235 => Self::HttpAuth,
170-
x if (1_000..10_000).contains(&x) => Self::Regular(x as u16),
171-
x if (10_000..20_000).contains(&x) => Self::Replaceable(x as u16),
172-
x if (20_000..30_000).contains(&x) => Self::Ephemeral(x as u16),
173-
x if (30_000..40_000).contains(&x) => Self::ParameterizedReplaceable(x as u16),
200+
x if (REGULAR_RANGE).contains(&x) => Self::Regular(x as u16),
201+
x if (REPLACEABLE_RANGE).contains(&x) => Self::Replaceable(x as u16),
202+
x if (EPHEMERAL_RANGE).contains(&x) => Self::Ephemeral(x as u16),
203+
x if (PARAMETERIZED_REPLACEABLE_RANGE).contains(&x) => {
204+
Self::ParameterizedReplaceable(x as u16)
205+
}
174206
x => Self::Custom(x),
175207
}
176208
}

crates/nostr/src/event/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,26 @@ impl Event {
213213
self.ots = Some(ots);
214214
Ok(())
215215
}
216+
217+
/// Check if event [`Kind`] is `Regular`
218+
pub fn is_regular(&self) -> bool {
219+
self.kind.is_regular()
220+
}
221+
222+
/// Check if event [`Kind`] is `Replaceable`
223+
pub fn is_replaceable(&self) -> bool {
224+
self.kind.is_replaceable()
225+
}
226+
227+
/// Check if event [`Kind`] is `Ephemeral`
228+
pub fn is_ephemeral(&self) -> bool {
229+
self.kind.is_ephemeral()
230+
}
231+
232+
/// Check if event [`Kind`] is `Parameterized replaceable`
233+
pub fn is_parameterized_replaceable(&self) -> bool {
234+
self.kind.is_parameterized_replaceable()
235+
}
216236
}
217237

218238
impl Event {

0 commit comments

Comments
 (0)