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