Skip to main content

ifc_validate/express/
options.rs

1//! Limits on EXPRESS evaluation.
2
3pub use crate::report::BudgetExceeded;
4
5/// How much work rule evaluation may do on one model.
6///
7/// A WHERE rule can call FUNCTIONs that loop and recurse, over data a user
8/// did not write. These limits turn a pathological file into typed
9/// [`BudgetExceeded`] results instead of a hang or a stack overflow:
10///
11/// - [`Self::steps_per_rule`] bounds one rule on one instance (each
12///   expression node and statement is a step); exceeding it reports that
13///   rule on that instance as not evaluated and moves on;
14/// - [`Self::max_depth`] bounds nested FUNCTION calls and derived
15///   attributes, which is what keeps the native stack safe;
16/// - [`Self::total_steps`] bounds the whole run; exceeding it stops rule
17///   evaluation and marks the report truncated.
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19#[non_exhaustive]
20pub struct EvalLimits {
21    /// Steps one rule may take on one instance.
22    pub steps_per_rule: u64,
23    /// Nesting of FUNCTION calls and derived attributes.
24    pub max_depth: usize,
25    /// Steps the whole run may take.
26    pub total_steps: u64,
27}
28
29impl EvalLimits {
30    /// Generous for real files: the IFC rules take tens to a few thousand
31    /// steps per instance, and nest a handful of calls deep.
32    pub const DEFAULT: Self = Self {
33        steps_per_rule: 1_000_000,
34        max_depth: 48,
35        total_steps: 2_000_000_000,
36    };
37
38    /// These limits with a different per-rule step limit.
39    #[must_use]
40    pub const fn with_steps_per_rule(mut self, steps: u64) -> Self {
41        self.steps_per_rule = steps;
42        self
43    }
44
45    /// These limits with a different nesting limit.
46    #[must_use]
47    pub const fn with_max_depth(mut self, depth: usize) -> Self {
48        self.max_depth = depth;
49        self
50    }
51
52    /// These limits with a different limit for the whole run.
53    #[must_use]
54    pub const fn with_total_steps(mut self, steps: u64) -> Self {
55        self.total_steps = steps;
56        self
57    }
58}
59
60impl Default for EvalLimits {
61    fn default() -> Self {
62        Self::DEFAULT
63    }
64}