ito_core/validate/
report.rs1use super::{ValidationIssue, ValidationReport};
10
11#[derive(Debug, Default)]
12pub struct ReportBuilder {
17 strict: bool,
18 issues: Vec<ValidationIssue>,
19}
20
21impl ReportBuilder {
22 pub fn new(strict: bool) -> Self {
28 Self {
29 strict,
30 issues: Vec::new(),
31 }
32 }
33
34 pub fn push(&mut self, issue: ValidationIssue) {
36 self.issues.push(issue);
37 }
38
39 pub fn extend<I>(&mut self, issues: I)
43 where
44 I: IntoIterator<Item = ValidationIssue>,
45 {
46 self.issues.extend(issues);
47 }
48
49 pub fn finish(self) -> ValidationReport {
54 ValidationReport::new(self.issues, self.strict)
55 }
56}
57
58pub fn report(strict: bool) -> ReportBuilder {
62 ReportBuilder::new(strict)
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 fn issue(level: &str, path: &str, message: &str) -> ValidationIssue {
70 ValidationIssue {
71 level: level.to_string(),
72 path: path.to_string(),
73 message: message.to_string(),
74 line: None,
75 column: None,
76 metadata: None,
77 }
78 }
79
80 #[test]
81 fn finish_non_strict_only_fails_on_errors() {
82 let mut builder = ReportBuilder::new(false);
83 builder.push(issue("WARNING", "spec.md", "brief purpose"));
84
85 let report = builder.finish();
86 assert!(report.valid);
87 assert_eq!(report.summary.errors, 0);
88 assert_eq!(report.summary.warnings, 1);
89 }
90
91 #[test]
92 fn finish_strict_fails_on_warnings() {
93 let mut builder = report(true);
94 builder.push(issue("WARNING", "spec.md", "brief purpose"));
95
96 let result = builder.finish();
97 assert!(!result.valid);
98 assert_eq!(result.summary.errors, 0);
99 assert_eq!(result.summary.warnings, 1);
100 }
101
102 #[test]
103 fn extend_collects_multiple_issues() {
104 let mut builder = report(false);
105 builder.extend(vec![
106 issue("ERROR", "a.md", "a"),
107 issue("INFO", "b.md", "b"),
108 issue("WARNING", "c.md", "c"),
109 ]);
110
111 let result = builder.finish();
112 assert!(!result.valid);
113 assert_eq!(result.issues.len(), 3);
114 assert_eq!(result.summary.errors, 1);
115 assert_eq!(result.summary.warnings, 1);
116 assert_eq!(result.summary.info, 1);
117 }
118}