Lints and suggestions for the Nix programming language
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

add support for json out

Akshay 5de0ba05 c79799c0

+156 -2
+47
Cargo.lock
··· 170 170 ] 171 171 172 172 [[package]] 173 + name = "itoa" 174 + version = "0.4.8" 175 + source = "registry+https://github.com/rust-lang/crates.io-index" 176 + checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" 177 + 178 + [[package]] 173 179 name = "lazy_static" 174 180 version = "1.4.0" 175 181 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 184 190 "macros", 185 191 "rnix", 186 192 "rowan", 193 + "serde", 194 + "serde_json", 187 195 ] 188 196 189 197 [[package]] ··· 330 338 checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" 331 339 332 340 [[package]] 341 + name = "ryu" 342 + version = "1.0.5" 343 + source = "registry+https://github.com/rust-lang/crates.io-index" 344 + checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" 345 + 346 + [[package]] 347 + name = "serde" 348 + version = "1.0.130" 349 + source = "registry+https://github.com/rust-lang/crates.io-index" 350 + checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913" 351 + dependencies = [ 352 + "serde_derive", 353 + ] 354 + 355 + [[package]] 356 + name = "serde_derive" 357 + version = "1.0.130" 358 + source = "registry+https://github.com/rust-lang/crates.io-index" 359 + checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b" 360 + dependencies = [ 361 + "proc-macro2", 362 + "quote", 363 + "syn", 364 + ] 365 + 366 + [[package]] 367 + name = "serde_json" 368 + version = "1.0.68" 369 + source = "registry+https://github.com/rust-lang/crates.io-index" 370 + checksum = "0f690853975602e1bfe1ccbf50504d67174e3bcf340f23b5ea9992e0587a52d8" 371 + dependencies = [ 372 + "itoa", 373 + "ryu", 374 + "serde", 375 + ] 376 + 377 + [[package]] 333 378 name = "similar" 334 379 version = "2.1.0" 335 380 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 350 395 "globset", 351 396 "lib", 352 397 "rnix", 398 + "serde", 399 + "serde_json", 353 400 "similar", 354 401 "thiserror", 355 402 "vfs",
+12
bin/Cargo.toml
··· 14 14 similar = "2.1.0" 15 15 vfs = { path = "../vfs" } 16 16 lib = { path = "../lib" } 17 + 18 + [dependencies.serde_json] 19 + version = "1.0.68" 20 + optional = true 21 + 22 + [dependencies.serde] 23 + version = "1.0.68" 24 + features = [ "derive" ] 25 + optional = true 26 + 27 + [features] 28 + json = [ "lib/json-out", "serde_json", "serde" ]
+43 -2
bin/src/traits.rs
··· 32 32 format: OutFormat, 33 33 ) -> io::Result<()> { 34 34 match format { 35 + #[cfg(feature = "json")] 36 + OutFormat::Json => json::write_json(self, lint_result, vfs), 35 37 OutFormat::StdErr => write_stderr(self, lint_result, vfs), 36 38 OutFormat::Errfmt => write_errfmt(self, lint_result, vfs), 37 - _ => Ok(()), 38 39 } 39 40 } 40 41 } ··· 84 85 Ok(()) 85 86 } 86 87 87 - fn write_errfmt<T: Write>(writer: &mut T, lint_result: &LintResult, vfs: &ReadOnlyVfs) -> io::Result<()> { 88 + fn write_errfmt<T: Write>( 89 + writer: &mut T, 90 + lint_result: &LintResult, 91 + vfs: &ReadOnlyVfs, 92 + ) -> io::Result<()> { 88 93 let file_id = lint_result.file_id; 89 94 let src = str::from_utf8(vfs.get(file_id)).unwrap(); 90 95 let path = vfs.file_path(file_id); ··· 105 110 } 106 111 } 107 112 Ok(()) 113 + } 114 + 115 + #[cfg(feature = "json")] 116 + mod json { 117 + use crate::lint::LintResult; 118 + use std::io::{self, Write}; 119 + use vfs::ReadOnlyVfs; 120 + 121 + use lib::Report; 122 + use serde::Serialize; 123 + use serde_json; 124 + 125 + #[derive(Serialize)] 126 + struct JsonReport<'μ> { 127 + file: &'μ std::path::Path, 128 + report: Vec<&'μ Report>, 129 + } 130 + 131 + pub fn write_json<T: Write>( 132 + writer: &mut T, 133 + lint_result: &LintResult, 134 + vfs: &ReadOnlyVfs, 135 + ) -> io::Result<()> { 136 + let file_id = lint_result.file_id; 137 + let path = vfs.file_path(file_id); 138 + writeln!( 139 + writer, 140 + "{}", 141 + serde_json::to_string_pretty(&JsonReport { 142 + file: path, 143 + report: lint_result.reports.iter().collect::<Vec<_>>() 144 + }) 145 + .unwrap() 146 + )?; 147 + Ok(()) 148 + } 108 149 } 109 150 110 151 fn line(at: TextRange, src: &str) -> usize {
+10
lib/Cargo.toml
··· 11 11 macros = { path = "../macros" } 12 12 lazy_static = "1.0" 13 13 rowan = "0.12.5" 14 + serde_json = { version = "1.0.68", optional = true } 15 + 16 + [dependencies.serde] 17 + version = "1.0.130" 18 + features = [ "derive" ] 19 + optional = true 20 + 21 + [features] 22 + default = [] 23 + json-out = [ "serde", "serde_json" ]
+44
lib/src/lib.rs
··· 6 6 use rnix::{SyntaxElement, SyntaxKind, TextRange}; 7 7 use std::{convert::Into, default::Default}; 8 8 9 + #[cfg(feature = "json-out")] 10 + use serde::{Serialize, ser::{SerializeStruct, Serializer}}; 11 + 9 12 /// Report generated by a lint 10 13 #[derive(Debug, Default)] 14 + #[cfg_attr(feature = "json-out", derive(Serialize))] 11 15 pub struct Report { 12 16 /// General information about this lint and where it applies. 13 17 pub note: &'static str, ··· 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 123 /// Suggested fix for a diagnostic, the fix is provided as a syntax element. 99 124 /// Look at `make.rs` to construct fixes. 100 125 #[derive(Debug)] ··· 116 141 let start = usize::from(self.at.start()); 117 142 let end = usize::from(self.at.end()); 118 143 src.replace_range(start..end, &self.fix.to_string()) 144 + } 145 + } 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() 119 163 } 120 164 } 121 165