Skip to main content

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
39    // Whether this rule has what it needs to say anything.
40    //
41    // Most rules always do, so the default is true. The header rule does not:
42    // it has no idea what your header says until `--header-file` tells it, and
43    // registering it anyway would let a run report "all rules satisfied" for a
44    // rule that never looked at a single file.
45    //
46    // A rule answers this for itself so the registry does not have to name any
47    // rule in particular. The alternative -- an `if` in the registry that knows
48    // about the header rule -- is how the registry ends up with a second,
49    // hand-maintained idea of which rules exist.
50    fn is_configured(&self) -> bool {
51        true
52    }
53}