Skip to main content

calandria_sim/simulation/error/
step.rs

1//! One-step scheduler, model, monitor, and lifecycle failures.
2
3use core::fmt;
4
5use crate::{ActionKey, ActionMeta, SimulationPhase};
6
7use super::{KernelFailure, LimitFailure};
8
9/// Why one deterministic kernel step could not complete.
10#[derive(Debug)]
11pub enum StepError<ModelError, MonitorError, SchedulerError> {
12    /// Consumer model action failed after its staged effects rolled back.
13    Model {
14        /// Action that invoked the model.
15        action: ActionMeta,
16        /// Consumer model failure.
17        source: ModelError,
18    },
19    /// A post-commit monitor rejected the resulting state.
20    Monitor {
21        /// Committed action rejected by the monitor.
22        action: ActionMeta,
23        /// Consumer monitor failure.
24        source: MonitorError,
25    },
26    /// Scheduler policy failed during selection or replay validation.
27    Scheduler(SchedulerError),
28    /// Scheduler returned an action outside the supplied ready set.
29    InvalidSelection(ActionKey),
30    /// A kernel ownership or lifecycle invariant failed.
31    Kernel(KernelFailure),
32    /// A hard execution limit prevented another transition.
33    Limit(LimitFailure),
34    /// Panic unwinding crossed a model, monitor, or scheduler boundary.
35    Poisoned,
36    /// The simulation is no longer active.
37    Inactive(SimulationPhase),
38}
39
40impl<ME: fmt::Display, NE: fmt::Display, SE: fmt::Display> fmt::Display for StepError<ME, NE, SE> {
41    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
42        match self {
43            Self::Model { action, source } => write!(
44                formatter,
45                "model action {} failed: {source}",
46                action.id().get()
47            ),
48            Self::Monitor { action, source } => write!(
49                formatter,
50                "monitor rejected action {}: {source}",
51                action.id().get()
52            ),
53            Self::Scheduler(source) => write!(formatter, "scheduler failed: {source}"),
54            Self::InvalidSelection(action) => write!(
55                formatter,
56                "scheduler selected unavailable action for duty {}",
57                action.duty().get()
58            ),
59            Self::Kernel(failure) => failure.fmt(formatter),
60            Self::Limit(failure) => failure.fmt(formatter),
61            Self::Poisoned => formatter.write_str("simulation is poisoned"),
62            Self::Inactive(phase) => write!(formatter, "simulation is {phase:?}"),
63        }
64    }
65}
66
67impl<ME, NE, SE> core::error::Error for StepError<ME, NE, SE>
68where
69    ME: fmt::Debug + fmt::Display,
70    NE: fmt::Debug + fmt::Display,
71    SE: fmt::Debug + fmt::Display,
72{
73}