Skip to main content

alint_rules/
lib.rs

1//! Built-in rule implementations for alint.
2//!
3//! Rules are registered into an [`alint_core::RuleRegistry`] via
4//! [`register_builtin`]. Each kind has its own submodule.
5
6use alint_core::RuleRegistry;
7
8pub mod case;
9pub mod command;
10pub mod dir_absent;
11pub mod dir_contains;
12pub mod dir_exists;
13pub mod dir_only_contains;
14pub mod every_matching_has;
15pub mod executable_bit;
16pub mod executable_has_shebang;
17pub mod file_absent;
18pub mod file_content_forbidden;
19pub mod file_content_matches;
20pub mod file_ends_with;
21pub mod file_exists;
22pub mod file_footer;
23pub mod file_hash;
24pub mod file_header;
25pub mod file_is_ascii;
26pub mod file_is_text;
27pub mod file_max_lines;
28pub mod file_max_size;
29pub mod file_min_lines;
30pub mod file_min_size;
31pub mod file_shebang;
32pub mod file_starts_with;
33pub mod filename_case;
34pub mod filename_regex;
35pub mod final_newline;
36pub mod fixers;
37pub mod for_each_dir;
38pub mod for_each_file;
39pub mod indent_style;
40pub mod io;
41pub mod line_endings;
42pub mod line_max_width;
43pub mod max_consecutive_blank_lines;
44pub mod max_directory_depth;
45pub mod max_files_per_directory;
46pub mod no_bidi_controls;
47pub mod no_bom;
48pub mod no_case_conflicts;
49pub mod no_empty_files;
50pub mod no_illegal_windows_names;
51pub mod no_merge_conflict_markers;
52pub mod no_submodules;
53pub mod no_symlinks;
54pub mod no_trailing_whitespace;
55pub mod no_zero_width_chars;
56pub mod pair;
57pub mod shebang_has_executable;
58pub mod structured_path;
59pub mod unique_by;
60
61/// Register every built-in rule kind into the given registry.
62///
63/// Naming convention: rules that have a `dir_*` sibling keep
64/// their `file_*` prefix (`file_exists` vs `dir_exists`); rules
65/// with no such parallel also register a short alias without the
66/// prefix — `content_matches`, `content_forbidden`, `header`,
67/// `is_text`, `max_size`. Both forms resolve to the same
68/// builder; new rules land under short names only.
69pub fn register_builtin(registry: &mut RuleRegistry) {
70    registry.register("file_exists", file_exists::build);
71    registry.register("file_absent", file_absent::build);
72    registry.register("dir_exists", dir_exists::build);
73    registry.register("dir_absent", dir_absent::build);
74
75    registry.register("file_content_matches", file_content_matches::build);
76    registry.register("content_matches", file_content_matches::build);
77    registry.register("file_content_forbidden", file_content_forbidden::build);
78    registry.register("content_forbidden", file_content_forbidden::build);
79    registry.register("file_header", file_header::build);
80    registry.register("header", file_header::build);
81    registry.register("file_max_size", file_max_size::build);
82    registry.register("max_size", file_max_size::build);
83    registry.register("file_min_size", file_min_size::build);
84    registry.register("min_size", file_min_size::build);
85    registry.register("file_min_lines", file_min_lines::build);
86    registry.register("min_lines", file_min_lines::build);
87    registry.register("file_max_lines", file_max_lines::build);
88    registry.register("max_lines", file_max_lines::build);
89    registry.register("file_footer", file_footer::build);
90    registry.register("footer", file_footer::build);
91    registry.register("file_shebang", file_shebang::build);
92    registry.register("shebang", file_shebang::build);
93
94    // Structured-query family — JSONPath queries over
95    // JSON / YAML / TOML documents.
96    registry.register("json_path_equals", structured_path::json_path_equals_build);
97    registry.register(
98        "json_path_matches",
99        structured_path::json_path_matches_build,
100    );
101    registry.register("yaml_path_equals", structured_path::yaml_path_equals_build);
102    registry.register(
103        "yaml_path_matches",
104        structured_path::yaml_path_matches_build,
105    );
106    registry.register("toml_path_equals", structured_path::toml_path_equals_build);
107    registry.register(
108        "toml_path_matches",
109        structured_path::toml_path_matches_build,
110    );
111    registry.register("file_is_text", file_is_text::build);
112    registry.register("is_text", file_is_text::build);
113
114    registry.register("filename_case", filename_case::build);
115    registry.register("filename_regex", filename_regex::build);
116    registry.register("pair", pair::build);
117    registry.register("for_each_dir", for_each_dir::build);
118    registry.register("for_each_file", for_each_file::build);
119    registry.register("dir_only_contains", dir_only_contains::build);
120    registry.register("unique_by", unique_by::build);
121    registry.register("dir_contains", dir_contains::build);
122    registry.register("every_matching_has", every_matching_has::build);
123
124    // Text-hygiene family (short names — no `file_` prefix).
125    registry.register("no_trailing_whitespace", no_trailing_whitespace::build);
126    registry.register("final_newline", final_newline::build);
127    registry.register("line_endings", line_endings::build);
128    registry.register("line_max_width", line_max_width::build);
129
130    // Security / Unicode sanity.
131    registry.register(
132        "no_merge_conflict_markers",
133        no_merge_conflict_markers::build,
134    );
135    registry.register("no_bidi_controls", no_bidi_controls::build);
136    registry.register("no_zero_width_chars", no_zero_width_chars::build);
137
138    // Encoding + content fingerprint.
139    registry.register("file_is_ascii", file_is_ascii::build);
140    registry.register("no_bom", no_bom::build);
141    registry.register("file_hash", file_hash::build);
142
143    // Structure / layout.
144    registry.register("max_directory_depth", max_directory_depth::build);
145    registry.register("max_files_per_directory", max_files_per_directory::build);
146    registry.register("no_empty_files", no_empty_files::build);
147
148    // Cross-platform / portable metadata.
149    registry.register("no_case_conflicts", no_case_conflicts::build);
150    registry.register("no_illegal_windows_names", no_illegal_windows_names::build);
151
152    // Unix metadata + git.
153    registry.register("no_symlinks", no_symlinks::build);
154    registry.register("executable_bit", executable_bit::build);
155    registry.register("executable_has_shebang", executable_has_shebang::build);
156    registry.register("shebang_has_executable", shebang_has_executable::build);
157    registry.register("no_submodules", no_submodules::build);
158
159    // Hygiene + byte fingerprint.
160    registry.register("indent_style", indent_style::build);
161    registry.register(
162        "max_consecutive_blank_lines",
163        max_consecutive_blank_lines::build,
164    );
165    registry.register("file_starts_with", file_starts_with::build);
166    registry.register("file_ends_with", file_ends_with::build);
167
168    // Plugin tier 1 — shell out to an external CLI per matched
169    // file. Trust-gated at config-load: only the user's own
170    // top-level config can declare these.
171    registry.register("command", command::build);
172}
173
174/// Convenience constructor that returns a fresh registry pre-populated with
175/// every built-in rule.
176pub fn builtin_registry() -> RuleRegistry {
177    let mut r = RuleRegistry::new();
178    register_builtin(&mut r);
179    r
180}
181
182#[cfg(test)]
183mod registry_tests {
184    use super::*;
185
186    #[test]
187    fn every_documented_kind_is_registered() {
188        let r = builtin_registry();
189        let known: Vec<&str> = r.known_kinds().collect();
190        for kind in [
191            // Prefixed kinds (parallel with dir_*).
192            "file_exists",
193            "file_absent",
194            "dir_exists",
195            "dir_absent",
196            // Prefixed + short alias pairs.
197            "file_content_matches",
198            "content_matches",
199            "file_content_forbidden",
200            "content_forbidden",
201            "file_header",
202            "header",
203            "file_max_size",
204            "max_size",
205            "file_min_size",
206            "min_size",
207            "file_min_lines",
208            "min_lines",
209            "file_max_lines",
210            "max_lines",
211            "file_footer",
212            "footer",
213            "file_shebang",
214            "shebang",
215            // Structured-query family.
216            "json_path_equals",
217            "json_path_matches",
218            "yaml_path_equals",
219            "yaml_path_matches",
220            "toml_path_equals",
221            "toml_path_matches",
222            "file_is_text",
223            "is_text",
224            // Short-only.
225            "filename_case",
226            "filename_regex",
227            "pair",
228            "for_each_dir",
229            "for_each_file",
230            "dir_only_contains",
231            "unique_by",
232            "dir_contains",
233            "every_matching_has",
234            // Text-hygiene family.
235            "no_trailing_whitespace",
236            "final_newline",
237            "line_endings",
238            "line_max_width",
239            // Security / Unicode sanity.
240            "no_merge_conflict_markers",
241            "no_bidi_controls",
242            "no_zero_width_chars",
243            // Encoding + fingerprint.
244            "file_is_ascii",
245            "no_bom",
246            "file_hash",
247            // Structure / layout.
248            "max_directory_depth",
249            "max_files_per_directory",
250            "no_empty_files",
251            // Portable metadata.
252            "no_case_conflicts",
253            "no_illegal_windows_names",
254            // Unix metadata + git.
255            "no_symlinks",
256            "executable_bit",
257            "executable_has_shebang",
258            "shebang_has_executable",
259            "no_submodules",
260            // Hygiene + byte fingerprint.
261            "indent_style",
262            "max_consecutive_blank_lines",
263            "file_starts_with",
264            "file_ends_with",
265            // Plugin (tier 1).
266            "command",
267        ] {
268            assert!(
269                known.contains(&kind),
270                "{kind} missing from builtin registry"
271            );
272        }
273    }
274}