aboutsummaryrefslogtreecommitdiff
path: root/bin/src/traits.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-10-24 08:54:52 +0100
committerAkshay <[email protected]>2021-10-24 08:54:52 +0100
commit5de0ba055cef7f2dc5451b1eaf0857deb77ae009 (patch)
tree2c9b0d9b6d90e071cbb9ebbbe2efcdf705e2375e /bin/src/traits.rs
parentc79799c0e418c0c37e4076d653d64e6adaa3378b (diff)
add support for json out
Diffstat (limited to 'bin/src/traits.rs')
-rw-r--r--bin/src/traits.rs45
1 files changed, 43 insertions, 2 deletions
diff --git a/bin/src/traits.rs b/bin/src/traits.rs
index 5cf7208..a0e40d4 100644
--- a/bin/src/traits.rs
+++ b/bin/src/traits.rs
@@ -32,9 +32,10 @@ where
32 format: OutFormat, 32 format: OutFormat,
33 ) -> io::Result<()> { 33 ) -> io::Result<()> {
34 match format { 34 match format {
35 #[cfg(feature = "json")]
36 OutFormat::Json => json::write_json(self, lint_result, vfs),
35 OutFormat::StdErr => write_stderr(self, lint_result, vfs), 37 OutFormat::StdErr => write_stderr(self, lint_result, vfs),
36 OutFormat::Errfmt => write_errfmt(self, lint_result, vfs), 38 OutFormat::Errfmt => write_errfmt(self, lint_result, vfs),
37 _ => Ok(()),
38 } 39 }
39 } 40 }
40} 41}
@@ -84,7 +85,11 @@ fn write_stderr<T: Write>(
84 Ok(()) 85 Ok(())
85} 86}
86 87
87fn write_errfmt<T: Write>(writer: &mut T, lint_result: &LintResult, vfs: &ReadOnlyVfs) -> io::Result<()> { 88fn write_errfmt<T: Write>(
89 writer: &mut T,
90 lint_result: &LintResult,
91 vfs: &ReadOnlyVfs,
92) -> io::Result<()> {
88 let file_id = lint_result.file_id; 93 let file_id = lint_result.file_id;
89 let src = str::from_utf8(vfs.get(file_id)).unwrap(); 94 let src = str::from_utf8(vfs.get(file_id)).unwrap();
90 let path = vfs.file_path(file_id); 95 let path = vfs.file_path(file_id);
@@ -107,6 +112,42 @@ fn write_errfmt<T: Write>(writer: &mut T, lint_result: &LintResult, vfs: &ReadOn
107 Ok(()) 112 Ok(())
108} 113}
109 114
115#[cfg(feature = "json")]
116mod 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 }
149}
150
110fn line(at: TextRange, src: &str) -> usize { 151fn line(at: TextRange, src: &str) -> usize {
111 let at = at.start().into(); 152 let at = at.start().into();
112 src[..at].chars().filter(|&c| c == '\n').count() + 1 153 src[..at].chars().filter(|&c| c == '\n').count() + 1