use std::fmt;
use std::str::FromStr;
#[cfg(feature = "bincode")]
pub mod bincode;
pub mod event;
#[cfg(feature = "rand")]
pub mod rand;
pub mod id;
pub mod macros;
pub use crate::event::Event;
pub use crate::id::ExternalEventId;
array_type_define_public!(
struct EventId, 32
);
array_type_impl_base32_str!(EventId);
impl From<blake3::Hash> for EventId {
fn from(value: blake3::Hash) -> Self {
Self(value.as_bytes()[..32].try_into().expect("Must be 32 bytes"))
}
}
impl From<EventId> for [u8; 32] {
fn from(value: EventId) -> Self {
value.0
}
}
impl From<EventId> for ShortEventId {
fn from(value: EventId) -> Self {
Self(value.0[..16].try_into().expect("Must be 16 bytes"))
}
}
array_type_define_public!(
struct ShortEventId, 16
);
array_type_impl_serde!(struct ShortEventId, 16);
array_type_impl_base32_str!(ShortEventId);
array_type_impl_zero_default!(ShortEventId, 16);
array_type_define_public!(
struct ContentHash, 32
);
array_type_impl_serde!(struct ContentHash, 32);
array_type_impl_base32_str!(ContentHash);
impl From<blake3::Hash> for ContentHash {
fn from(value: blake3::Hash) -> Self {
Self(value.as_bytes()[..32].try_into().expect("Must be 32 bytes"))
}
}
impl From<ContentHash> for [u8; 32] {
fn from(value: ContentHash) -> Self {
value.0
}
}
impl From<ContentHash> for blake3::Hash {
fn from(value: ContentHash) -> Self {
blake3::Hash::from_bytes(value.0)
}
}
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct MsgLen(pub u32);
impl From<u32> for MsgLen {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<MsgLen> for u32 {
fn from(value: MsgLen) -> Self {
value.0
}
}
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TimestampFixed(u64);
impl TimestampFixed {
pub const fn as_u64(self) -> u64 {
self.0
}
}
impl From<u64> for TimestampFixed {
fn from(value: u64) -> Self {
Self(value)
}
}
impl From<TimestampFixed> for u64 {
fn from(value: TimestampFixed) -> Self {
value.0
}
}
impl From<Timestamp> for TimestampFixed {
fn from(value: Timestamp) -> Self {
Self(value.0)
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "bincode", derive(::bincode::Encode, ::bincode::Decode))]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct Timestamp(u64);
impl Timestamp {
pub const ZERO: Self = Self(0);
pub const MAX: Self = Self(u64::MAX);
pub fn now() -> Self {
Self(time::OffsetDateTime::now_utc().unix_timestamp() as u64)
}
pub const fn as_u64(self) -> u64 {
self.0
}
pub const fn saturating_add_secs(self, secs: u64) -> Self {
Self(self.0.saturating_add(secs))
}
pub const fn secs_since(self, earlier: Self) -> u64 {
self.0.saturating_sub(earlier.0)
}
pub fn to_offset_date_time(self) -> Option<time::OffsetDateTime> {
time::OffsetDateTime::from_unix_timestamp(self.0 as i64).ok()
}
}
impl From<u64> for Timestamp {
fn from(value: u64) -> Self {
Self(value)
}
}
impl From<Timestamp> for u64 {
fn from(value: Timestamp) -> Self {
value.0
}
}
impl From<TimestampFixed> for Timestamp {
fn from(value: TimestampFixed) -> Self {
Self(value.0)
}
}
impl FromStr for Timestamp {
type Err = <u64 as FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(u64::from_str(s)?))
}
}
impl fmt::Display for Timestamp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct NullableShortEventId(Option<ShortEventId>);
impl From<ShortEventId> for NullableShortEventId {
fn from(value: ShortEventId) -> Self {
if value == ShortEventId::ZERO {
Self(None)
} else {
Self(Some(value))
}
}
}
impl From<NullableShortEventId> for Option<ShortEventId> {
fn from(value: NullableShortEventId) -> Self {
value.0
}
}
impl fmt::Display for NullableShortEventId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(id) = self.0 {
id.fmt(f)?;
} else {
f.write_str("none")?;
}
Ok(())
}
}