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