calandria_sim/simulation/error/
build.rs1use core::{fmt, num::NonZeroUsize};
4
5use calandria::Moment;
6
7use crate::Topology;
8
9#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11pub enum SimulationBuildFailure {
12 DutyCapacity {
14 limit: NonZeroUsize,
16 actual: usize,
18 },
19 ReadyCapacityOverflow {
21 duties: usize,
23 events: NonZeroUsize,
25 },
26 InitialTimeBeyondLimit {
28 initial: Moment,
30 limit: Moment,
32 },
33}
34
35impl fmt::Display for SimulationBuildFailure {
36 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
37 match self {
38 Self::DutyCapacity { limit, actual } => write!(
39 formatter,
40 "simulation topology has {actual} duties but limit is {limit}"
41 ),
42 Self::ReadyCapacityOverflow { duties, events } => write!(
43 formatter,
44 "combining {duties} duties with {events} pending events overflows ready capacity"
45 ),
46 Self::InitialTimeBeyondLimit { initial, limit } => write!(
47 formatter,
48 "initial moment {}ns exceeds virtual-time limit {}ns",
49 initial.as_nanos(),
50 limit.as_nanos()
51 ),
52 }
53 }
54}
55
56impl core::error::Error for SimulationBuildFailure {}
57
58pub struct SimulationBuildError<M, S, N> {
60 failure: SimulationBuildFailure,
61 model: M,
62 topology: Topology,
63 scheduler: S,
64 monitor: N,
65}
66
67impl<M, S, N> SimulationBuildError<M, S, N> {
68 pub(in crate::simulation) const fn new(
69 failure: SimulationBuildFailure,
70 model: M,
71 topology: Topology,
72 scheduler: S,
73 monitor: N,
74 ) -> Self {
75 Self {
76 failure,
77 model,
78 topology,
79 scheduler,
80 monitor,
81 }
82 }
83
84 pub const fn failure(&self) -> &SimulationBuildFailure {
86 &self.failure
87 }
88
89 pub fn into_parts(self) -> (SimulationBuildFailure, M, Topology, S, N) {
91 (
92 self.failure,
93 self.model,
94 self.topology,
95 self.scheduler,
96 self.monitor,
97 )
98 }
99}
100
101impl<M, S, N> fmt::Display for SimulationBuildError<M, S, N> {
102 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
103 self.failure.fmt(formatter)
104 }
105}
106
107impl<M, S, N> fmt::Debug for SimulationBuildError<M, S, N> {
108 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
109 formatter
110 .debug_struct("SimulationBuildError")
111 .field("failure", &self.failure)
112 .finish_non_exhaustive()
113 }
114}
115
116impl<M, S, N> core::error::Error for SimulationBuildError<M, S, N> {}