alint_rules/
no_submodules.rs1use alint_core::{
21 Context, Error, FixSpec, Fixer, Level, PathsSpec, Result, Rule, RuleSpec, Scope, Violation,
22};
23
24use crate::fixers::FileRemoveFixer;
25
26#[derive(Debug)]
27pub struct NoSubmodulesRule {
28 id: String,
29 level: Level,
30 policy_url: Option<String>,
31 message: Option<String>,
32 scope: Scope,
33 fixer: Option<FileRemoveFixer>,
34}
35
36impl Rule for NoSubmodulesRule {
37 fn id(&self) -> &str {
38 &self.id
39 }
40 fn level(&self) -> Level {
41 self.level
42 }
43 fn policy_url(&self) -> Option<&str> {
44 self.policy_url.as_deref()
45 }
46
47 fn evaluate(&self, ctx: &Context<'_>) -> Result<Vec<Violation>> {
48 let mut violations = Vec::new();
49 for entry in ctx.index.files() {
50 if !self.scope.matches(&entry.path) {
51 continue;
52 }
53 let msg = self.message.clone().unwrap_or_else(|| {
54 "`.gitmodules` present — git submodules are forbidden".to_string()
55 });
56 violations.push(Violation::new(msg).with_path(&entry.path));
57 }
58 Ok(violations)
59 }
60
61 fn fixer(&self) -> Option<&dyn Fixer> {
62 self.fixer.as_ref().map(|f| f as &dyn Fixer)
63 }
64}
65
66pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
67 if spec.paths.is_some() {
68 return Err(Error::rule_config(
69 &spec.id,
70 "no_submodules does not accept a `paths` field; it always targets `.gitmodules` at \
71 the repo root. Use `file_absent` for more general patterns.",
72 ));
73 }
74 let fixer = match &spec.fix {
75 Some(FixSpec::FileRemove { .. }) => Some(FileRemoveFixer),
76 Some(other) => {
77 return Err(Error::rule_config(
78 &spec.id,
79 format!(
80 "fix.{} is not compatible with no_submodules",
81 other.op_name()
82 ),
83 ));
84 }
85 None => None,
86 };
87 let pinned = PathsSpec::Single(".gitmodules".to_string());
88 Ok(Box::new(NoSubmodulesRule {
89 id: spec.id.clone(),
90 level: spec.level,
91 policy_url: spec.policy_url.clone(),
92 message: spec.message.clone(),
93 scope: Scope::from_paths_spec(&pinned)?,
94 fixer,
95 }))
96}