1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/// Handle template validation command.
pub fn handle_template_validate(colors: Colors) {
let _ = writeln!(
crate::cli::handlers::boundary::stdout(),
"{}Validating templates...{}",
colors.bold(),
colors.reset()
);
let _ = writeln!(crate::cli::handlers::boundary::stdout());
let templates = get_all_templates();
let partials_set: std::collections::HashSet<String> =
get_shared_partials().keys().cloned().collect();
// Collect templates and sort by name using functional pipeline
let sorted_templates: Vec<_> = templates.iter().sorted_by(|a, b| a.0.cmp(b.0)).collect();
// Process all templates and accumulate results using fold
let (total_errors, total_warnings) =
sorted_templates
.iter()
.fold((0u32, 0u32), |(err, warn), (name, (content, _))| {
let result = validate_template(content, &partials_set);
if result.is_valid {
let _ = writeln!(
crate::cli::handlers::boundary::stdout(),
"{}✓{} {}{}{}",
colors.green(),
colors.reset(),
colors.cyan(),
name,
colors.reset()
);
} else {
let _ = writeln!(
crate::cli::handlers::boundary::stdout(),
"{}✗{} {}{}{}",
colors.red(),
colors.reset(),
colors.cyan(),
name,
colors.reset()
);
}
// Print errors using iterator for_each
result.errors.iter().for_each(|error| {
let _ = writeln!(
crate::cli::handlers::boundary::stdout(),
" {}error:{} {}",
colors.red(),
colors.reset(),
format_error(error)
);
});
// Print warnings using iterator for_each
result.warnings.iter().for_each(|warning| {
let _ = writeln!(
crate::cli::handlers::boundary::stdout(),
" {}warning:{} {}",
colors.yellow(),
colors.reset(),
format_warning(warning)
);
});
if !result.variables.is_empty() {
let var_names: Vec<&str> =
result.variables.iter().map(|v| v.name.as_str()).collect();
let _ = writeln!(
crate::cli::handlers::boundary::stdout(),
" {}variables:{} {}",
colors.dim(),
colors.reset(),
var_names.join(", ")
);
}
if !result.partials.is_empty() {
let _ = writeln!(
crate::cli::handlers::boundary::stdout(),
" {}partials:{} {}",
colors.dim(),
colors.reset(),
result.partials.join(", ")
);
}
// Accumulate counts without mutation
(
err + result.errors.len() as u32,
warn + result.warnings.len() as u32,
)
});
let _ = writeln!(crate::cli::handlers::boundary::stdout());
if total_errors == 0 {
let _ = writeln!(
crate::cli::handlers::boundary::stdout(),
"{}All templates validated successfully!{}",
colors.green(),
colors.reset()
);
if total_warnings > 0 {
let _ = writeln!(
crate::cli::handlers::boundary::stdout(),
"{total_warnings} warnings"
);
}
} else {
let _ = writeln!(
crate::cli::handlers::boundary::stdout(),
"{}Validation failed with {} error(s){}",
colors.red(),
total_errors,
colors.reset()
);
if total_warnings > 0 {
let _ = writeln!(
crate::cli::handlers::boundary::stdout(),
"{total_warnings} warnings"
);
}
crate::cli::handlers::boundary::exit_with_code(1);
}
}