1#![warn(rust_2024_compatibility, clippy::all)]
2
3mod cargo_toml;
6mod counting;
7mod structure;
8mod visibility;
9
10use dictator_decree_abi::{BoxDecree, Decree, Diagnostics};
11use dictator_supreme::SupremeConfig;
12
13pub use cargo_toml::lint_cargo_toml;
14
15#[derive(Debug, Clone)]
17pub struct RustConfig {
18 pub max_lines: usize,
19 pub min_edition: Option<String>,
21 pub min_rust_version: Option<String>,
23 pub ignore_comments: bool,
25}
26
27impl Default for RustConfig {
28 fn default() -> Self {
29 Self {
30 max_lines: 400,
31 min_edition: None,
32 min_rust_version: None,
33 ignore_comments: false,
34 }
35 }
36}
37
38#[must_use]
40pub fn lint_source(source: &str) -> Diagnostics {
41 lint_source_with_configs(source, &RustConfig::default(), &SupremeConfig::default())
42}
43
44#[must_use]
46pub fn lint_source_with_config(source: &str, config: &RustConfig) -> Diagnostics {
47 let mut diags = Diagnostics::new();
48
49 counting::check_file_line_count(source, config.max_lines, &mut diags);
50 visibility::check_visibility_ordering(source, &mut diags);
51
52 diags
53}
54
55#[must_use]
57pub fn lint_source_with_configs(
58 source: &str,
59 rust_config: &RustConfig,
60 supreme_config: &SupremeConfig,
61) -> Diagnostics {
62 let mut diags = Diagnostics::new();
63
64 let supreme_diags = dictator_supreme::lint_source_with_owner(source, supreme_config, "rust");
65
66 if rust_config.ignore_comments {
67 let lines: Vec<&str> = source.lines().collect();
69 diags.extend(supreme_diags.into_iter().filter(|d| {
70 if d.rule == "rust/line-too-long" {
71 let line_idx = source[..d.span.start].matches('\n').count();
72 !lines
73 .get(line_idx)
74 .is_some_and(|line| line.trim_start().starts_with("//"))
75 } else {
76 true
77 }
78 }));
79 } else {
80 diags.extend(supreme_diags);
81 }
82
83 diags.extend(lint_source_with_config(source, rust_config));
85
86 diags
87}
88
89#[derive(Default)]
90pub struct RustDecree {
91 config: RustConfig,
92 supreme: SupremeConfig,
93}
94
95impl RustDecree {
96 #[must_use]
97 pub const fn new(config: RustConfig, supreme: SupremeConfig) -> Self {
98 Self { config, supreme }
99 }
100}
101
102impl Decree for RustDecree {
103 fn name(&self) -> &'static str {
104 "rust"
105 }
106
107 fn lint(&self, path: &str, source: &str) -> Diagnostics {
108 let filename = std::path::Path::new(path)
109 .file_name()
110 .and_then(|f| f.to_str())
111 .unwrap_or("");
112
113 if filename == "Cargo.toml" {
115 return cargo_toml::lint_cargo_toml(source, &self.config);
116 }
117
118 let mut diags = lint_source_with_configs(source, &self.config, &self.supreme);
120
121 structure::check_mod_rs_structure(path, &mut diags);
123
124 diags
125 }
126
127 fn metadata(&self) -> dictator_decree_abi::DecreeMetadata {
128 dictator_decree_abi::DecreeMetadata {
129 abi_version: dictator_decree_abi::ABI_VERSION.to_string(),
130 decree_version: env!("CARGO_PKG_VERSION").to_string(),
131 description: "Rust structural rules".to_string(),
132 dectauthors: Some(env!("CARGO_PKG_AUTHORS").to_string()),
133 supported_extensions: vec!["rs".to_string()],
134 supported_filenames: vec![
135 "Cargo.toml".to_string(),
136 "build.rs".to_string(),
137 "rust-toolchain".to_string(),
138 "rust-toolchain.toml".to_string(),
139 ".rustfmt.toml".to_string(),
140 "rustfmt.toml".to_string(),
141 "clippy.toml".to_string(),
142 ".clippy.toml".to_string(),
143 ],
144 skip_filenames: vec!["Cargo.lock".to_string()],
145 capabilities: vec![dictator_decree_abi::Capability::Lint],
146 }
147 }
148}
149
150#[must_use]
151pub fn init_decree() -> BoxDecree {
152 Box::new(RustDecree::default())
153}
154
155#[must_use]
157pub fn init_decree_with_config(config: RustConfig) -> BoxDecree {
158 Box::new(RustDecree::new(config, SupremeConfig::default()))
159}
160
161#[must_use]
163pub fn init_decree_with_configs(config: RustConfig, supreme: SupremeConfig) -> BoxDecree {
164 Box::new(RustDecree::new(config, supreme))
165}
166
167#[must_use]
169pub fn config_from_decree_settings(settings: &dictator_core::DecreeSettings) -> RustConfig {
170 RustConfig {
171 max_lines: settings.max_lines.unwrap_or(400),
172 min_edition: settings.min_edition.clone(),
173 min_rust_version: settings.min_rust_version.clone(),
174 ignore_comments: settings.ignore_comments.unwrap_or(false),
175 }
176}
177
178#[cfg(test)]
179mod tests {
180 use super::*;
181
182 #[test]
183 fn ignores_long_comment_lines_when_configured() {
184 let long_comment = format!("// {}\n", "x".repeat(150));
185 let src = format!("fn main() {{\n{long_comment}}}\n");
186 let config = RustConfig {
187 ignore_comments: true,
188 ..Default::default()
189 };
190 let supreme = SupremeConfig {
191 max_line_length: Some(120),
192 ..Default::default()
193 };
194 let diags = lint_source_with_configs(&src, &config, &supreme);
195 assert!(!diags.iter().any(|d| d.rule == "rust/line-too-long"));
196 }
197
198 #[test]
199 fn detects_long_comment_lines_when_not_configured() {
200 let long_comment = format!("// {}\n", "x".repeat(150));
201 let src = format!("fn main() {{\n{long_comment}}}\n");
202 let config = RustConfig::default(); let supreme = SupremeConfig {
204 max_line_length: Some(120),
205 ..Default::default()
206 };
207 let diags = lint_source_with_configs(&src, &config, &supreme);
208 assert!(diags.iter().any(|d| d.rule == "rust/line-too-long"));
209 }
210
211 #[test]
212 fn still_detects_long_code_lines_with_ignore_comments() {
213 let long_code = format!(" let x = \"{}\";\n", "a".repeat(150));
214 let src = format!("fn main() {{\n{long_code}}}\n");
215 let config = RustConfig {
216 ignore_comments: true,
217 ..Default::default()
218 };
219 let supreme = SupremeConfig {
220 max_line_length: Some(120),
221 ..Default::default()
222 };
223 let diags = lint_source_with_configs(&src, &config, &supreme);
224 assert!(diags.iter().any(|d| d.rule == "rust/line-too-long"));
225 }
226}