1use std::fmt;
2use std::str::FromStr;
3
4#[cfg(feature = "bincode")]
5pub mod bincode;
6pub mod event;
7
8#[cfg(feature = "rand")]
9pub mod rand;
10
11pub mod id;
16
17pub mod macros;
18pub use crate::event::Event;
19pub use crate::id::ExternalEventId;
20
21array_type_define_public!(
22 struct EventId, 32
23);
24array_type_impl_base32_str!(EventId);
25
26impl From<blake3::Hash> for EventId {
27 fn from(value: blake3::Hash) -> Self {
28 Self(value.as_bytes()[..32].try_into().expect("Must be 32 bytes"))
29 }
30}
31
32impl From<EventId> for [u8; 32] {
33 fn from(value: EventId) -> Self {
34 value.0
35 }
36}
37
38impl From<EventId> for ShortEventId {
39 fn from(value: EventId) -> Self {
40 Self(value.0[..16].try_into().expect("Must be 16 bytes"))
41 }
42}
43
44array_type_define_public!(
45 struct ShortEventId, 16
55);
56array_type_impl_serde!(struct ShortEventId, 16);
57array_type_impl_base32_str!(ShortEventId);
58array_type_impl_zero_default!(ShortEventId, 16);
59
60array_type_define_public!(
61 struct ContentHash, 32
65);
66array_type_impl_serde!(struct ContentHash, 32);
67array_type_impl_base32_str!(ContentHash);
68
69impl From<blake3::Hash> for ContentHash {
70 fn from(value: blake3::Hash) -> Self {
71 Self(value.as_bytes()[..32].try_into().expect("Must be 32 bytes"))
72 }
73}
74
75impl From<ContentHash> for [u8; 32] {
76 fn from(value: ContentHash) -> Self {
77 value.0
78 }
79}
80
81impl From<ContentHash> for blake3::Hash {
82 fn from(value: ContentHash) -> Self {
83 blake3::Hash::from_bytes(value.0)
84 }
85}
86
87#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
92#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
93pub struct MsgLen(pub u32);
94
95impl From<u32> for MsgLen {
96 fn from(value: u32) -> Self {
97 Self(value)
98 }
99}
100
101impl From<MsgLen> for u32 {
102 fn from(value: MsgLen) -> Self {
103 value.0
104 }
105}
106
107#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
109#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
110pub struct TimestampFixed(u64);
111
112impl TimestampFixed {
113 pub const fn as_u64(self) -> u64 {
115 self.0
116 }
117}
118
119impl From<u64> for TimestampFixed {
120 fn from(value: u64) -> Self {
121 Self(value)
122 }
123}
124
125impl From<TimestampFixed> for u64 {
126 fn from(value: TimestampFixed) -> Self {
127 value.0
128 }
129}
130
131impl From<Timestamp> for TimestampFixed {
132 fn from(value: Timestamp) -> Self {
133 Self(value.0)
134 }
135}
136
137#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
139#[cfg_attr(feature = "bincode", derive(::bincode::Encode, ::bincode::Decode))]
140#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
141pub struct Timestamp(u64);
142
143impl Timestamp {
144 pub const ZERO: Self = Self(0);
145 pub const MAX: Self = Self(u64::MAX);
146
147 pub fn now() -> Self {
149 Self(time::OffsetDateTime::now_utc().unix_timestamp() as u64)
150 }
151
152 pub const fn as_u64(self) -> u64 {
154 self.0
155 }
156
157 pub const fn saturating_add_secs(self, secs: u64) -> Self {
159 Self(self.0.saturating_add(secs))
160 }
161
162 pub const fn secs_since(self, earlier: Self) -> u64 {
164 self.0.saturating_sub(earlier.0)
165 }
166
167 pub fn to_offset_date_time(self) -> Option<time::OffsetDateTime> {
169 time::OffsetDateTime::from_unix_timestamp(self.0 as i64).ok()
170 }
171}
172
173impl From<u64> for Timestamp {
174 fn from(value: u64) -> Self {
175 Self(value)
176 }
177}
178
179impl From<Timestamp> for u64 {
180 fn from(value: Timestamp) -> Self {
181 value.0
182 }
183}
184
185impl From<TimestampFixed> for Timestamp {
186 fn from(value: TimestampFixed) -> Self {
187 Self(value.0)
188 }
189}
190
191impl FromStr for Timestamp {
192 type Err = <u64 as FromStr>::Err;
193
194 fn from_str(s: &str) -> Result<Self, Self::Err> {
195 Ok(Self(u64::from_str(s)?))
196 }
197}
198
199impl fmt::Display for Timestamp {
200 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
201 self.0.fmt(f)
202 }
203}
204
205#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
206#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
207pub struct NullableShortEventId(Option<ShortEventId>);
208
209impl From<ShortEventId> for NullableShortEventId {
210 fn from(value: ShortEventId) -> Self {
211 if value == ShortEventId::ZERO {
212 Self(None)
213 } else {
214 Self(Some(value))
215 }
216 }
217}
218
219impl From<NullableShortEventId> for Option<ShortEventId> {
220 fn from(value: NullableShortEventId) -> Self {
221 value.0
222 }
223}
224
225impl fmt::Display for NullableShortEventId {
226 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
227 if let Some(id) = self.0 {
228 id.fmt(f)?;
229 } else {
230 f.write_str("none")?;
231 }
232 Ok(())
233 }
234}