Skip to main content

calandria_sim/simulation/error/
injection.rs

1//! Ownership-preserving external event injection failures.
2
3use core::fmt;
4
5use calandria::{Moment, Span};
6
7use crate::{DutyId, ScheduleFailure, SimulationPhase};
8
9/// Why an external modeled event could not enter a simulation.
10#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11pub enum InjectionFailure {
12    /// The simulation was poisoned by panic unwinding.
13    Poisoned,
14    /// The simulation no longer accepts external work.
15    Inactive(SimulationPhase),
16    /// The target is absent from the static topology.
17    UnknownTarget(DutyId),
18    /// The target has permanently stopped.
19    TargetStopped(DutyId),
20    /// Relative virtual-time arithmetic overflowed.
21    TimeOverflow {
22        /// Current virtual moment.
23        current: Moment,
24        /// Requested relative delay.
25        delay: Span,
26    },
27    /// The requested moment exceeds the configured ceiling.
28    BeyondTimeLimit {
29        /// Requested delivery moment.
30        requested: Moment,
31        /// Configured maximum virtual moment.
32        limit: Moment,
33    },
34    /// The bounded event timeline rejected admission.
35    Timeline(ScheduleFailure),
36}
37
38impl fmt::Display for InjectionFailure {
39    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            Self::Poisoned => formatter.write_str("simulation is poisoned"),
42            Self::Inactive(phase) => write!(formatter, "simulation is {phase:?}"),
43            Self::UnknownTarget(duty) => write!(formatter, "unknown duty {}", duty.get()),
44            Self::TargetStopped(duty) => write!(formatter, "duty {} has stopped", duty.get()),
45            Self::TimeOverflow { current, delay } => write!(
46                formatter,
47                "scheduling {}ns after {}ns would overflow virtual time",
48                delay.as_nanos(),
49                current.as_nanos()
50            ),
51            Self::BeyondTimeLimit { requested, limit } => write!(
52                formatter,
53                "event at {}ns exceeds virtual-time limit {}ns",
54                requested.as_nanos(),
55                limit.as_nanos()
56            ),
57            Self::Timeline(failure) => failure.fmt(formatter),
58        }
59    }
60}
61
62impl core::error::Error for InjectionFailure {}
63
64/// Ownership-preserving external injection error.
65#[derive(Debug)]
66pub struct InjectionError<E> {
67    event: E,
68    failure: InjectionFailure,
69}
70
71impl<E> InjectionError<E> {
72    pub(crate) const fn new(event: E, failure: InjectionFailure) -> Self {
73        Self { event, failure }
74    }
75
76    /// Returns the rejection reason.
77    pub const fn failure(&self) -> InjectionFailure {
78        self.failure
79    }
80
81    /// Returns ownership of the rejected event.
82    pub fn into_event(self) -> E {
83        self.event
84    }
85
86    /// Splits the rejected event from its reason.
87    pub fn into_parts(self) -> (E, InjectionFailure) {
88        (self.event, self.failure)
89    }
90}
91
92impl<E> fmt::Display for InjectionError<E> {
93    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
94        self.failure.fmt(formatter)
95    }
96}
97
98impl<E: fmt::Debug> core::error::Error for InjectionError<E> {}