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