Skip to main content

ito_core/validate/
report.rs

1//! Report builder for validation runs.
2//!
3//! Validation functions accumulate multiple issues and then compute a summary and
4//! validity flag (strict vs non-strict) when finishing.
5
6use super::{ValidationIssue, ValidationReport};
7
8#[derive(Debug, Default)]
9/// Incrementally build a [`ValidationReport`].
10pub struct ReportBuilder {
11    strict: bool,
12    issues: Vec<ValidationIssue>,
13}
14
15impl ReportBuilder {
16    /// Create a new builder.
17    pub fn new(strict: bool) -> Self {
18        Self {
19            strict,
20            issues: Vec::new(),
21        }
22    }
23
24    /// Add a single issue.
25    pub fn push(&mut self, issue: ValidationIssue) {
26        self.issues.push(issue);
27    }
28
29    /// Extend this builder with multiple issues.
30    pub fn extend<I>(&mut self, issues: I)
31    where
32        I: IntoIterator<Item = ValidationIssue>,
33    {
34        self.issues.extend(issues);
35    }
36
37    /// Finish building and compute the final report fields.
38    pub fn finish(self) -> ValidationReport {
39        ValidationReport::new(self.issues, self.strict)
40    }
41}
42
43/// Convenience constructor for a [`ReportBuilder`].
44pub fn report(strict: bool) -> ReportBuilder {
45    ReportBuilder::new(strict)
46}