Skip to main content

sqrust_rules/convention/
comma_style.rs

1use sqrust_core::{Diagnostic, FileContext, Rule};
2
3pub struct CommaStyle;
4
5impl Rule for CommaStyle {
6    fn name(&self) -> &'static str {
7        "Convention/CommaStyle"
8    }
9
10    fn check(&self, ctx: &FileContext) -> Vec<Diagnostic> {
11        let mut has_trailing = false;
12        let mut first_leading_line: Option<usize> = None;
13
14        for (line_num, line) in ctx.lines() {
15            let trimmed = line.trim();
16            if trimmed.is_empty() {
17                continue;
18            }
19
20            let last_non_ws = line.trim_end().chars().last();
21            let first_non_ws = trimmed.chars().next();
22
23            let is_trailing = last_non_ws == Some(',');
24            let is_leading = first_non_ws == Some(',');
25
26            if is_trailing {
27                has_trailing = true;
28            }
29            if is_leading && first_leading_line.is_none() {
30                first_leading_line = Some(line_num);
31            }
32        }
33
34        if has_trailing {
35            if let Some(leading_line) = first_leading_line {
36                return vec![Diagnostic {
37                    rule: self.name(),
38                    message: "Inconsistent comma style: mix of leading and trailing commas"
39                        .to_string(),
40                    line: leading_line,
41                    col: 1,
42                }];
43            }
44        }
45
46        vec![]
47    }
48}