aboutsummaryrefslogtreecommitdiff
path: root/lib/src/lib.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-09-21 12:11:24 +0100
committerAkshay <[email protected]>2021-09-21 12:11:24 +0100
commit40867728c023a9f95f346671b51be68002cc7552 (patch)
treee856327b0d42d9a4740c460a5efb633f8a73f3d9 /lib/src/lib.rs
parentc91c2ae1398e4f567ee5e4c50a1da35a9b65919b (diff)
add suggestion to bool_comparison
Diffstat (limited to 'lib/src/lib.rs')
-rw-r--r--lib/src/lib.rs42
1 files changed, 36 insertions, 6 deletions
diff --git a/lib/src/lib.rs b/lib/src/lib.rs
index c06f02d..0f92d0d 100644
--- a/lib/src/lib.rs
+++ b/lib/src/lib.rs
@@ -1,8 +1,10 @@
1mod lints; 1mod lints;
2mod make;
3
2pub use lints::LINTS; 4pub use lints::LINTS;
3 5
4use rnix::{SyntaxElement, SyntaxKind, TextRange}; 6use rnix::{SyntaxElement, SyntaxKind, TextRange};
5use std::default::Default; 7use std::{default::Default, convert::Into};
6 8
7pub trait Rule { 9pub trait Rule {
8 fn validate(&self, node: &SyntaxElement) -> Option<Report>; 10 fn validate(&self, node: &SyntaxElement) -> Option<Report>;
@@ -12,32 +14,60 @@ pub trait Rule {
12pub struct Diagnostic { 14pub struct Diagnostic {
13 pub at: TextRange, 15 pub at: TextRange,
14 pub message: String, 16 pub message: String,
17 pub suggestion: Option<Suggestion>,
15} 18}
16 19
17impl Diagnostic { 20impl Diagnostic {
18 pub fn new(at: TextRange, message: String) -> Self { 21 pub fn new(at: TextRange, message: String) -> Self {
19 Self { at, message } 22 Self { at, message, suggestion: None }
23 }
24 pub fn suggest(at: TextRange, message: String, suggestion: Suggestion) -> Self {
25 Self { at, message, suggestion: Some(suggestion) }
26 }
27}
28
29#[derive(Debug)]
30pub struct Suggestion {
31 pub at: TextRange,
32 pub fix: SyntaxElement,
33}
34
35impl Suggestion {
36 pub fn new<E: Into<SyntaxElement>>(at: TextRange, fix: E) -> Self {
37 Self {
38 at,
39 fix: fix.into()
40 }
20 } 41 }
21} 42}
22 43
23#[derive(Debug, Default)] 44#[derive(Debug, Default)]
24pub struct Report { 45pub struct Report {
25 pub diagnostics: Vec<Diagnostic>, 46 pub diagnostics: Vec<Diagnostic>,
47 pub note: &'static str
26} 48}
27 49
28impl Report { 50impl Report {
29 pub fn new() -> Self { 51 pub fn new(note: &'static str) -> Self {
30 Self::default() 52 Self {
53 note,
54 ..Default::default()
55 }
31 } 56 }
32 pub fn diagnostic(mut self, at: TextRange, message: String) -> Self { 57 pub fn diagnostic(mut self, at: TextRange, message: String) -> Self {
33 self.diagnostics.push(Diagnostic::new(at, message)); 58 self.diagnostics.push(Diagnostic::new(at, message));
34 self 59 self
35 } 60 }
61 pub fn suggest(mut self, at: TextRange, message: String, suggestion: Suggestion) -> Self {
62 self.diagnostics.push(Diagnostic::suggest(at, message, suggestion));
63 self
64 }
65
36} 66}
37 67
38pub trait Metadata { 68pub trait Metadata {
39 fn name(&self) -> &str; 69 fn name() -> &'static str where Self: Sized;
40 fn note(&self) -> &str; 70 fn note() -> &'static str where Self: Sized;
41 fn match_with(&self, with: &SyntaxKind) -> bool; 71 fn match_with(&self, with: &SyntaxKind) -> bool;
42 fn match_kind(&self) -> SyntaxKind; 72 fn match_kind(&self) -> SyntaxKind;
43} 73}