1use std::fmt;
7
8pub use sew_proto::e2ee::names::{CallId as RoomName, JoinIdHex as JoinId, LkIdentity};
9pub use sew_proto::e2ee::{CallScope, ConsentState as Consent};
10pub use sew_proto::id::Id;
11
12pub type MemberId = Id;
14pub type RecordingId = Id;
16pub type Epoch = u64;
18pub type Millis = u64;
21pub type UnixMs = u64;
23
24pub const KEY_RING_SIZE: u8 = sew_proto::e2ee::tls::KEY_RING_SIZE;
26
27#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
29pub struct Slot(u8);
30
31impl Slot {
32 pub const fn new(v: u8) -> Option<Slot> {
34 if v < KEY_RING_SIZE {
35 Some(Slot(v))
36 } else {
37 None
38 }
39 }
40
41 pub fn of(slot_base: u8, epoch: Epoch) -> Slot {
43 Slot(sew_proto::e2ee::tls::slot(slot_base, epoch, KEY_RING_SIZE))
44 }
45
46 pub const fn get(self) -> u8 {
48 self.0
49 }
50
51 pub fn as_i32(self) -> i32 {
53 i32::from(self.0)
54 }
55
56 pub fn next(self) -> Slot {
58 Slot((self.0 + 1) % KEY_RING_SIZE)
59 }
60}
61
62impl fmt::Debug for Slot {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 write!(f, "Slot({})", self.0)
65 }
66}
67
68impl fmt::Display for Slot {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 write!(f, "{}", self.0)
71 }
72}
73
74macro_rules! counter_id {
75 ($(#[$m:meta])* $name:ident) => {
76 $(#[$m])*
77 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
78 pub struct $name(pub u64);
79
80 impl fmt::Display for $name {
81 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82 write!(f, "{}", self.0)
83 }
84 }
85 };
86}
87
88counter_id!(
89 FenceId
91);
92counter_id!(
93 SignId
96);
97counter_id!(
98 RequestId
100);
101
102pub fn lk_identity_for(join_id: &JoinId) -> LkIdentity {
104 join_id.lk_identity()
105}
106
107pub fn identity_matches(join_id: &JoinId, id: &LkIdentity) -> bool {
109 &join_id.lk_identity() == id
110}
111
112pub fn install_id_bytes(install_id: u64) -> Vec<u8> {
114 install_id.to_string().into_bytes()
115}
116
117#[cfg(test)]
118mod tests {
119 use super::*;
120
121 #[test]
122 fn slot_formula_wraps() {
123 assert_eq!(Slot::of(60, 0).get(), 60);
124 assert_eq!(Slot::of(60, 4).get(), 0);
125 assert_eq!(Slot::of(0, 64).get(), 0);
126 assert_eq!(Slot::of(63, 1), Slot::new(0).unwrap_or(Slot(0)));
127 assert!(Slot::new(64).is_none());
128 assert_eq!(Slot(63).next().get(), 0);
129 }
130}