Skip to main content

alint_rules/
file_starts_with.rs

1//! `file_starts_with` — every file in scope must begin with the
2//! configured prefix (byte-level).
3//!
4//! Useful for SPDX headers (when `file_header`'s line-oriented
5//! matching is too loose), magic bytes on binary formats, or a
6//! required "do not edit — generated" banner. Works on any byte
7//! content, not just UTF-8.
8//!
9//! Check-only: the correct fix is to call `file_prepend` with
10//! the same content, and having the rule do it implicitly would
11//! silently duplicate the prefix on files that start with a
12//! similar but non-matching string.
13
14use std::path::Path;
15
16use alint_core::{Context, Error, Level, PerFileRule, Result, Rule, RuleSpec, Scope, Violation};
17use serde::Deserialize;
18
19use crate::io::read_prefix_n;
20
21#[derive(Debug, Deserialize)]
22#[serde(deny_unknown_fields)]
23struct Options {
24    /// The required prefix. Matched byte-for-byte.
25    prefix: String,
26}
27
28#[derive(Debug)]
29pub struct FileStartsWithRule {
30    id: String,
31    level: Level,
32    policy_url: Option<String>,
33    message: Option<String>,
34    scope: Scope,
35    prefix: Vec<u8>,
36}
37
38impl Rule for FileStartsWithRule {
39    fn id(&self) -> &str {
40        &self.id
41    }
42    fn level(&self) -> Level {
43        self.level
44    }
45    fn policy_url(&self) -> Option<&str> {
46        self.policy_url.as_deref()
47    }
48
49    fn evaluate(&self, ctx: &Context<'_>) -> Result<Vec<Violation>> {
50        let mut violations = Vec::new();
51        for entry in ctx.index.files() {
52            if !self.scope.matches(&entry.path, ctx.index) {
53                continue;
54            }
55            // Bounded read: only the first `prefix.len()` bytes
56            // matter. When this rule runs solo (e.g. via `alint
57            // fix --only ...` or test harnesses) we read just
58            // those bytes, not the whole file. The dispatch-flip
59            // path (`evaluate_file`) gets the full slice from
60            // the engine and bounds-checks via `starts_with`.
61            let full = ctx.root.join(&entry.path);
62            let Ok(bytes) = read_prefix_n(&full, self.prefix.len()) else {
63                continue;
64            };
65            violations.extend(self.evaluate_file(ctx, &entry.path, &bytes)?);
66        }
67        Ok(violations)
68    }
69
70    fn as_per_file(&self) -> Option<&dyn PerFileRule> {
71        Some(self)
72    }
73}
74
75impl PerFileRule for FileStartsWithRule {
76    fn path_scope(&self) -> &Scope {
77        &self.scope
78    }
79
80    fn evaluate_file(
81        &self,
82        _ctx: &Context<'_>,
83        path: &Path,
84        bytes: &[u8],
85    ) -> Result<Vec<Violation>> {
86        if bytes.starts_with(&self.prefix) {
87            return Ok(Vec::new());
88        }
89        let msg = self
90            .message
91            .clone()
92            .unwrap_or_else(|| "file does not start with the required prefix".to_string());
93        Ok(vec![
94            Violation::new(msg)
95                .with_path(std::sync::Arc::<Path>::from(path))
96                .with_location(1, 1),
97        ])
98    }
99
100    fn max_bytes_needed(&self) -> Option<usize> {
101        Some(self.prefix.len())
102    }
103}
104
105pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
106    let _paths = spec
107        .paths
108        .as_ref()
109        .ok_or_else(|| Error::rule_config(&spec.id, "file_starts_with requires a `paths` field"))?;
110    let opts: Options = spec
111        .deserialize_options()
112        .map_err(|e| Error::rule_config(&spec.id, format!("invalid options: {e}")))?;
113    if opts.prefix.is_empty() {
114        return Err(Error::rule_config(
115            &spec.id,
116            "file_starts_with.prefix must not be empty",
117        ));
118    }
119    if spec.fix.is_some() {
120        return Err(Error::rule_config(
121            &spec.id,
122            "file_starts_with has no fix op — pair with an explicit `file_prepend` rule if you \
123             want auto-prepend (avoids silently duplicating near-matching prefixes).",
124        ));
125    }
126    Ok(Box::new(FileStartsWithRule {
127        id: spec.id.clone(),
128        level: spec.level,
129        policy_url: spec.policy_url.clone(),
130        message: spec.message.clone(),
131        scope: Scope::from_spec(spec)?,
132        prefix: opts.prefix.into_bytes(),
133    }))
134}
135
136#[cfg(test)]
137mod tests {
138    use super::*;
139    use crate::test_support::{ctx, spec_yaml, tempdir_with_files};
140
141    #[test]
142    fn build_rejects_missing_paths_field() {
143        let spec = spec_yaml(
144            "id: t\n\
145             kind: file_starts_with\n\
146             prefix: \"#!/bin/sh\"\n\
147             level: error\n",
148        );
149        assert!(build(&spec).is_err());
150    }
151
152    #[test]
153    fn build_rejects_empty_prefix() {
154        let spec = spec_yaml(
155            "id: t\n\
156             kind: file_starts_with\n\
157             paths: \"**/*.sh\"\n\
158             prefix: \"\"\n\
159             level: error\n",
160        );
161        let err = build(&spec).unwrap_err().to_string();
162        assert!(err.contains("empty"), "unexpected: {err}");
163    }
164
165    #[test]
166    fn build_rejects_fix_block() {
167        let spec = spec_yaml(
168            "id: t\n\
169             kind: file_starts_with\n\
170             paths: \"**/*.sh\"\n\
171             prefix: \"#!/bin/sh\\n\"\n\
172             level: error\n\
173             fix:\n  \
174               file_prepend:\n    \
175                 content: \"x\"\n",
176        );
177        assert!(build(&spec).is_err());
178    }
179
180    #[test]
181    fn evaluate_passes_when_prefix_matches() {
182        let spec = spec_yaml(
183            "id: t\n\
184             kind: file_starts_with\n\
185             paths: \"**/*.sh\"\n\
186             prefix: \"#!/bin/sh\"\n\
187             level: error\n",
188        );
189        let rule = build(&spec).unwrap();
190        let (tmp, idx) = tempdir_with_files(&[("script.sh", b"#!/bin/sh\necho hi\n")]);
191        let v = rule.evaluate(&ctx(tmp.path(), &idx)).unwrap();
192        assert!(v.is_empty(), "expected pass: {v:?}");
193    }
194
195    #[test]
196    fn evaluate_fires_when_prefix_missing() {
197        let spec = spec_yaml(
198            "id: t\n\
199             kind: file_starts_with\n\
200             paths: \"**/*.sh\"\n\
201             prefix: \"#!/bin/sh\"\n\
202             level: error\n",
203        );
204        let rule = build(&spec).unwrap();
205        let (tmp, idx) = tempdir_with_files(&[("script.sh", b"echo hi\n")]);
206        let v = rule.evaluate(&ctx(tmp.path(), &idx)).unwrap();
207        assert_eq!(v.len(), 1);
208    }
209}