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.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/src/lib.rs b/lib/src/lib.rs
index d8a3859..bde039f 100644
--- a/lib/src/lib.rs
+++ b/lib/src/lib.rs
@@ -6,8 +6,12 @@ pub use lints::LINTS;
6use rnix::{SyntaxElement, SyntaxKind, TextRange}; 6use rnix::{SyntaxElement, SyntaxKind, TextRange};
7use std::{convert::Into, default::Default}; 7use std::{convert::Into, default::Default};
8 8
9#[cfg(feature = "json-out")]
10use serde::{Serialize, ser::{SerializeStruct, Serializer}};
11
9/// Report generated by a lint 12/// Report generated by a lint
10#[derive(Debug, Default)] 13#[derive(Debug, Default)]
14#[cfg_attr(feature = "json-out", derive(Serialize))]
11pub struct Report { 15pub struct Report {
12 /// General information about this lint and where it applies. 16 /// General information about this lint and where it applies.
13 pub note: &'static str, 17 pub note: &'static str,
@@ -95,6 +99,27 @@ impl Diagnostic {
95 } 99 }
96} 100}
97 101
102#[cfg(feature = "json-out")]
103impl Serialize for Diagnostic {
104 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
105 where
106 S: Serializer,
107 {
108 let mut s = serializer.serialize_struct("Diagnostic", 3)?;
109 let at = {
110 let start = usize::from(self.at.start());
111 let end = usize::from(self.at.end());
112 (start, end)
113 };
114 s.serialize_field("at", &at)?;
115 s.serialize_field("message", &self.message)?;
116 if let Some(suggestion) = &self.suggestion {
117 s.serialize_field("suggestion", suggestion)?;
118 }
119 s.end()
120 }
121}
122
98/// Suggested fix for a diagnostic, the fix is provided as a syntax element. 123/// Suggested fix for a diagnostic, the fix is provided as a syntax element.
99/// Look at `make.rs` to construct fixes. 124/// Look at `make.rs` to construct fixes.
100#[derive(Debug)] 125#[derive(Debug)]
@@ -119,6 +144,25 @@ impl Suggestion {
119 } 144 }
120} 145}
121 146
147#[cfg(feature = "json-out")]
148impl Serialize for Suggestion {
149 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
150 where
151 S: Serializer,
152 {
153 let mut s = serializer.serialize_struct("Suggestion", 2)?;
154 let at = {
155 let start = usize::from(self.at.start());
156 let end = usize::from(self.at.end());
157 (start, end)
158 };
159 let fix = self.fix.to_string();
160 s.serialize_field("at", &at)?;
161 s.serialize_field("fix", &fix)?;
162 s.end()
163 }
164}
165
122/// Lint logic is defined via this trait. Do not implement manually, 166/// Lint logic is defined via this trait. Do not implement manually,
123/// look at the `lint` attribute macro instead for implementing rules 167/// look at the `lint` attribute macro instead for implementing rules
124pub trait Rule { 168pub trait Rule {