#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Id26([u8; 26]);
impl Id26 {
pub fn from_str(s: &str) -> Option<Self> {
let b = s.as_bytes();
if b.len() != 26
|| !b
.iter()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit())
{
return None;
}
let mut arr = [0u8; 26];
arr.copy_from_slice(b);
Some(Self(arr))
}
pub fn as_str(&self) -> &str {
unsafe { std::str::from_utf8_unchecked(&self.0) }
}
pub fn as_bytes(&self) -> &[u8; 26] {
&self.0
}
pub fn hash_u64(&self) -> u64 {
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
for &b in &self.0 {
h ^= b as u64;
h = h.wrapping_mul(0x0000_0100_0000_01b3);
}
h
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ChannelId(pub Id26);
impl ChannelId {
pub fn from_str(s: &str) -> Option<Self> {
Id26::from_str(s).map(Self)
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
pub fn as_bytes(&self) -> &[u8; 26] {
self.0.as_bytes()
}
pub fn hash_u64(&self) -> u64 {
self.0.hash_u64()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ServerId(pub Id26);
impl ServerId {
pub fn from_str(s: &str) -> Option<Self> {
Id26::from_str(s).map(Self)
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
pub fn is_canonical_post_id(s: &str) -> bool {
matches!(s.len(), 24 | 26)
&& s.as_bytes()
.iter()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit())
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TemporaryId(pub String);
impl TemporaryId {
pub(crate) fn mint(now_ms: u64, sequence: u64) -> Self {
Self(format!("helix_tmp_{now_ms:016x}_{sequence:016x}"))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Seq(pub u64);
impl Seq {
pub fn next(self) -> Self {
Self(self.0 + 1)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SendStatus {
Local,
Sending,
Sent,
UnSend,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InflightSync(pub helix_core::Correlation);
#[derive(Debug, Clone, Copy)]
pub struct Cursor {
pub value: Seq,
}
impl Cursor {
pub fn new(value: Seq) -> Self {
Self { value }
}
pub fn try_advance(&mut self, next: Seq) -> bool {
if next > self.value {
self.value = next;
true
} else {
false
}
}
pub fn value(&self) -> Seq {
self.value
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConnState {
Disconnected,
Connecting,
Connected,
Closing,
}