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 persona: "Ferris".to_string(),
133 dectauthors: Some(env!("CARGO_PKG_AUTHORS").to_string()),
134 supported_extensions: vec!["rs".to_string()],
135 supported_filenames: vec![
136 "Cargo.toml".to_string(),
137 "build.rs".to_string(),
138 "rust-toolchain".to_string(),
139 "rust-toolchain.toml".to_string(),
140 ".rustfmt.toml".to_string(),
141 "rustfmt.toml".to_string(),
142 "clippy.toml".to_string(),
143 ".clippy.toml".to_string(),
144 ],
145 skip_filenames: vec!["Cargo.lock".to_string()],
146 file_scope_rules: dictator_supreme::file_scope_rules(&[
147 "file-too-long",
148 "missing-edition",
149 "missing-rust-version",
150 "unnecessary-mod-rs",
151 ]),
152 capabilities: vec![dictator_decree_abi::Capability::Lint],
153 }
154 }
155}
156
157#[must_use]
158pub fn init_decree() -> BoxDecree {
159 Box::new(RustDecree::default())
160}
161
162#[must_use]
164pub fn init_decree_with_config(config: RustConfig) -> BoxDecree {
165 Box::new(RustDecree::new(config, SupremeConfig::default()))
166}
167
168#[must_use]
170pub fn init_decree_with_configs(config: RustConfig, supreme: SupremeConfig) -> BoxDecree {
171 Box::new(RustDecree::new(config, supreme))
172}
173
174#[must_use]
176pub fn config_from_decree_settings(settings: &dictator_core::DecreeSettings) -> RustConfig {
177 RustConfig {
178 max_lines: settings.max_lines.unwrap_or(400),
179 min_edition: settings.min_edition.clone(),
180 min_rust_version: settings.min_rust_version.clone(),
181 ignore_comments: settings.ignore_comments.unwrap_or(false),
182 }
183}
184
185#[cfg(test)]
186mod tests;