alint_rules/
file_min_lines.rs1use std::path::Path;
19
20use alint_core::{Context, Error, Level, PerFileRule, Result, Rule, RuleSpec, Scope, Violation};
21use serde::Deserialize;
22
23#[derive(Debug, Deserialize)]
24struct Options {
25 min_lines: u64,
26}
27
28#[derive(Debug)]
29pub struct FileMinLinesRule {
30 id: String,
31 level: Level,
32 policy_url: Option<String>,
33 message: Option<String>,
34 scope: Scope,
35 min_lines: u64,
36}
37
38impl Rule for FileMinLinesRule {
39 fn id(&self) -> &str {
40 &self.id
41 }
42 fn level(&self) -> Level {
43 self.level
44 }
45 fn policy_url(&self) -> Option<&str> {
46 self.policy_url.as_deref()
47 }
48
49 fn evaluate(&self, ctx: &Context<'_>) -> Result<Vec<Violation>> {
50 let mut violations = Vec::new();
51 for entry in ctx.index.files() {
52 if !self.scope.matches(&entry.path, ctx.index) {
53 continue;
54 }
55 let full = ctx.root.join(&entry.path);
56 let Ok(bytes) = std::fs::read(&full) else {
57 continue;
62 };
63 violations.extend(self.evaluate_file(ctx, &entry.path, &bytes)?);
64 }
65 Ok(violations)
66 }
67
68 fn as_per_file(&self) -> Option<&dyn PerFileRule> {
69 Some(self)
70 }
71}
72
73impl PerFileRule for FileMinLinesRule {
74 fn path_scope(&self) -> &Scope {
75 &self.scope
76 }
77
78 fn evaluate_file(
79 &self,
80 _ctx: &Context<'_>,
81 path: &Path,
82 bytes: &[u8],
83 ) -> Result<Vec<Violation>> {
84 let lines = count_lines(bytes);
85 if lines >= self.min_lines {
86 return Ok(Vec::new());
87 }
88 let msg = self.message.clone().unwrap_or_else(|| {
89 format!(
90 "file has {} line(s); at least {} required",
91 lines, self.min_lines,
92 )
93 });
94 Ok(vec![
95 Violation::new(msg).with_path(std::sync::Arc::<Path>::from(path)),
96 ])
97 }
98}
99
100fn count_lines(bytes: &[u8]) -> u64 {
105 if bytes.is_empty() {
106 return 0;
107 }
108 #[allow(clippy::naive_bytecount)]
112 let newlines = bytes.iter().filter(|&&b| b == b'\n').count() as u64;
113 let trailing_unterminated = u64::from(!bytes.ends_with(b"\n"));
114 newlines + trailing_unterminated
115}
116
117pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
118 let Some(_paths) = &spec.paths else {
119 return Err(Error::rule_config(
120 &spec.id,
121 "file_min_lines requires a `paths` field",
122 ));
123 };
124 let opts: Options = spec
125 .deserialize_options()
126 .map_err(|e| Error::rule_config(&spec.id, format!("invalid options: {e}")))?;
127 Ok(Box::new(FileMinLinesRule {
128 id: spec.id.clone(),
129 level: spec.level,
130 policy_url: spec.policy_url.clone(),
131 message: spec.message.clone(),
132 scope: Scope::from_spec(spec)?,
133 min_lines: opts.min_lines,
134 }))
135}
136
137#[cfg(test)]
138mod tests {
139 use super::count_lines;
140
141 #[test]
142 fn empty_file_has_zero_lines() {
143 assert_eq!(count_lines(b""), 0);
144 }
145
146 #[test]
147 fn content_with_trailing_newline_counts_each_line() {
148 assert_eq!(count_lines(b"a\n"), 1);
149 assert_eq!(count_lines(b"a\nb\n"), 2);
150 assert_eq!(count_lines(b"a\nb\nc\n"), 3);
151 }
152
153 #[test]
154 fn content_without_trailing_newline_adds_one_for_tail() {
155 assert_eq!(count_lines(b"a"), 1);
156 assert_eq!(count_lines(b"a\nb"), 2);
157 }
158
159 #[test]
160 fn blank_lines_count() {
161 assert_eq!(count_lines(b"a\n\nb\n"), 3);
162 assert_eq!(count_lines(b"\n\n"), 2);
163 }
164}