diff options
author | Akshay <[email protected]> | 2021-10-24 08:54:52 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-10-24 08:54:52 +0100 |
commit | 5de0ba055cef7f2dc5451b1eaf0857deb77ae009 (patch) | |
tree | 2c9b0d9b6d90e071cbb9ebbbe2efcdf705e2375e /lib/src | |
parent | c79799c0e418c0c37e4076d653d64e6adaa3378b (diff) |
add support for json out
Diffstat (limited to 'lib/src')
-rw-r--r-- | lib/src/lib.rs | 44 |
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; | |||
6 | use rnix::{SyntaxElement, SyntaxKind, TextRange}; | 6 | use rnix::{SyntaxElement, SyntaxKind, TextRange}; |
7 | use std::{convert::Into, default::Default}; | 7 | use std::{convert::Into, default::Default}; |
8 | 8 | ||
9 | #[cfg(feature = "json-out")] | ||
10 | use 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))] | ||
11 | pub struct Report { | 15 | pub 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")] | ||
103 | impl 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")] | ||
148 | impl 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 |
124 | pub trait Rule { | 168 | pub trait Rule { |