alint_rules/
line_max_width.rs1use std::path::Path;
13
14use alint_core::{
15 Context, Error, Level, PerFileRule, Result, Rule, RuleSpec, Scope, ScopeFilter, Violation,
16};
17use serde::Deserialize;
18
19#[derive(Debug, Deserialize)]
20#[serde(deny_unknown_fields)]
21struct Options {
22 max_width: usize,
23}
24
25#[derive(Debug)]
26pub struct LineMaxWidthRule {
27 id: String,
28 level: Level,
29 policy_url: Option<String>,
30 message: Option<String>,
31 scope: Scope,
32 scope_filter: Option<ScopeFilter>,
33 max_width: usize,
34}
35
36impl Rule for LineMaxWidthRule {
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 if let Some(filter) = &self.scope_filter
54 && !filter.matches(&entry.path, ctx.index)
55 {
56 continue;
57 }
58 let full = ctx.root.join(&entry.path);
59 let Ok(bytes) = std::fs::read(&full) else {
60 continue;
61 };
62 violations.extend(self.evaluate_file(ctx, &entry.path, &bytes)?);
63 }
64 Ok(violations)
65 }
66
67 fn as_per_file(&self) -> Option<&dyn PerFileRule> {
68 Some(self)
69 }
70
71 fn scope_filter(&self) -> Option<&ScopeFilter> {
72 self.scope_filter.as_ref()
73 }
74}
75
76impl PerFileRule for LineMaxWidthRule {
77 fn path_scope(&self) -> &Scope {
78 &self.scope
79 }
80
81 fn evaluate_file(
82 &self,
83 _ctx: &Context<'_>,
84 path: &Path,
85 bytes: &[u8],
86 ) -> Result<Vec<Violation>> {
87 let Ok(text) = std::str::from_utf8(bytes) else {
91 return Ok(Vec::new());
92 };
93 let Some((line_no, width)) = first_overlong_line(text, self.max_width) else {
94 return Ok(Vec::new());
95 };
96 let msg = self.message.clone().unwrap_or_else(|| {
97 format!(
98 "line {line_no} is {width} chars wide; max is {}",
99 self.max_width
100 )
101 });
102 Ok(vec![
103 Violation::new(msg)
104 .with_path(std::sync::Arc::<Path>::from(path))
105 .with_location(line_no, self.max_width + 1),
106 ])
107 }
108}
109
110fn first_overlong_line(text: &str, max_width: usize) -> Option<(usize, usize)> {
111 for (idx, line) in text.split('\n').enumerate() {
112 let trimmed = line.strip_suffix('\r').unwrap_or(line);
113 let width = trimmed.chars().count();
114 if width > max_width {
115 return Some((idx + 1, width));
116 }
117 }
118 None
119}
120
121pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
122 let paths = spec
123 .paths
124 .as_ref()
125 .ok_or_else(|| Error::rule_config(&spec.id, "line_max_width requires a `paths` field"))?;
126 let opts: Options = spec
127 .deserialize_options()
128 .map_err(|e| Error::rule_config(&spec.id, format!("invalid options: {e}")))?;
129 if opts.max_width == 0 {
130 return Err(Error::rule_config(
131 &spec.id,
132 "line_max_width `max_width` must be > 0",
133 ));
134 }
135 if spec.fix.is_some() {
136 return Err(Error::rule_config(
137 &spec.id,
138 "line_max_width has no fix op — truncation is unsafe",
139 ));
140 }
141 Ok(Box::new(LineMaxWidthRule {
142 id: spec.id.clone(),
143 level: spec.level,
144 policy_url: spec.policy_url.clone(),
145 message: spec.message.clone(),
146 scope: Scope::from_paths_spec(paths)?,
147 scope_filter: spec.parse_scope_filter()?,
148 max_width: opts.max_width,
149 }))
150}
151
152#[cfg(test)]
153mod tests {
154 use super::*;
155
156 #[test]
157 fn short_file_is_ok() {
158 assert_eq!(first_overlong_line("hi\nthere\n", 10), None);
159 }
160
161 #[test]
162 fn flags_first_overlong_line() {
163 let txt = "short\nway too looooong for ten\nok\n";
164 assert_eq!(first_overlong_line(txt, 10), Some((2, 24)));
166 }
167
168 #[test]
169 fn width_exactly_at_limit_is_ok() {
170 assert_eq!(first_overlong_line("0123456789\n", 10), None);
171 }
172
173 #[test]
174 fn crlf_is_stripped_before_counting() {
175 assert_eq!(first_overlong_line("hi\r\n", 2), None);
177 }
178
179 #[test]
180 fn counts_unicode_scalar_values_not_bytes() {
181 assert_eq!(first_overlong_line("☃☃☃\n", 3), None);
183 assert_eq!(first_overlong_line("☃☃☃\n", 2), Some((1, 3)));
185 }
186}