Skip to main content

armillae_simulate/
error.rs

1use crate::{
2    BackendId, CapabilityId, ClockErrorCode, ClockInstanceId, ClockKey, ClockTypeId,
3    ExecuteEntryId, ExecutionPlane, ModuleId, SemanticVersion, SimulationStatus, SystemErrorCode,
4    SystemId, SystemTrigger, VersionRequirement,
5};
6
7#[derive(Debug, thiserror::Error)]
8#[non_exhaustive]
9pub enum SimulationBuildError {
10    #[error("invalid module descriptor: {code}: {message}")]
11    InvalidDescriptor {
12        module: Option<ModuleId>,
13        code: String,
14        message: String,
15    },
16    #[error("duplicate module `{module}`")]
17    DuplicateModule { module: ModuleId },
18    #[error("duplicate execute entry `{entry}`")]
19    DuplicateExecuteEntry {
20        entry: ExecuteEntryId,
21        first: ModuleId,
22        second: ModuleId,
23    },
24    #[error("duplicate clock type `{clock_type}`")]
25    DuplicateClockType {
26        clock_type: ClockTypeId,
27        first: ModuleId,
28        second: ModuleId,
29    },
30    #[error("duplicate system `{system}`")]
31    DuplicateSystem {
32        system: SystemId,
33        first: ModuleId,
34        second: ModuleId,
35    },
36    #[error("module `{module}` requires missing module `{dependency}`")]
37    MissingDependency {
38        module: ModuleId,
39        dependency: ModuleId,
40    },
41    #[error("module `{module}` has an incompatible dependency")]
42    IncompatibleDependency {
43        module: ModuleId,
44        dependency: ModuleId,
45        required: VersionRequirement,
46        found: SemanticVersion,
47    },
48    #[error("system `{system}` references an unknown trigger")]
49    UnknownTrigger {
50        module: ModuleId,
51        system: SystemId,
52        trigger: SystemTrigger,
53    },
54    #[error("invalid ordering edge from `{system}` to `{target}`")]
55    InvalidOrdering {
56        system: SystemId,
57        target: SystemId,
58        reason: OrderingError,
59    },
60    #[error("system ordering contains a cycle")]
61    OrderingCycle {
62        trigger: SystemTrigger,
63        systems: Vec<SystemId>,
64    },
65    #[error("module `{module}` requires unsupported capability `{capability}`")]
66    UnsupportedCapability {
67        module: ModuleId,
68        capability: CapabilityId,
69    },
70    #[error("module `{module}` uses an unsupported execution plane")]
71    UnsupportedExecutionPlane {
72        module: ModuleId,
73        execution: ExecutionPlane,
74    },
75    #[error("module `{module}` targets a different backend")]
76    BackendMismatch {
77        module: ModuleId,
78        required: BackendId,
79        actual: BackendId,
80    },
81    #[error("module `{module}` requires an incompatible adapter version")]
82    IncompatibleAdapter {
83        module: ModuleId,
84        backend: BackendId,
85        required: VersionRequirement,
86        found: SemanticVersion,
87    },
88    #[error("native registration failed for module {module:?}: {code}")]
89    NativeRegistrationFailed {
90        module: Option<ModuleId>,
91        code: String,
92        message: String,
93    },
94    #[error("failed to build system graph")]
95    SystemGraphBuildFailed {
96        trigger: SystemTrigger,
97        code: String,
98        message: String,
99    },
100}
101
102#[derive(Clone, Copy, Debug, PartialEq, Eq)]
103#[non_exhaustive]
104pub enum OrderingError {
105    SelfReference,
106    UnknownSystem,
107    DifferentTrigger,
108}
109
110#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
111#[error("system execution failed: {code}: {message}")]
112pub struct SystemExecutionError {
113    pub code: SystemErrorCode,
114    pub message: String,
115}
116
117pub type SystemExecutionResult = Result<(), SystemExecutionError>;
118
119#[derive(Clone, Copy, Debug, PartialEq, Eq)]
120#[non_exhaustive]
121pub enum SimulationOperation {
122    Execute,
123    ReadClock,
124    InsertClock,
125    RemoveClock,
126    Advance,
127    InspectWorld,
128    WriteWorld,
129    Stop,
130}
131
132#[derive(Clone, Debug, PartialEq, Eq)]
133pub struct SchemaViolation {
134    pub instance_path: String,
135    pub schema_path: String,
136    pub keyword: Option<String>,
137}
138
139#[derive(Clone, Debug, PartialEq, Eq)]
140#[non_exhaustive]
141pub enum AdvanceRequestViolation {
142    EmptyTargets,
143    DuplicateInstance { instance: ClockInstanceId },
144}
145
146#[derive(Debug, thiserror::Error)]
147#[non_exhaustive]
148pub enum SimulationError {
149    #[error("cannot perform {operation:?} while simulation is {status:?}")]
150    InvalidState {
151        operation: SimulationOperation,
152        status: SimulationStatus,
153    },
154    #[error("cannot perform {operation:?} because simulation is faulted")]
155    Faulted { operation: SimulationOperation },
156    #[error("unknown execute entry `{entry}`")]
157    UnknownExecuteEntry { entry: ExecuteEntryId },
158    #[error("unknown clock type `{clock_type}`")]
159    UnknownClockType { clock_type: ClockTypeId },
160    #[error("native clock type `{rust_type}` is not bound")]
161    NativeClockTypeNotBound { rust_type: &'static str },
162    #[error("unknown clock instance")]
163    UnknownClockInstance { key: ClockKey },
164    #[error("clock instance already exists")]
165    DuplicateClockInstance { key: ClockKey },
166    #[error("invalid execute input for `{entry}`")]
167    InvalidExecuteInput {
168        entry: ExecuteEntryId,
169        violations: Vec<SchemaViolation>,
170    },
171    #[error("execute entry `{entry}` does not declare output")]
172    UnexpectedExecuteOutput { entry: ExecuteEntryId },
173    #[error("failed to encode output for `{entry}`")]
174    ExecuteOutputEncodingFailed { entry: ExecuteEntryId },
175    #[error("execute entry `{entry}` did not produce required output")]
176    MissingExecuteOutput { entry: ExecuteEntryId },
177    #[error("execute entry `{entry}` produced output more than once")]
178    ConflictingExecuteOutput { entry: ExecuteEntryId },
179    #[error("invalid execute output for `{entry}`")]
180    InvalidExecuteOutput {
181        entry: ExecuteEntryId,
182        violations: Vec<SchemaViolation>,
183    },
184    #[error("invalid clock value")]
185    InvalidClockValue {
186        key: ClockKey,
187        violations: Vec<SchemaViolation>,
188    },
189    #[error("clock value rejected: {code}: {message}")]
190    ClockValueRejected {
191        key: ClockKey,
192        code: ClockErrorCode,
193        message: String,
194    },
195    #[error("invalid advance request")]
196    InvalidAdvanceRequest {
197        clock_type: ClockTypeId,
198        reason: AdvanceRequestViolation,
199    },
200    #[error("invalid clock step")]
201    InvalidClockStep {
202        clock_type: ClockTypeId,
203        instance: ClockInstanceId,
204        violations: Vec<SchemaViolation>,
205    },
206    #[error("clock transition failed: {code}: {message}")]
207    ClockTransitionFailed {
208        clock_type: ClockTypeId,
209        instance: ClockInstanceId,
210        code: ClockErrorCode,
211        message: String,
212    },
213    #[error("system `{system}` failed: {code}: {message}")]
214    SystemFailed {
215        system: SystemId,
216        trigger: SystemTrigger,
217        code: SystemErrorCode,
218        message: String,
219    },
220    #[error("backend `{backend}` failed: {code}: {message}")]
221    BackendFailure {
222        backend: BackendId,
223        operation: SimulationOperation,
224        code: String,
225        message: String,
226    },
227    #[error("backend `{backend}` panicked during {operation:?}")]
228    BackendPanicked {
229        backend: BackendId,
230        operation: SimulationOperation,
231    },
232}
233
234impl SimulationError {
235    #[cfg(feature = "testing")]
236    pub(crate) fn faults_simulation(&self) -> bool {
237        matches!(
238            self,
239            Self::UnexpectedExecuteOutput { .. }
240                | Self::ExecuteOutputEncodingFailed { .. }
241                | Self::MissingExecuteOutput { .. }
242                | Self::ConflictingExecuteOutput { .. }
243                | Self::InvalidExecuteOutput { .. }
244                | Self::SystemFailed { .. }
245                | Self::BackendFailure { .. }
246                | Self::BackendPanicked { .. }
247        )
248    }
249}