Skip to main content

orbit_core/
error.rs

1//! Error type for `orbit-core`. Deliberately small — this layer has few
2//! independent failure modes.
3
4use std::fmt;
5
6pub type Result<T, E = Error> = std::result::Result<T, E>;
7
8#[derive(Debug)]
9pub enum Error {
10    /// `Fleet::join` was called twice in the same process.
11    AlreadyJoined { name: &'static str },
12
13    /// Fleet capacity cannot be zero — Orbit needs at least one addressable node.
14    EmptyFleet,
15
16    /// A node id must address one of the fleet's reserved physical slots.
17    NodeOutsideFleet { node_id: u16, fleet_capacity: u16 },
18
19    /// Shared-memory operation failed.
20    Io(std::io::Error)
21}
22
23impl fmt::Display for Error {
24    fn fmt(
25        &self,
26        f: &mut fmt::Formatter<'_>
27    ) -> fmt::Result {
28        match self {
29            Self::AlreadyJoined { name } => {
30                write!(f, "fleet '{name}' has already been joined in this process")
31            }
32            Self::EmptyFleet => {
33                write!(f, "fleet_capacity must be ≥ 1; Orbit needs at least one node lane")
34            }
35            Self::NodeOutsideFleet { node_id, fleet_capacity } => {
36                write!(f, "node_id {node_id} is outside fleet_capacity {fleet_capacity}")
37            }
38            Self::Io(err) => write!(f, "orbit io error: {err}")
39        }
40    }
41}
42
43impl std::error::Error for Error {
44    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
45        match self {
46            Self::Io(err) => Some(err),
47            _ => None
48        }
49    }
50}