From 40867728c023a9f95f346671b51be68002cc7552 Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 21 Sep 2021 16:41:24 +0530 Subject: add suggestion to bool_comparison --- lib/src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) (limited to 'lib/src/lib.rs') 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 @@ mod lints; +mod make; + pub use lints::LINTS; use rnix::{SyntaxElement, SyntaxKind, TextRange}; -use std::default::Default; +use std::{default::Default, convert::Into}; pub trait Rule { fn validate(&self, node: &SyntaxElement) -> Option; @@ -12,32 +14,60 @@ pub trait Rule { pub struct Diagnostic { pub at: TextRange, pub message: String, + pub suggestion: Option, } impl Diagnostic { pub fn new(at: TextRange, message: String) -> Self { - Self { at, message } + 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>(at: TextRange, fix: E) -> Self { + Self { + at, + fix: fix.into() + } } } #[derive(Debug, Default)] pub struct Report { pub diagnostics: Vec, + pub note: &'static str } impl Report { - pub fn new() -> Self { - Self::default() + 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(&self) -> &str; - fn note(&self) -> &str; + 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; } -- cgit v1.2.3