alint_rules/
file_min_size.rs1use alint_core::{Context, Error, Level, Result, Rule, RuleSpec, Scope, Violation};
15use serde::Deserialize;
16
17#[derive(Debug, Deserialize)]
18struct Options {
19 min_bytes: u64,
20}
21
22#[derive(Debug)]
23pub struct FileMinSizeRule {
24 id: String,
25 level: Level,
26 policy_url: Option<String>,
27 message: Option<String>,
28 scope: Scope,
29 min_bytes: u64,
30}
31
32impl Rule for FileMinSizeRule {
33 fn id(&self) -> &str {
34 &self.id
35 }
36 fn level(&self) -> Level {
37 self.level
38 }
39 fn policy_url(&self) -> Option<&str> {
40 self.policy_url.as_deref()
41 }
42
43 fn evaluate(&self, ctx: &Context<'_>) -> Result<Vec<Violation>> {
44 let mut violations = Vec::new();
45 for entry in ctx.index.files() {
46 if !self.scope.matches(&entry.path) {
47 continue;
48 }
49 if entry.size < self.min_bytes {
50 let msg = self.message.clone().unwrap_or_else(|| {
51 format!(
52 "file below {} byte(s) (actual: {})",
53 self.min_bytes, entry.size,
54 )
55 });
56 violations.push(Violation::new(msg).with_path(&entry.path));
57 }
58 }
59 Ok(violations)
60 }
61}
62
63pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
64 let Some(paths) = &spec.paths else {
65 return Err(Error::rule_config(
66 &spec.id,
67 "file_min_size requires a `paths` field",
68 ));
69 };
70 let opts: Options = spec
71 .deserialize_options()
72 .map_err(|e| Error::rule_config(&spec.id, format!("invalid options: {e}")))?;
73 Ok(Box::new(FileMinSizeRule {
74 id: spec.id.clone(),
75 level: spec.level,
76 policy_url: spec.policy_url.clone(),
77 message: spec.message.clone(),
78 scope: Scope::from_paths_spec(paths)?,
79 min_bytes: opts.min_bytes,
80 }))
81}