Skip to main content

caustic/tooling/core/output/exit/
standard.rs

1//! Standard exit condition evaluation and reporting.
2
3use super::super::super::{
4    conditions::{ExitCondition, ExitReason},
5    diagnostics::GlobalDiagnostics,
6};
7
8/// Evaluates all active exit conditions each timestep.
9pub struct ExitEvaluator {
10    pub conditions: Vec<Box<dyn ExitCondition>>,
11    pub initial: GlobalDiagnostics,
12}
13
14impl ExitEvaluator {
15    pub fn new(initial: GlobalDiagnostics) -> Self {
16        Self {
17            conditions: vec![],
18            initial,
19        }
20    }
21
22    pub fn add_condition(&mut self, cond: Box<dyn ExitCondition>) {
23        self.conditions.push(cond);
24    }
25
26    /// Evaluate all conditions and return the first triggered reason.
27    pub fn check(&self, current: &GlobalDiagnostics) -> Option<ExitReason> {
28        self.conditions
29            .iter()
30            .find_map(|c| c.check(current, &self.initial))
31    }
32}