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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright 2025 Umberto Gotti <umberto.gotti@umbertogotti.dev>
// Licensed under the MIT License
// SPDX-License-Identifier: MIT
use crate::reporting::column_widths::ColumnWidths;
use crate::reporting::offence::Offence;
use crate::reporting::offence_threshold::OffenceThreshold;
use std::collections::BTreeSet;
// One table for every rule. Columns are sized to their contents so the report
// stays readable when a rule name or a path grows, and the summary line is
// greppable so a wrapper script can report a count without parsing the table.
pub struct ReportPrinter {
files_scanned: usize,
threshold: OffenceThreshold,
applied: Vec<String>,
skipped: Vec<String>,
unconfigured: Vec<String>,
exclusions: Vec<(String, usize)>,
config_file: Option<String>,
baseline: Option<String>,
suppressed: usize,
stale: usize,
fixed: usize,
}
impl ReportPrinter {
pub fn new(files_scanned: usize) -> Self {
Self {
files_scanned,
threshold: OffenceThreshold::default(),
applied: Vec::new(),
skipped: Vec::new(),
unconfigured: Vec::new(),
exclusions: Vec::new(),
config_file: None,
baseline: None,
suppressed: 0,
stale: 0,
fixed: 0,
}
}
// How many files --fix repaired. Stated alongside what is left, because a
// fixer reporting only its successes would be the same silence this tool
// refuses everywhere else.
pub fn with_fixed(self, fixed: usize) -> Self {
Self { fixed, ..self }
}
// A run that reported nothing while a baseline hid four hundred findings
// would be the most comfortable lie this tool could tell, so the count is
// in the summary of every run that used one -- including when it is the
// whole story and the report itself is empty.
pub fn with_baseline(self, baseline: Option<String>, suppressed: usize, stale: usize) -> Self {
Self {
baseline,
suppressed,
stale,
..self
}
}
// A run configured by a file the reader never typed on the command line
// must say so, or the switches in force are invisible.
pub fn with_config_file(self, config_file: Option<String>) -> Self {
Self {
config_file,
..self
}
}
pub fn with_exclusions(self, exclusions: Vec<(String, usize)>) -> Self {
Self { exclusions, ..self }
}
pub fn with_rules(
self,
applied: Vec<String>,
skipped: Vec<String>,
unconfigured: Vec<String>,
) -> Self {
Self {
applied,
skipped,
unconfigured,
..self
}
}
pub fn with_threshold(self, threshold: OffenceThreshold) -> Self {
Self { threshold, ..self }
}
pub fn print(&self, offences: &[Offence]) {
println!("{}", self.render(offences));
}
// Returns the report rather than writing it, so the shape is assertable in a
// test instead of being checked for not panicking.
pub fn render(&self, offences: &[Offence]) -> String {
let mut report = String::from("stern4rust report\n\n");
if offences.is_empty() {
report.push_str(self.clean_verdict());
report.push_str("\n\n");
report.push_str(&self.roster());
report.push_str(&self.exclusion_roster());
report.push_str(&self.config_line());
report.push_str(&self.baseline_line());
report.push_str(&self.fixed_line());
report.push_str(&self.summary(offences));
return report;
}
// Sized to what is shown, so one withheld offence with a very long path
// cannot widen a column nothing in the report occupies.
let shown = self.threshold.kept(offences);
let widths = ColumnWidths::of(shown);
report.push_str(&Self::heading(&widths));
for offence in shown {
report.push_str(&Self::row(offence, &widths));
report.push_str(&Self::correction_row(offence, &widths));
}
report.push('\n');
report.push_str(&self.omission(offences));
report.push_str(&self.roster());
report.push_str(&self.exclusion_roster());
report.push_str(&self.config_line());
report.push_str(&self.baseline_line());
report.push_str(&self.fixed_line());
report.push_str(&self.summary(offences));
report
}
// "All rules are satisfied" is only true when all of them ran. Saying it
// after --skip turned two off, or after the header rule was dropped for
// want of a header file, would be the tool telling the comfortable lie it
// exists to catch.
fn clean_verdict(&self) -> &'static str {
if self.everything_ran() {
"All rules are satisfied."
} else {
"All applied rules are satisfied."
}
}
fn everything_ran(&self) -> bool {
self.skipped.is_empty() && self.unconfigured.is_empty()
}
// Named, not counted. A count answers "how many", which is only useful to a
// reader who already knows how many there are.
fn roster(&self) -> String {
if self.applied.is_empty() {
return String::new();
}
let mut roster = format!(" applied: {}\n", self.applied.join(", "));
if !self.everything_ran() {
roster.push_str(&format!(" not applied: {}\n", self.absences().join(", ")));
}
roster.push('\n');
roster
}
// What --fix repaired, stated beside what it could not. A fixer reporting
// only its successes would leave the reader believing the file is done.
fn fixed_line(&self) -> String {
if self.fixed == 0 {
return String::new();
}
format!(" fixed: {} file(s) rewritten\n\n", self.fixed)
}
// Named with its count, because a run that reported nothing while a
// baseline hid four hundred findings would be the most comfortable lie this
// tool could tell. A stale entry is called out for the same reason a dead
// --exclude pattern is: it describes an offence somebody has since fixed,
// and until the file is rewritten it makes the baseline look like it is
// still holding something back.
fn baseline_line(&self) -> String {
let Some(path) = &self.baseline else {
return String::new();
};
let mut line = format!(" baseline: {path} ({} suppressed)\n", self.suppressed);
if self.stale > 0 {
line.push_str(&format!(
" {} baseline entries matched nothing -- rerun with --write-baseline to \
refresh it\n",
self.stale
));
}
line.push('\n');
line
}
// A run configured by a file the reader never typed must say which file.
// Every switch in force would otherwise be invisible, and a report that
// applied one rule because of a line in a .toml would look exactly like one
// that applied one rule because somebody asked for it.
fn config_line(&self) -> String {
match &self.config_file {
Some(path) => format!(" config: {path}\n\n"),
None => String::new(),
}
}
// Every pattern with the number of files it removed, including zero. A
// pattern that matched nothing is the one the reader most needs to see:
// it names a tree that has moved or been deleted, and until somebody is
// told, it goes on looking like it is doing work.
fn exclusion_roster(&self) -> String {
if self.exclusions.is_empty() {
return String::new();
}
let listed: Vec<String> = self
.exclusions
.iter()
.map(|(pattern, count)| format!("{pattern} ({count} files)"))
.collect();
let mut roster = format!(" excluded: {}\n", listed.join(", "));
let dead = self.unmatched();
if !dead.is_empty() {
roster.push_str(&format!(
" matched nothing: {} -- delete the pattern or correct it\n",
dead.join(", ")
));
}
roster.push('\n');
roster
}
fn unmatched(&self) -> Vec<&str> {
self.exclusions
.iter()
.filter(|(_, count)| *count == 0)
.map(|(pattern, _)| pattern.as_str())
.collect()
}
// Skipped and unconfigured are both "did not run" and are not the same
// thing. One is a choice the reader made; the other is a flag they did not
// pass, and saying which is the difference between a note and an
// instruction.
fn absences(&self) -> Vec<String> {
self.skipped
.iter()
.map(|name| format!("{name} (skipped)"))
.chain(
self.unconfigured
.iter()
.map(|name| format!("{name} (needs --header-file)")),
)
.collect()
}
// Named alongside the flag that raises it. A cap nobody was told about reads
// as "that was all of them", which is the one thing this report must never
// say when it is not true.
fn omission(&self, offences: &[Offence]) -> String {
let omitted = self.threshold.omitted(offences);
if omitted == 0 {
return String::new();
}
format!(
"... and {omitted} more offences not shown. Raise --offence-threshold \
(currently {}, use 0 for all) to see them.\n\n",
self.threshold.limit()
)
}
fn heading(widths: &ColumnWidths) -> String {
format!(
"{:<file$} {:>line$} {:<rule$} offence\n{} {} {} {}\n",
"file",
"line",
"rule",
"-".repeat(widths.file),
"-".repeat(widths.line),
"-".repeat(widths.rule),
"-".repeat(widths.description),
file = widths.file,
line = widths.line,
rule = widths.rule
)
}
fn row(offence: &Offence, widths: &ColumnWidths) -> String {
format!(
"{:<file$} {:>line$} {:<rule$} {}\n",
offence.file,
offence.line,
offence.rule,
offence.description,
file = widths.file,
line = widths.line,
rule = widths.rule
)
}
// On its own line beneath the offence rather than in a fifth column. The
// description column is already the widest thing in the report, and a
// correction is a sentence rather than a field -- side by side, neither
// would be readable.
fn correction_row(offence: &Offence, widths: &ColumnWidths) -> String {
let indent = widths.file + widths.line + widths.rule + 6;
format!("{}fix: {}\n", " ".repeat(indent), offence.correction)
}
fn excluded_total(&self) -> usize {
self.exclusions.iter().map(|(_, count)| count).sum()
}
fn summary(&self, offences: &[Offence]) -> String {
let broken: BTreeSet<&str> = offences.iter().map(|offence| offence.rule).collect();
format!(
"summary: files_scanned={} files_excluded={} offences={} rules_broken={} \
rules_applied={} rules_skipped={} rules_unconfigured={}",
self.files_scanned,
self.excluded_total(),
offences.len(),
broken.len(),
self.applied.len(),
self.skipped.len(),
self.unconfigured.len()
)
}
}