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