Skip to main content

orbit_atomic/
error.rs

1//! What can fail: allocating, opening and waiting. Value operations cannot.
2
3use std::fmt;
4
5use crate::{Kind, OrbitalId};
6
7pub type Result<T, E = Error> = std::result::Result<T, E>;
8
9/// Additions are expected: a cause discovered later lands here rather than
10/// in a new major version, so a caller matches what it handles and leaves
11/// the rest to a catch-all.
12#[non_exhaustive]
13#[derive(Debug)]
14pub enum Error {
15    /// The id names nothing in this table: it was minted before the table
16    /// was reset, or it was never minted here.
17    Stale(OrbitalId),
18    /// The id names a value of another type than the one asked for.
19    KindMismatch {
20        id: OrbitalId,
21        expected: Kind,
22    },
23    /// The leading byte of an encoded id is not a known kind.
24    UnknownKind(u8),
25    /// The text is not an id.
26    Malformed(Box<str>),
27    /// Every slot of the table has been handed out.
28    Full {
29        capacity: usize,
30    },
31    /// The table cannot start another epoch.
32    EpochExhausted,
33    Io(std::io::Error),
34}
35
36impl fmt::Display for Error {
37    fn fmt(
38        &self,
39        f: &mut fmt::Formatter<'_>,
40    ) -> fmt::Result {
41        match self {
42            Self::Stale(id) => write!(f, "orbital {id} is not in this table"),
43            Self::KindMismatch { id, expected } => {
44                write!(f, "orbital {id} is not {expected}")
45            }
46            Self::UnknownKind(kind) => {
47                write!(f, "unknown orbital kind byte {kind:#04x}")
48            }
49            Self::Malformed(text) => write!(f, "not an orbital id: {text:?}"),
50            Self::Full { capacity } => {
51                write!(f, "orbital table is full: capacity={capacity}")
52            }
53            Self::EpochExhausted => {
54                f.write_str("orbital table epoch is exhausted")
55            }
56            Self::Io(error) => write!(f, "orbital table io error: {error}"),
57        }
58    }
59}
60
61impl std::error::Error for Error {
62    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
63        match self {
64            Self::Io(error) => Some(error),
65            _ => None,
66        }
67    }
68}
69
70impl From<std::io::Error> for Error {
71    fn from(value: std::io::Error) -> Self {
72        Self::Io(value)
73    }
74}