Skip to main content

cranpose_core/
node_error.rs

1/// An error from an applier or from applying composition commands to nodes.
2#[derive(Debug, Clone, PartialEq, Eq)]
3pub enum NodeError {
4    /// No node has this id.
5    Missing {
6        /// The id that was looked up.
7        id: NodeId,
8    },
9    /// The node's type differs from the type that the caller requested.
10    TypeMismatch {
11        /// The id of the node.
12        id: NodeId,
13        /// The name of the type that the caller requested.
14        expected: &'static str,
15    },
16    /// The node lacks the context that an operation needs.
17    MissingContext {
18        /// The id of the node.
19        id: NodeId,
20        /// What the node lacks.
21        reason: &'static str,
22    },
23    /// A node with this id exists already.
24    AlreadyExists {
25        /// The id that is already in use.
26        id: NodeId,
27    },
28    /// A queued command has no payload or the wrong one.
29    MalformedCommandPayload {
30        /// The label of the command whose payload is wrong.
31        tag: &'static str,
32    },
33    /// A slot host could not be used for `operation`.
34    SlotHostUnavailable {
35        /// The operation that needed the host.
36        operation: &'static str,
37        /// Why the host was unavailable.
38        reason: &'static str,
39    },
40    /// Reconciling a composition did not settle within `limit` iterations.
41    RecompositionLimitExceeded {
42        /// The loop that reached the limit.
43        operation: &'static str,
44        /// The most iterations allowed.
45        limit: usize,
46    },
47}
48
49impl std::fmt::Display for NodeError {
50    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51        match self {
52            Self::Missing { id } => write!(f, "node {id} missing"),
53            Self::TypeMismatch { id, expected } => {
54                write!(f, "node {id} type mismatch; expected {expected}")
55            }
56            Self::MissingContext { id, reason } => {
57                write!(f, "missing context for node {id}: {reason}")
58            }
59            Self::AlreadyExists { id } => {
60                write!(f, "node {id} already exists")
61            }
62            Self::MalformedCommandPayload { tag } => {
63                write!(f, "command queue missing or invalid {tag} payload")
64            }
65            Self::SlotHostUnavailable { operation, reason } => {
66                write!(f, "{operation} cannot access slot host: {reason}")
67            }
68            Self::RecompositionLimitExceeded { operation, limit } => {
69                write!(
70                    f,
71                    "{operation} exceeded {limit} iterations while reconciling composition"
72                )
73            }
74        }
75    }
76}
77
78impl std::error::Error for NodeError {}
79
80use crate::NodeId;