Skip to main content

alint_rules/
pair.rs

1//! `pair` — for every file matching `primary`, require a file matching the
2//! `partner` template to exist somewhere in the tree.
3//!
4//! The partner template is a path string with `{dir}`, `{stem}`, `{ext}`,
5//! `{basename}`, `{path}`, `{parent_name}` substitutions derived from the
6//! primary match. The resolved partner path is looked up in the engine's
7//! `FileIndex`; a missing match emits a violation anchored on the primary.
8//!
9//! Example (every `.c` needs a same-directory `.h`):
10//!
11//! ```yaml
12//! - id: c-requires-h
13//!   kind: pair
14//!   primary: "**/*.c"
15//!   partner: "{dir}/{stem}.h"
16//!   level: error
17//!   message: "{{ctx.primary}} has no header at {{ctx.partner}}"
18//! ```
19
20use std::path::{Path, PathBuf};
21
22use alint_core::template::{PathTokens, render_message, render_path};
23use alint_core::{Context, Error, Level, Result, Rule, RuleSpec, Scope, Violation};
24use serde::Deserialize;
25
26#[derive(Debug, Deserialize)]
27#[serde(deny_unknown_fields)]
28struct Options {
29    primary: String,
30    partner: String,
31}
32
33#[derive(Debug)]
34pub struct PairRule {
35    id: String,
36    level: Level,
37    policy_url: Option<String>,
38    message: Option<String>,
39    primary_scope: Scope,
40    partner_template: String,
41}
42
43impl Rule for PairRule {
44    fn id(&self) -> &str {
45        &self.id
46    }
47    fn level(&self) -> Level {
48        self.level
49    }
50    fn policy_url(&self) -> Option<&str> {
51        self.policy_url.as_deref()
52    }
53
54    fn requires_full_index(&self) -> bool {
55        // Cross-file: a verdict on a primary file depends on
56        // whether its partner exists *anywhere* in the tree, not
57        // just in the diff. Roadmap §"Monorepo & scale" defines
58        // this rule as opting out of `--changed` filtering. We
59        // also leave `path_scope` as `None` so the engine doesn't
60        // skip-by-intersection — pair always evaluates.
61        true
62    }
63
64    fn evaluate(&self, ctx: &Context<'_>) -> Result<Vec<Violation>> {
65        let mut violations = Vec::new();
66        for entry in ctx.index.files() {
67            if !self.primary_scope.matches(&entry.path) {
68                continue;
69            }
70            let tokens = PathTokens::from_path(&entry.path);
71            let partner_rel = render_path(&self.partner_template, &tokens);
72            if partner_rel.is_empty() {
73                violations.push(
74                    Violation::new(format!(
75                        "partner template {:?} resolved to an empty path for {}",
76                        self.partner_template,
77                        entry.path.display(),
78                    ))
79                    .with_path(entry.path.clone()),
80                );
81                continue;
82            }
83            let partner_path = PathBuf::from(&partner_rel);
84            if resolves_to_self(&partner_path, &entry.path) {
85                violations.push(
86                    Violation::new(format!(
87                        "partner template {:?} resolves to the primary file itself ({}) — \
88                         check that the template differs from the primary",
89                        self.partner_template,
90                        entry.path.display(),
91                    ))
92                    .with_path(entry.path.clone()),
93                );
94                continue;
95            }
96            if ctx.index.contains_file(&partner_path) {
97                continue;
98            }
99            let message = self.format_message(&entry.path, &partner_path);
100            violations.push(Violation::new(message).with_path(entry.path.clone()));
101        }
102        Ok(violations)
103    }
104}
105
106fn resolves_to_self(partner: &Path, primary: &Path) -> bool {
107    partner == primary
108}
109
110impl PairRule {
111    fn format_message(&self, primary: &Path, partner: &Path) -> String {
112        let primary_str = primary.display().to_string();
113        let partner_str = partner.display().to_string();
114        if let Some(user_msg) = self.message.as_deref() {
115            return render_message(user_msg, |ns, key| match (ns, key) {
116                ("ctx", "primary") => Some(primary_str.clone()),
117                ("ctx", "partner") => Some(partner_str.clone()),
118                _ => None,
119            });
120        }
121        format!(
122            "{} has no matching partner at {} (template: {})",
123            primary_str, partner_str, self.partner_template,
124        )
125    }
126}
127
128pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
129    alint_core::reject_scope_filter_on_cross_file(spec, "pair")?;
130    let opts: Options = spec
131        .deserialize_options()
132        .map_err(|e| Error::rule_config(&spec.id, format!("invalid options: {e}")))?;
133    if opts.partner.trim().is_empty() {
134        return Err(Error::rule_config(
135            &spec.id,
136            "pair `partner` template must not be empty",
137        ));
138    }
139    let primary_patterns = vec![opts.primary.clone()];
140    let primary_scope = Scope::from_patterns(&primary_patterns)?;
141    Ok(Box::new(PairRule {
142        id: spec.id.clone(),
143        level: spec.level,
144        policy_url: spec.policy_url.clone(),
145        message: spec.message.clone(),
146        primary_scope,
147        partner_template: opts.partner,
148    }))
149}
150
151#[cfg(test)]
152mod tests {
153    use super::*;
154    use alint_core::{FileEntry, FileIndex};
155    use std::path::Path;
156
157    fn idx(paths: &[&str]) -> FileIndex {
158        FileIndex::from_entries(
159            paths
160                .iter()
161                .map(|p| FileEntry {
162                    path: std::path::Path::new(p).into(),
163                    is_dir: false,
164                    size: 1,
165                })
166                .collect(),
167        )
168    }
169
170    fn rule(primary: &str, partner: &str, message: Option<&str>) -> PairRule {
171        PairRule {
172            id: "t".into(),
173            level: Level::Error,
174            policy_url: None,
175            message: message.map(ToString::to_string),
176            primary_scope: Scope::from_patterns(&[primary.to_string()]).unwrap(),
177            partner_template: partner.into(),
178        }
179    }
180
181    fn eval(rule: &PairRule, files: &[&str]) -> Vec<Violation> {
182        let index = idx(files);
183        let ctx = Context {
184            root: Path::new("/"),
185            index: &index,
186            registry: None,
187            facts: None,
188            vars: None,
189            git_tracked: None,
190            git_blame: None,
191        };
192        rule.evaluate(&ctx).unwrap()
193    }
194
195    #[test]
196    fn passes_when_partner_exists() {
197        let r = rule("**/*.c", "{dir}/{stem}.h", None);
198        let v = eval(&r, &["src/mod/foo.c", "src/mod/foo.h"]);
199        assert!(v.is_empty(), "unexpected: {v:?}");
200    }
201
202    #[test]
203    fn violates_when_partner_missing() {
204        let r = rule("**/*.c", "{dir}/{stem}.h", None);
205        let v = eval(&r, &["src/mod/foo.c"]);
206        assert_eq!(v.len(), 1);
207        assert_eq!(v[0].path.as_deref(), Some(Path::new("src/mod/foo.c")));
208        assert!(v[0].message.contains("src/mod/foo.h"));
209    }
210
211    #[test]
212    fn violates_per_missing_primary() {
213        let r = rule("**/*.c", "{dir}/{stem}.h", None);
214        let v = eval(
215            &r,
216            &[
217                "src/mod/foo.c",
218                "src/mod/foo.h", // has partner — OK
219                "src/mod/bar.c", // no bar.h
220                "src/mod/baz.c", // no baz.h
221            ],
222        );
223        assert_eq!(v.len(), 2);
224    }
225
226    #[test]
227    fn no_primary_matches_means_no_violation() {
228        let r = rule("**/*.c", "{dir}/{stem}.h", None);
229        let v = eval(&r, &["README.md", "src/mod/other.rs"]);
230        assert!(v.is_empty());
231    }
232
233    #[test]
234    fn user_message_with_ctx_substitution() {
235        let r = rule(
236            "**/*.c",
237            "{dir}/{stem}.h",
238            Some("missing header {{ctx.partner}} for {{ctx.primary}}"),
239        );
240        let v = eval(&r, &["src/foo.c"]);
241        assert_eq!(v.len(), 1);
242        assert_eq!(v[0].message, "missing header src/foo.h for src/foo.c");
243    }
244
245    #[test]
246    fn rejects_partner_resolving_to_self() {
247        // Partner template evaluates to the same file as the primary — caught
248        // as a config/authorship mistake rather than silently passing.
249        let r = rule("**/*.c", "{path}", None);
250        let v = eval(&r, &["src/foo.c"]);
251        assert_eq!(v.len(), 1);
252        assert!(v[0].message.contains("primary file itself"));
253    }
254
255    #[test]
256    fn empty_partner_after_substitution_is_a_violation() {
257        // Template yields "" (only unknown-stripped content would). This
258        // exercises the empty-partner guard.
259        let r = rule("**/*.c", "", None);
260        // Guard is in build(); direct construction bypasses it, so the runtime
261        // guard in evaluate() catches this.
262        let v = eval(&r, &["src/foo.c"]);
263        assert_eq!(v.len(), 1);
264        assert!(v[0].message.contains("empty path"));
265    }
266
267    #[test]
268    fn build_rejects_scope_filter_on_cross_file_rule() {
269        // pair is a cross-file rule (requires_full_index = true);
270        // scope_filter is per-file-rules-only. The build path
271        // must reject it with a clear message pointing at the
272        // for_each_dir + when_iter: alternative.
273        //
274        // YAML indentation: keep all leading text flush-left
275        // (no Rust string-continuation `\` at line ends, no
276        // leading source-indent whitespace). The indentation
277        // seen by the YAML parser is the literal indentation
278        // inside the string.
279        let yaml = r#"
280id: t
281kind: pair
282primary: "**/*.c"
283partner: "{dir}/{stem}.h"
284level: error
285scope_filter:
286  has_ancestor: Cargo.toml
287"#;
288        let spec = crate::test_support::spec_yaml(yaml);
289        let err = build(&spec).unwrap_err().to_string();
290        assert!(
291            err.contains("scope_filter is supported on per-file rules only"),
292            "expected per-file-only message, got: {err}",
293        );
294        assert!(
295            err.contains("pair"),
296            "expected message to name the cross-file kind, got: {err}",
297        );
298    }
299}