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