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(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        match self {
26            Self::AlreadyJoined { name } => {
27                write!(f, "fleet '{name}' has already been joined in this process")
28            }
29            Self::EmptyFleet => {
30                write!(
31                    f,
32                    "fleet_capacity must be ≥ 1; Orbit needs at least one node lane"
33                )
34            }
35            Self::NodeOutsideFleet {
36                node_id,
37                fleet_capacity,
38            } => write!(
39                f,
40                "node_id {node_id} is outside fleet_capacity {fleet_capacity}"
41            ),
42            Self::Io(err) => write!(f, "orbit io error: {err}"),
43        }
44    }
45}
46
47impl std::error::Error for Error {
48    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
49        match self {
50            Self::Io(err) => Some(err),
51            _ => None,
52        }
53    }
54}