aboutsummaryrefslogtreecommitdiff
path: root/lib/src/lib.rs
blob: 0f92d0dcdbe92af280a7dc763b66843d0f035929 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
mod lints;
mod make;

pub use lints::LINTS;

use rnix::{SyntaxElement, SyntaxKind, TextRange};
use std::{default::Default, convert::Into};

pub trait Rule {
    fn validate(&self, node: &SyntaxElement) -> Option<Report>;
}

#[derive(Debug)]
pub struct Diagnostic {
    pub at: TextRange,
    pub message: String,
    pub suggestion: Option<Suggestion>,
}

impl Diagnostic {
    pub fn new(at: TextRange, message: String) -> Self {
        Self { at, message, suggestion: None }
    }
    pub fn suggest(at: TextRange, message: String, suggestion: Suggestion) -> Self {
        Self { at, message, suggestion: Some(suggestion) }
    }
}

#[derive(Debug)]
pub struct Suggestion {
    pub at: TextRange,
    pub fix: SyntaxElement,
}

impl Suggestion {
    pub fn new<E: Into<SyntaxElement>>(at: TextRange, fix: E) -> Self {
        Self {
            at,
            fix: fix.into()
        }
    }
}

#[derive(Debug, Default)]
pub struct Report {
    pub diagnostics: Vec<Diagnostic>,
    pub note: &'static str
}

impl Report {
    pub fn new(note: &'static str) -> Self {
        Self {
            note,
            ..Default::default()
        }
    }
    pub fn diagnostic(mut self, at: TextRange, message: String) -> Self {
        self.diagnostics.push(Diagnostic::new(at, message));
        self
    }
    pub fn suggest(mut self, at: TextRange, message: String, suggestion: Suggestion) -> Self {
        self.diagnostics.push(Diagnostic::suggest(at, message, suggestion));
        self
    }

}

pub trait Metadata {
    fn name() -> &'static str where Self: Sized;
    fn note() -> &'static str where Self: Sized;
    fn match_with(&self, with: &SyntaxKind) -> bool;
    fn match_kind(&self) -> SyntaxKind;
}

pub trait Lint: Metadata + Rule + Send + Sync {}

#[macro_export]
macro_rules! lint_map {
    ($($s:ident),*,) => {
        lint_map!($($s),*);
    };
    ($($s:ident),*) => {
        use ::std::collections::HashMap;
        use ::rnix::SyntaxKind;
        $(
            mod $s;
        )*
        ::lazy_static::lazy_static! {
            pub static ref LINTS: HashMap<SyntaxKind, Vec<&'static Box<dyn $crate::Lint>>> = {
                let mut map = HashMap::new();
                $(
                    {
                        let temp_lint = &*$s::LINT;
                        let temp_match = temp_lint.match_kind();
                        map.entry(temp_match)
                           .and_modify(|v: &mut Vec<_>| v.push(temp_lint))
                           .or_insert_with(|| vec![temp_lint]);
                    }
                )*
                map
            };
        }
    }
}