Skip to main content

des_sim/execution/
result.rs

1//! The `result` module defines the outcome types for a simulation run:
2//! `SimulationResult`, `SimulationOutput`, and `SimulationError`.
3//!
4//! These types encapsulate the final state of the simulation, whether it completed
5//! successfully or terminated due to an error, providing access to the final
6//! simulation time and model state.
7
8use crate::primitive::time::SimTime;
9
10/// A type alias representing the final outcome of a simulation run.
11pub type SimulationResult<M, Err> = Result<SimulationOutput<M>, SimulationError<M, Err>>;
12
13/// The final output of a successfully completed simulation.
14#[derive(Debug)]
15pub struct SimulationOutput<M> {
16    /// The simulation time at which the execution finished.
17    time: SimTime,
18    /// The state of the model at the end of the simulation.
19    model: M,
20}
21
22impl<M> SimulationOutput<M> {
23    /// Create `SimulationOutput` instance.
24    pub(crate) fn new(time: SimTime, model: M) -> Self {
25        Self { time, model }
26    }
27
28    /// Returns the last simulation tick (time) reached.
29    pub fn last_tick(&self) -> SimTime {
30        self.time
31    }
32
33    /// Returns a reference to the final model state.
34    pub fn model(&self) -> &M {
35        &self.model
36    }
37
38    /// Returns a mutable reference to the final model state.
39    pub fn model_mut(&mut self) -> &mut M {
40        &mut self.model
41    }
42}
43
44/// The state of a simulation that terminated due to an error.
45#[derive(Debug)]
46pub struct SimulationError<M, Err> {
47    /// The simulation time at which the error occurred.
48    time: SimTime,
49    /// The state of the model at the time of the error.
50    model: M,
51    /// The specific error that caused the simulation to halt.
52    error: Err,
53}
54
55impl<M, Err> SimulationError<M, Err> {
56    pub(crate) fn new(time: SimTime, model: M, error: Err) -> Self {
57        Self { time, model, error }
58    }
59
60    /// Returns the simulation tick (time) at which the error occurred.
61    pub fn last_tick(&self) -> SimTime {
62        self.time
63    }
64
65    /// Returns a reference to the model state at the time of the error.
66    pub fn model(&self) -> &M {
67        &self.model
68    }
69
70    /// Returns a mutable reference to the model state at the time of the error.
71    pub fn model_mut(&mut self) -> &mut M {
72        &mut self.model
73    }
74
75    /// Returns a reference to the error that occurred.
76    pub fn error(&self) -> &Err {
77        &self.error
78    }
79
80    /// Returns a mutable reference to the error that occurred.
81    pub fn error_mut(&mut self) -> &mut Err {
82        &mut self.error
83    }
84}