Skip to main content

alint_rules/
file_header.rs

1//! `file_header` — first N lines of each file in scope must match a pattern.
2
3use std::path::Path;
4
5use alint_core::{
6    Context, Error, FixSpec, Fixer, Level, PerFileRule, Result, Rule, RuleSpec, Scope, Violation,
7};
8use regex::Regex;
9use serde::Deserialize;
10
11use crate::fixers::FilePrependFixer;
12
13#[derive(Debug, Deserialize)]
14struct Options {
15    pattern: String,
16    #[serde(default = "default_lines")]
17    lines: usize,
18}
19
20fn default_lines() -> usize {
21    20
22}
23
24#[derive(Debug)]
25pub struct FileHeaderRule {
26    id: String,
27    level: Level,
28    policy_url: Option<String>,
29    message: Option<String>,
30    scope: Scope,
31    pattern_src: String,
32    pattern: Regex,
33    lines: usize,
34    fixer: Option<FilePrependFixer>,
35}
36
37impl Rule for FileHeaderRule {
38    fn id(&self) -> &str {
39        &self.id
40    }
41    fn level(&self) -> Level {
42        self.level
43    }
44    fn policy_url(&self) -> Option<&str> {
45        self.policy_url.as_deref()
46    }
47
48    fn fixer(&self) -> Option<&dyn Fixer> {
49        self.fixer.as_ref().map(|f| f as &dyn Fixer)
50    }
51
52    fn evaluate(&self, ctx: &Context<'_>) -> Result<Vec<Violation>> {
53        let mut violations = Vec::new();
54        for entry in ctx.index.files() {
55            if !self.scope.matches(&entry.path) {
56                continue;
57            }
58            let full = ctx.root.join(&entry.path);
59            let bytes = match std::fs::read(&full) {
60                Ok(b) => b,
61                Err(e) => {
62                    violations.push(
63                        Violation::new(format!("could not read file: {e}"))
64                            .with_path(entry.path.clone()),
65                    );
66                    continue;
67                }
68            };
69            violations.extend(self.evaluate_file(ctx, &entry.path, &bytes)?);
70        }
71        Ok(violations)
72    }
73
74    fn as_per_file(&self) -> Option<&dyn PerFileRule> {
75        Some(self)
76    }
77}
78
79impl PerFileRule for FileHeaderRule {
80    fn path_scope(&self) -> &Scope {
81        &self.scope
82    }
83
84    fn evaluate_file(
85        &self,
86        _ctx: &Context<'_>,
87        path: &Path,
88        bytes: &[u8],
89    ) -> Result<Vec<Violation>> {
90        let Ok(text) = std::str::from_utf8(bytes) else {
91            return Ok(vec![
92                Violation::new("file is not valid UTF-8; cannot match header")
93                    .with_path(std::sync::Arc::<Path>::from(path)),
94            ]);
95        };
96        let header: String = text.split_inclusive('\n').take(self.lines).collect();
97        if self.pattern.is_match(&header) {
98            return Ok(Vec::new());
99        }
100        let msg = self.message.clone().unwrap_or_else(|| {
101            format!(
102                "first {} line(s) do not match required header /{}/",
103                self.lines, self.pattern_src
104            )
105        });
106        Ok(vec![
107            Violation::new(msg)
108                .with_path(std::sync::Arc::<Path>::from(path))
109                .with_location(1, 1),
110        ])
111    }
112}
113
114pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
115    let Some(paths) = &spec.paths else {
116        return Err(Error::rule_config(
117            &spec.id,
118            "file_header requires a `paths` field",
119        ));
120    };
121    let opts: Options = spec
122        .deserialize_options()
123        .map_err(|e| Error::rule_config(&spec.id, format!("invalid options: {e}")))?;
124    if opts.lines == 0 {
125        return Err(Error::rule_config(
126            &spec.id,
127            "file_header `lines` must be > 0",
128        ));
129    }
130    let pattern = Regex::new(&opts.pattern)
131        .map_err(|e| Error::rule_config(&spec.id, format!("invalid pattern: {e}")))?;
132    let fixer = match &spec.fix {
133        Some(FixSpec::FilePrepend { file_prepend }) => {
134            let source = alint_core::resolve_content_source(
135                &spec.id,
136                "file_prepend",
137                &file_prepend.content,
138                &file_prepend.content_from,
139            )?;
140            Some(FilePrependFixer::new(source))
141        }
142        Some(other) => {
143            return Err(Error::rule_config(
144                &spec.id,
145                format!("fix.{} is not compatible with file_header", other.op_name()),
146            ));
147        }
148        None => None,
149    };
150    Ok(Box::new(FileHeaderRule {
151        id: spec.id.clone(),
152        level: spec.level,
153        policy_url: spec.policy_url.clone(),
154        message: spec.message.clone(),
155        scope: Scope::from_paths_spec(paths)?,
156        pattern_src: opts.pattern,
157        pattern,
158        lines: opts.lines,
159        fixer,
160    }))
161}
162
163#[cfg(test)]
164mod tests {
165    use super::*;
166    use crate::test_support::{ctx, spec_yaml, tempdir_with_files};
167
168    #[test]
169    fn build_rejects_missing_paths_field() {
170        let spec = spec_yaml(
171            "id: t\n\
172             kind: file_header\n\
173             pattern: \"^// SPDX\"\n\
174             level: error\n",
175        );
176        assert!(build(&spec).is_err());
177    }
178
179    #[test]
180    fn build_rejects_zero_lines() {
181        let spec = spec_yaml(
182            "id: t\n\
183             kind: file_header\n\
184             paths: \"src/**/*.rs\"\n\
185             pattern: \"^// SPDX\"\n\
186             lines: 0\n\
187             level: error\n",
188        );
189        let err = build(&spec).unwrap_err().to_string();
190        assert!(err.contains("lines"), "unexpected: {err}");
191    }
192
193    #[test]
194    fn build_rejects_invalid_regex() {
195        let spec = spec_yaml(
196            "id: t\n\
197             kind: file_header\n\
198             paths: \"src/**/*.rs\"\n\
199             pattern: \"[unterminated\"\n\
200             level: error\n",
201        );
202        assert!(build(&spec).is_err());
203    }
204
205    #[test]
206    fn evaluate_passes_when_header_matches() {
207        let spec = spec_yaml(
208            "id: t\n\
209             kind: file_header\n\
210             paths: \"src/**/*.rs\"\n\
211             pattern: \"SPDX-License-Identifier: Apache-2.0\"\n\
212             level: error\n",
213        );
214        let rule = build(&spec).unwrap();
215        let (tmp, idx) = tempdir_with_files(&[(
216            "src/main.rs",
217            b"// SPDX-License-Identifier: Apache-2.0\n\nfn main() {}\n",
218        )]);
219        let v = rule.evaluate(&ctx(tmp.path(), &idx)).unwrap();
220        assert!(v.is_empty(), "header should match: {v:?}");
221    }
222
223    #[test]
224    fn evaluate_fires_when_header_missing() {
225        let spec = spec_yaml(
226            "id: t\n\
227             kind: file_header\n\
228             paths: \"src/**/*.rs\"\n\
229             pattern: \"SPDX-License-Identifier:\"\n\
230             level: error\n",
231        );
232        let rule = build(&spec).unwrap();
233        let (tmp, idx) = tempdir_with_files(&[("src/main.rs", b"fn main() {}\n")]);
234        let v = rule.evaluate(&ctx(tmp.path(), &idx)).unwrap();
235        assert_eq!(v.len(), 1);
236    }
237
238    #[test]
239    fn evaluate_only_inspects_first_n_lines() {
240        // Pattern only on line 30, but `lines: 5` only looks at
241        // lines 1-5 → rule fires.
242        let spec = spec_yaml(
243            "id: t\n\
244             kind: file_header\n\
245             paths: \"src/**/*.rs\"\n\
246             pattern: \"NEEDLE\"\n\
247             lines: 5\n\
248             level: error\n",
249        );
250        let rule = build(&spec).unwrap();
251        let mut content = String::new();
252        for _ in 0..30 {
253            content.push_str("filler\n");
254        }
255        content.push_str("NEEDLE\n");
256        let (tmp, idx) = tempdir_with_files(&[("src/main.rs", content.as_bytes())]);
257        let v = rule.evaluate(&ctx(tmp.path(), &idx)).unwrap();
258        assert_eq!(v.len(), 1);
259    }
260}