ifc_validate/where_rule/budget.rs
1//! Bounds on how much work one validation run may do.
2//!
3//! # Why a validator needs a budget
4//!
5//! Validation runs on files a user did not write, in CI, on a schedule. A
6//! pathological or hostile file must not turn that into an unbounded run: a
7//! 2 GB model with a million dangling references would otherwise produce a
8//! million findings and exhaust memory before reporting anything.
9//!
10//! The budget is a *reporting* limit, not a correctness compromise. When it
11//! is hit the report is marked truncated, so "12 errors" never silently means
12//! "at least 12 errors".
13//!
14//! # Why there is no depth limit
15//!
16//! Every graph walk this crate performs -- supertype chains, SELECT
17//! membership, defined-type alias chains -- runs over the *schema's* type
18//! graph, never over the file's entity graph. The schema is bundled and
19//! finite, so those walks are bounded by its size and guarded against cycles
20//! by a visited set (`type_check::select`); a file cannot lengthen them. The
21//! per-entity checks are single passes over each record's own slots.
22//!
23//! A caller-tunable depth would therefore bound nothing a file controls, and
24//! a small value would only turn "not yet searched" into "not a member" --
25//! the false accusation `type_check::select` documents. The one quantity a
26//! file does control is how many findings it provokes, and that is what this
27//! budget caps.
28
29/// Limits applied to one validation run.
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub struct Budget {
32 /// Stop recording after this many findings.
33 pub max_findings: usize,
34}
35
36impl Budget {
37 /// A budget large enough for real files and small enough to bound memory.
38 ///
39 /// 10,000 findings is far past the point where a report is actionable --
40 /// a file with that many defects needs a different conversation -- and
41 /// costs a few hundred KB to hold.
42 pub const DEFAULT: Self = Self {
43 max_findings: 10_000,
44 };
45
46 /// An explicitly unbounded budget, for tests and for callers that have
47 /// already decided the input is trustworthy.
48 pub const UNLIMITED: Self = Self {
49 max_findings: usize::MAX,
50 };
51}
52
53impl Default for Budget {
54 fn default() -> Self {
55 Self::DEFAULT
56 }
57}