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 } => {
43                write!(f, "lock state table is full: capacity={capacity}")
44            }
45            Self::IdExhausted => f.write_str("lock id counter is exhausted"),
46            Self::TtlZero => f.write_str("lock TTL must be greater than zero"),
47            Self::TtlOverflow => f.write_str("lock TTL deadline overflowed"),
48            Self::Io(error) => write!(f, "orbit lock io error: {error}"),
49        }
50    }
51}
52
53impl std::error::Error for Error {
54    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
55        match self {
56            Self::Io(error) => Some(error),
57            _ => None,
58        }
59    }
60}
61
62impl From<std::io::Error> for Error {
63    fn from(value: std::io::Error) -> Self {
64        Self::Io(value)
65    }
66}