stern4rust/rule.rs
1// Copyright 2025 Umberto Gotti <umberto.gotti@umbertogotti.dev>
2// Licensed under the MIT License
3// SPDX-License-Identifier: MIT
4
5use crate::offence::Offence;
6use crate::source_file::SourceFile;
7
8// One rule, one file, one implementation. A rule sees a single source file and
9// answers with what is wrong with it -- it does not walk, does not print, and
10// does not know which other rules exist.
11//
12// That is what keeps the set open: adding a rule is a new file implementing this
13// trait plus one line in the registry, and the rule is testable on a string of
14// source without a workspace behind it.
15pub trait Rule {
16 // Appears verbatim in the report's rule column, so it is kebab-case and
17 // reads as the thing being required rather than the thing being forbidden.
18 fn name(&self) -> &'static str;
19
20 // What is wrong with this one file, judged on its own.
21 fn check(&self, _file: &SourceFile) -> Vec<Offence> {
22 Vec::new()
23 }
24
25 // What is wrong with the set of files taken together.
26 //
27 // Some rules are not about a file at all. "There is exactly one all_tests.rs"
28 // and "every subfolder has a mod.rs" are facts about a tree, and the file
29 // that would carry the offence is precisely the one that does not exist --
30 // so there is nothing for check() to be handed.
31 //
32 // Both methods default to reporting nothing, so a rule implements whichever
33 // question it actually answers and the registry calls both without caring
34 // which.
35 fn check_workspace(&self, _files: &[SourceFile]) -> Vec<Offence> {
36 Vec::new()
37 }
38}