Skip to main content

alint_rules/
dir_exists.rs

1//! `dir_exists` — at least one directory matching `paths` must exist.
2
3use alint_core::{Context, Error, Level, PathsSpec, Result, Rule, RuleSpec, Scope, Violation};
4
5#[derive(Debug)]
6pub struct DirExistsRule {
7    id: String,
8    level: Level,
9    policy_url: Option<String>,
10    message: Option<String>,
11    scope: Scope,
12    patterns: Vec<String>,
13    /// When `true`, only consider directories that contain at
14    /// least one git-tracked file. Outside a git repo the
15    /// tracked set is empty, so the rule reports the "missing"
16    /// violation as if no matching directory existed.
17    git_tracked_only: bool,
18}
19
20impl Rule for DirExistsRule {
21    fn id(&self) -> &str {
22        &self.id
23    }
24    fn level(&self) -> Level {
25        self.level
26    }
27    fn policy_url(&self) -> Option<&str> {
28        self.policy_url.as_deref()
29    }
30    fn wants_git_tracked(&self) -> bool {
31        self.git_tracked_only
32    }
33
34    fn requires_full_index(&self) -> bool {
35        // Aggregate verdict over the whole tree. Note we
36        // deliberately don't expose `path_scope` here: directory
37        // scopes (e.g. `src/foo`) don't naturally intersect a
38        // changed-set built from file paths (`src/foo/main.rs`),
39        // so the engine evaluates dir-existence rules on every
40        // `--changed` run. Cheap (one O(N) scan) and correct.
41        true
42    }
43
44    fn evaluate(&self, ctx: &Context<'_>) -> Result<Vec<Violation>> {
45        let found = ctx.index.dirs().any(|entry| {
46            if !self.scope.matches(&entry.path) {
47                return false;
48            }
49            if self.git_tracked_only && !ctx.dir_has_tracked_files(&entry.path) {
50                return false;
51            }
52            true
53        });
54        if found {
55            Ok(Vec::new())
56        } else {
57            let msg = self.message.clone().unwrap_or_else(|| {
58                let tracked = if self.git_tracked_only {
59                    " (with tracked content)"
60                } else {
61                    ""
62                };
63                format!(
64                    "expected a directory matching [{}]{tracked}",
65                    self.patterns.join(", ")
66                )
67            });
68            Ok(vec![Violation::new(msg)])
69        }
70    }
71}
72
73pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
74    let Some(paths) = &spec.paths else {
75        return Err(Error::rule_config(
76            &spec.id,
77            "dir_exists requires a `paths` field",
78        ));
79    };
80    Ok(Box::new(DirExistsRule {
81        id: spec.id.clone(),
82        level: spec.level,
83        policy_url: spec.policy_url.clone(),
84        message: spec.message.clone(),
85        scope: Scope::from_paths_spec(paths)?,
86        patterns: patterns_of(paths),
87        git_tracked_only: spec.git_tracked_only,
88    }))
89}
90
91fn patterns_of(spec: &PathsSpec) -> Vec<String> {
92    match spec {
93        PathsSpec::Single(s) => vec![s.clone()],
94        PathsSpec::Many(v) => v.clone(),
95        PathsSpec::IncludeExclude { include, .. } => include.clone(),
96    }
97}