Skip to main content

calandria_sim/simulation/error/
build.rs

1//! Static simulation construction failures.
2
3use core::{fmt, num::NonZeroUsize};
4
5use calandria::Moment;
6
7use crate::Topology;
8
9/// Why a deterministic simulation could not be constructed.
10#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11pub enum SimulationBuildFailure {
12    /// The fixed topology exceeds its configured capacity.
13    DutyCapacity {
14        /// Configured duty limit.
15        limit: NonZeroUsize,
16        /// Requested static duty count.
17        actual: usize,
18    },
19    /// Duty and event capacities cannot share one ready-set allocation domain.
20    ReadyCapacityOverflow {
21        /// Static duty count.
22        duties: usize,
23        /// Pending-event capacity.
24        events: NonZeroUsize,
25    },
26    /// Initial virtual time is already beyond the run ceiling.
27    InitialTimeBeyondLimit {
28        /// Requested initial virtual moment.
29        initial: Moment,
30        /// Configured maximum virtual moment.
31        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
58/// Construction failure retaining every consumer-supplied component.
59pub 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    /// Returns the construction failure boundary.
85    pub const fn failure(&self) -> &SimulationBuildFailure {
86        &self.failure
87    }
88
89    /// Consumes the error and returns its failure and rejected components.
90    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> {}