Skip to main content

orbit_lock/
error.rs

1use std::fmt;
2
3pub type Result<T, E = Error> = std::result::Result<T, E>;
4
5#[derive(Debug)]
6pub enum Error {
7    InvalidLayout(&'static str),
8    NamespaceEmpty,
9    KeyEmpty,
10    OwnerEmpty,
11    EntryTooLarge { namespace_len: usize, key_len: usize, owner_len: usize, max_payload: usize },
12    StateFull { capacity: usize },
13    IdExhausted,
14    TtlZero,
15    TtlOverflow,
16    Io(std::io::Error)
17}
18
19impl fmt::Display for Error {
20    fn fmt(
21        &self,
22        f: &mut fmt::Formatter<'_>
23    ) -> fmt::Result {
24        match self {
25            Self::InvalidLayout(message) => write!(f, "invalid lock layout: {message}"),
26            Self::NamespaceEmpty => f.write_str("lock namespace must not be empty"),
27            Self::KeyEmpty => f.write_str("lock key must not be empty"),
28            Self::OwnerEmpty => f.write_str("lock owner must not be empty"),
29            Self::EntryTooLarge { namespace_len, key_len, owner_len, max_payload } => write!(
30                f,
31                "lock entry is too large: namespace_len={namespace_len} key_len={key_len} owner_len={owner_len} max_payload={max_payload}"
32            ),
33            Self::StateFull { capacity } => write!(
34                f,
35                "lock state table is full: {capacity} leases held and none expired. Every \
36                 acquisition fails until one does, so whatever the caller was locking does not \
37                 happen. Capacity is fixed at compile time — rebuild with \
38                 ORBIT_LOCK_STATE_CAPACITY set to a power of two above {capacity}"
39            ),
40            Self::IdExhausted => f.write_str("lock id counter is exhausted"),
41            Self::TtlZero => f.write_str("lock TTL must be greater than zero"),
42            Self::TtlOverflow => f.write_str("lock TTL deadline overflowed"),
43            Self::Io(error) => write!(f, "orbit lock io error: {error}")
44        }
45    }
46}
47
48impl std::error::Error for Error {
49    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
50        match self {
51            Self::Io(error) => Some(error),
52            _ => None
53        }
54    }
55}
56
57impl From<std::io::Error> for Error {
58    fn from(value: std::io::Error) -> Self {
59        Self::Io(value)
60    }
61}