alint_rules/
dir_absent.rs1use alint_core::{Context, Error, Level, PathsSpec, Result, Rule, RuleSpec, Scope, Violation};
4
5#[derive(Debug)]
6pub struct DirAbsentRule {
7 id: String,
8 level: Level,
9 policy_url: Option<String>,
10 message: Option<String>,
11 scope: Scope,
12 patterns: Vec<String>,
13 git_tracked_only: bool,
20}
21
22impl Rule for DirAbsentRule {
23 fn id(&self) -> &str {
24 &self.id
25 }
26 fn level(&self) -> Level {
27 self.level
28 }
29 fn policy_url(&self) -> Option<&str> {
30 self.policy_url.as_deref()
31 }
32 fn wants_git_tracked(&self) -> bool {
33 self.git_tracked_only
34 }
35
36 fn requires_full_index(&self) -> bool {
37 true
42 }
43
44 fn evaluate(&self, ctx: &Context<'_>) -> Result<Vec<Violation>> {
45 let mut violations = Vec::new();
46 for entry in ctx.index.dirs() {
47 if !self.scope.matches(&entry.path) {
48 continue;
49 }
50 if self.git_tracked_only && !ctx.dir_has_tracked_files(&entry.path) {
51 continue;
52 }
53 let msg = self.message.clone().unwrap_or_else(|| {
54 let tracked = if self.git_tracked_only {
55 " and has tracked content"
56 } else {
57 ""
58 };
59 format!(
60 "directory is forbidden (matches [{}]{tracked}): {}",
61 self.patterns.join(", "),
62 entry.path.display()
63 )
64 });
65 violations.push(Violation::new(msg).with_path(&entry.path));
66 }
67 Ok(violations)
68 }
69}
70
71pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
72 let Some(paths) = &spec.paths else {
73 return Err(Error::rule_config(
74 &spec.id,
75 "dir_absent requires a `paths` field",
76 ));
77 };
78 Ok(Box::new(DirAbsentRule {
79 id: spec.id.clone(),
80 level: spec.level,
81 policy_url: spec.policy_url.clone(),
82 message: spec.message.clone(),
83 scope: Scope::from_paths_spec(paths)?,
84 patterns: patterns_of(paths),
85 git_tracked_only: spec.git_tracked_only,
86 }))
87}
88
89fn patterns_of(spec: &PathsSpec) -> Vec<String> {
90 match spec {
91 PathsSpec::Single(s) => vec![s.clone()],
92 PathsSpec::Many(v) => v.clone(),
93 PathsSpec::IncludeExclude { include, .. } => include.clone(),
94 }
95}