use std::fmt;
use std::str::FromStr;
use std::time::Duration;
use crate::error::{Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct EntryId {
ms: u64,
seq: u64,
}
impl EntryId {
pub const ZERO: Self = Self { ms: 0, seq: 0 };
#[must_use]
pub const fn new(ms: u64, seq: u64) -> Self {
Self { ms, seq }
}
#[must_use]
pub const fn ms(&self) -> u64 {
self.ms
}
#[must_use]
pub const fn seq(&self) -> u64 {
self.seq
}
}
impl fmt::Display for EntryId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}-{}", self.ms, self.seq)
}
}
impl FromStr for EntryId {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
let invalid = || Error::InvalidEntryId(s.chars().take(64).collect());
let (ms, seq) = s.split_once('-').ok_or_else(invalid)?;
let digits = |part: &str| !part.is_empty() && part.bytes().all(|b| b.is_ascii_digit());
if !digits(ms) || !digits(seq) {
return Err(invalid());
}
Ok(Self {
ms: ms.parse().map_err(|_| invalid())?,
seq: seq.parse().map_err(|_| invalid())?,
})
}
}
pub type RawFields = Vec<(Vec<u8>, Vec<u8>)>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Entry {
pub id: EntryId,
pub fields: RawFields,
}
impl Entry {
#[must_use]
pub const fn new(id: EntryId, fields: RawFields) -> Self {
Self { id, fields }
}
#[must_use]
pub fn field(&self, name: &str) -> Option<&[u8]> {
self.fields
.iter()
.find(|(key, _)| key == name.as_bytes())
.map(|(_, value)| value.as_slice())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Pending {
pub id: EntryId,
pub consumer: String,
pub idle: Duration,
pub deliveries: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct AutoClaim {
pub next: EntryId,
pub claimed: Vec<Entry>,
pub deleted: Vec<EntryId>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct GroupBacklog {
pub pending: u64,
pub lag: Option<u64>,
pub length: Option<u64>,
}
impl GroupBacklog {
#[must_use]
pub const fn new(pending: u64, lag: Option<u64>) -> Self {
Self {
pending,
lag,
length: None,
}
}
#[must_use]
pub const fn with_length(mut self, length: u64) -> Self {
self.length = Some(length);
self
}
#[must_use]
pub const fn waiting(&self) -> Option<Waiting> {
match (self.lag, self.length) {
(Some(lag), _) => Some(Waiting::Exact(lag)),
(None, Some(length)) => Some(Waiting::Estimate(length.saturating_sub(self.pending))),
(None, None) => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Waiting {
Exact(u64),
Estimate(u64),
}
impl Waiting {
#[must_use]
pub const fn count(self) -> u64 {
match self {
Self::Exact(count) | Self::Estimate(count) => count,
}
}
#[must_use]
pub const fn is_exact(self) -> bool {
matches!(self, Self::Exact(_))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Renewal {
Renewed,
Lost(LostLease),
}
impl Renewal {
#[must_use]
pub const fn is_renewed(self) -> bool {
matches!(self, Self::Renewed)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LostLease {
NotPending,
OtherConsumer,
}
impl fmt::Display for LostLease {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::NotPending => "the entry is no longer pending",
Self::OtherConsumer => "another consumer owns the entry",
})
}
}