1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use crate::linter::SolidFile;
use crate::rules::types::*;
use crate::types::*;

// global
pub const RULE_ID: &str = "max-line-length";

// specific
const DEFAULT_LENGTH: usize = 120;
const DEFAULT_SEVERITY: Severity = Severity::ERROR;

pub struct MaxLineLength {
    max_len: usize,
    data: RuleEntry,
}

impl MaxLineLength {
    fn create_diag(&self, file: &SolidFile, line_idx: usize, line: &str) -> LintDiag {
        LintDiag {
            range: Range {
                start: Position {
                    line: line_idx,
                    character: self.max_len,
                },
                end: Position {
                    line: line_idx,
                    character: line.len(),
                },
            },
            id: RULE_ID.to_string(),
            message: format!(
                "Line length must be no more than {} but current length is {}",
                self.max_len,
                line.len()
            ),
            severity: self.data.severity,
            code: None,
            source: None,
            uri: file.path.clone(),
        }
    }
}

impl RuleType for MaxLineLength {
    fn diagnose(&self, file: &SolidFile, _files: &[SolidFile]) -> Vec<LintDiag> {
        let mut res = Vec::new();
        let mut line_idx = 1;

        for line in file.content.lines() {
            if line.len() > self.max_len {
                res.push(self.create_diag(file, line_idx, line));
            }
            line_idx += 1;
        }
        res
    }
}

impl MaxLineLength {
    pub(crate) fn create(data: RuleEntry) -> Box<dyn RuleType> {
        let mut max_line_length = DEFAULT_LENGTH;

        if let Some(data) = &data.data {
            let parsed: Result<usize, serde_json::Error> = serde_json::from_value(data.clone());
            match parsed {
                Ok(val) => max_line_length = val,
                Err(_) => {
                    eprintln!("{} rule : bad config data", RULE_ID);
                }
            }
        } else {
            eprintln!("{} rule : bad config data", RULE_ID);
        }
        let rule = MaxLineLength {
            max_len: max_line_length,
            data,
        };
        Box::new(rule)
    }

    pub(crate) fn create_default() -> RuleEntry {
        RuleEntry {
            id: RULE_ID.to_string(),
            severity: DEFAULT_SEVERITY,
            data: Some(DEFAULT_LENGTH.into()),
        }
    }
}