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