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