use std::fmt;
pub use sew_proto::e2ee::names::{CallId as RoomName, JoinIdHex as JoinId, LkIdentity};
pub use sew_proto::e2ee::{CallScope, ConsentState as Consent};
pub use sew_proto::id::Id;
pub type MemberId = Id;
pub type RecordingId = Id;
pub type Epoch = u64;
pub type Millis = u64;
pub type UnixMs = u64;
pub const KEY_RING_SIZE: u8 = sew_proto::e2ee::tls::KEY_RING_SIZE;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Slot(u8);
impl Slot {
pub const fn new(v: u8) -> Option<Slot> {
if v < KEY_RING_SIZE {
Some(Slot(v))
} else {
None
}
}
pub fn of(slot_base: u8, epoch: Epoch) -> Slot {
Slot(sew_proto::e2ee::tls::slot(slot_base, epoch, KEY_RING_SIZE))
}
pub const fn get(self) -> u8 {
self.0
}
pub fn as_i32(self) -> i32 {
i32::from(self.0)
}
pub fn next(self) -> Slot {
Slot((self.0 + 1) % KEY_RING_SIZE)
}
}
impl fmt::Debug for Slot {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Slot({})", self.0)
}
}
impl fmt::Display for Slot {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
macro_rules! counter_id {
($(#[$m:meta])* $name:ident) => {
$(#[$m])*
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $name(pub u64);
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
};
}
counter_id!(
FenceId
);
counter_id!(
SignId
);
counter_id!(
RequestId
);
pub fn lk_identity_for(join_id: &JoinId) -> LkIdentity {
join_id.lk_identity()
}
pub fn identity_matches(join_id: &JoinId, id: &LkIdentity) -> bool {
&join_id.lk_identity() == id
}
pub fn install_id_bytes(install_id: u64) -> Vec<u8> {
install_id.to_string().into_bytes()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn slot_formula_wraps() {
assert_eq!(Slot::of(60, 0).get(), 60);
assert_eq!(Slot::of(60, 4).get(), 0);
assert_eq!(Slot::of(0, 64).get(), 0);
assert_eq!(Slot::of(63, 1), Slot::new(0).unwrap_or(Slot(0)));
assert!(Slot::new(64).is_none());
assert_eq!(Slot(63).next().get(), 0);
}
}