aboutsummaryrefslogtreecommitdiff
path: root/lib/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/src/lib.rs')
-rw-r--r--lib/src/lib.rs45
1 files changed, 41 insertions, 4 deletions
diff --git a/lib/src/lib.rs b/lib/src/lib.rs
index ec96f50..196cbf8 100644
--- a/lib/src/lib.rs
+++ b/lib/src/lib.rs
@@ -4,7 +4,7 @@ mod make;
4 4
5pub use lints::LINTS; 5pub use lints::LINTS;
6 6
7use rnix::{SyntaxElement, SyntaxKind, TextRange}; 7use rnix::{parser::ParseError, SyntaxElement, SyntaxKind, TextRange};
8use std::{convert::Into, default::Default}; 8use std::{convert::Into, default::Default};
9 9
10#[cfg(feature = "json-out")] 10#[cfg(feature = "json-out")]
@@ -13,10 +13,18 @@ use serde::{
13 Serialize, 13 Serialize,
14}; 14};
15 15
16#[derive(Debug)]
16#[cfg_attr(feature = "json-out", derive(Serialize))] 17#[cfg_attr(feature = "json-out", derive(Serialize))]
17pub struct Position { 18pub enum Severity {
18 line: usize, 19 Warn,
19 column: usize, 20 Error,
21 Hint,
22}
23
24impl Default for Severity {
25 fn default() -> Self {
26 Self::Warn
27 }
20} 28}
21 29
22/// Report generated by a lint 30/// Report generated by a lint
@@ -27,6 +35,8 @@ pub struct Report {
27 pub note: &'static str, 35 pub note: &'static str,
28 /// An error code to uniquely identify this lint 36 /// An error code to uniquely identify this lint
29 pub code: u32, 37 pub code: u32,
38 /// Report severity level
39 pub severity: Severity,
30 /// Collection of diagnostics raised by this lint 40 /// Collection of diagnostics raised by this lint
31 pub diagnostics: Vec<Diagnostic>, 41 pub diagnostics: Vec<Diagnostic>,
32} 42}
@@ -56,6 +66,11 @@ impl Report {
56 .push(Diagnostic::suggest(at, message, suggestion)); 66 .push(Diagnostic::suggest(at, message, suggestion));
57 self 67 self
58 } 68 }
69 /// Set severity level
70 pub fn severity(mut self, severity: Severity) -> Self {
71 self.severity = severity;
72 self
73 }
59 /// A range that encompasses all the suggestions provided in this report 74 /// A range that encompasses all the suggestions provided in this report
60 pub fn total_suggestion_range(&self) -> Option<TextRange> { 75 pub fn total_suggestion_range(&self) -> Option<TextRange> {
61 self.diagnostics 76 self.diagnostics
@@ -80,6 +95,28 @@ impl Report {
80 d.apply(src); 95 d.apply(src);
81 } 96 }
82 } 97 }
98 /// Create a report out of a parse error
99 pub fn from_parse_err(err: ParseError) -> Self {
100 let at = match err {
101 ParseError::Unexpected(at) => at,
102 ParseError::UnexpectedExtra(at) => at,
103 ParseError::UnexpectedWanted(_, at, _) => at,
104 ParseError::UnexpectedDoubleBind(at) => at,
105 ParseError::UnexpectedEOF | ParseError::UnexpectedEOFWanted(_) => {
106 TextRange::empty(0u32.into())
107 }
108 _ => panic!("report a bug, pepper forgot to handle a parse error"),
109 };
110 let mut message = err.to_string();
111 message
112 .as_mut_str()
113 .get_mut(0..1)
114 .unwrap()
115 .make_ascii_uppercase();
116 Self::new("Syntax error", 0)
117 .diagnostic(at, message)
118 .severity(Severity::Error)
119 }
83} 120}
84 121
85/// Mapping from a bytespan to an error message. 122/// Mapping from a bytespan to an error message.