|
| 1 | +use crate::diagnostics::CliDiagnostic; |
| 2 | +use crate::reporter::{Report, ReportConfig, ReportWriter}; |
| 3 | +use pgls_console::{Console, ConsoleExt, markup}; |
| 4 | +use pgls_diagnostics::display::SourceFile; |
| 5 | +use pgls_diagnostics::{Error, PrintDescription, Resource, Severity}; |
| 6 | +use serde::Serialize; |
| 7 | + |
| 8 | +pub(crate) struct JsonReportWriter { |
| 9 | + pub pretty: bool, |
| 10 | +} |
| 11 | + |
| 12 | +impl ReportWriter for JsonReportWriter { |
| 13 | + fn write( |
| 14 | + &mut self, |
| 15 | + console: &mut dyn Console, |
| 16 | + command_name: &str, |
| 17 | + report: &Report, |
| 18 | + config: &ReportConfig, |
| 19 | + ) -> Result<(), CliDiagnostic> { |
| 20 | + let diagnostics: Vec<_> = report |
| 21 | + .diagnostics |
| 22 | + .iter() |
| 23 | + .filter(|d| d.severity() >= config.diagnostic_level) |
| 24 | + .filter(|d| { |
| 25 | + if d.tags().is_verbose() { |
| 26 | + config.verbose |
| 27 | + } else { |
| 28 | + true |
| 29 | + } |
| 30 | + }) |
| 31 | + .map(to_json_report) |
| 32 | + .collect(); |
| 33 | + |
| 34 | + let summary = JsonSummary::from_report(report); |
| 35 | + |
| 36 | + let output = JsonOutput { |
| 37 | + summary, |
| 38 | + diagnostics, |
| 39 | + command: command_name.to_string(), |
| 40 | + }; |
| 41 | + |
| 42 | + let serialized = if self.pretty { |
| 43 | + serde_json::to_string_pretty(&output) |
| 44 | + } else { |
| 45 | + serde_json::to_string(&output) |
| 46 | + } |
| 47 | + .map_err(|e| CliDiagnostic::io_error(std::io::Error::other(e)))?; |
| 48 | + |
| 49 | + console.log(markup!({ serialized })); |
| 50 | + Ok(()) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +#[derive(Serialize)] |
| 55 | +struct JsonOutput { |
| 56 | + summary: JsonSummary, |
| 57 | + diagnostics: Vec<JsonDiagnostic>, |
| 58 | + command: String, |
| 59 | +} |
| 60 | + |
| 61 | +#[derive(Serialize)] |
| 62 | +#[serde(rename_all = "camelCase")] |
| 63 | +struct JsonSummary { |
| 64 | + changed: usize, |
| 65 | + unchanged: usize, |
| 66 | + duration: u128, |
| 67 | + errors: u32, |
| 68 | + warnings: u32, |
| 69 | + skipped: usize, |
| 70 | + skipped_diagnostics: u32, |
| 71 | +} |
| 72 | + |
| 73 | +impl JsonSummary { |
| 74 | + fn from_report(report: &Report) -> Self { |
| 75 | + let (changed, unchanged, skipped) = report |
| 76 | + .traversal |
| 77 | + .as_ref() |
| 78 | + .map(|t| (t.changed, t.unchanged, t.skipped)) |
| 79 | + .unwrap_or_default(); |
| 80 | + Self { |
| 81 | + changed, |
| 82 | + unchanged, |
| 83 | + duration: report.duration.as_nanos(), |
| 84 | + errors: report.errors, |
| 85 | + warnings: report.warnings, |
| 86 | + skipped, |
| 87 | + skipped_diagnostics: report.skipped_diagnostics, |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#[derive(Serialize)] |
| 93 | +struct JsonDiagnostic { |
| 94 | + severity: &'static str, |
| 95 | + message: String, |
| 96 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 97 | + category: Option<String>, |
| 98 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 99 | + location: Option<JsonLocation>, |
| 100 | +} |
| 101 | + |
| 102 | +#[derive(Serialize)] |
| 103 | +struct JsonLocation { |
| 104 | + path: String, |
| 105 | + start: JsonPosition, |
| 106 | + end: JsonPosition, |
| 107 | +} |
| 108 | + |
| 109 | +#[derive(Serialize)] |
| 110 | +struct JsonPosition { |
| 111 | + line: usize, |
| 112 | + column: usize, |
| 113 | +} |
| 114 | + |
| 115 | +fn to_json_report(diagnostic: &Error) -> JsonDiagnostic { |
| 116 | + let message = PrintDescription(diagnostic).to_string(); |
| 117 | + let category = diagnostic.category().map(|c| c.name().to_string()); |
| 118 | + let severity = match diagnostic.severity() { |
| 119 | + Severity::Hint => "hint", |
| 120 | + Severity::Information => "info", |
| 121 | + Severity::Warning => "warning", |
| 122 | + Severity::Error => "error", |
| 123 | + Severity::Fatal => "fatal", |
| 124 | + }; |
| 125 | + |
| 126 | + let location = to_location(diagnostic); |
| 127 | + |
| 128 | + JsonDiagnostic { |
| 129 | + severity, |
| 130 | + message, |
| 131 | + category, |
| 132 | + location, |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +fn to_location(diagnostic: &Error) -> Option<JsonLocation> { |
| 137 | + let loc = diagnostic.location(); |
| 138 | + let path = match loc.resource { |
| 139 | + Some(Resource::File(file)) => file.to_string(), |
| 140 | + _ => return None, |
| 141 | + }; |
| 142 | + |
| 143 | + match (loc.span, loc.source_code) { |
| 144 | + (Some(span), Some(source_code)) => { |
| 145 | + let source = SourceFile::new(source_code); |
| 146 | + let start = source.location(span.start()).ok()?; |
| 147 | + let end = source.location(span.end()).ok()?; |
| 148 | + Some(JsonLocation { |
| 149 | + path, |
| 150 | + start: JsonPosition { |
| 151 | + line: start.line_number.get(), |
| 152 | + column: start.column_number.get(), |
| 153 | + }, |
| 154 | + end: JsonPosition { |
| 155 | + line: end.line_number.get(), |
| 156 | + column: end.column_number.get(), |
| 157 | + }, |
| 158 | + }) |
| 159 | + } |
| 160 | + _ => Some(JsonLocation { |
| 161 | + path, |
| 162 | + start: JsonPosition { line: 0, column: 0 }, |
| 163 | + end: JsonPosition { line: 0, column: 0 }, |
| 164 | + }), |
| 165 | + } |
| 166 | +} |
0 commit comments