calandria_sim/simulation/error/
injection.rs1use core::fmt;
4
5use calandria::{Moment, Span};
6
7use crate::{DutyId, ScheduleFailure, SimulationPhase};
8
9#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11pub enum InjectionFailure {
12 Poisoned,
14 Inactive(SimulationPhase),
16 UnknownTarget(DutyId),
18 TargetStopped(DutyId),
20 TimeOverflow {
22 current: Moment,
24 delay: Span,
26 },
27 BeyondTimeLimit {
29 requested: Moment,
31 limit: Moment,
33 },
34 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#[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 pub const fn failure(&self) -> InjectionFailure {
78 self.failure
79 }
80
81 pub fn into_event(self) -> E {
83 self.event
84 }
85
86 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> {}