use super::Renderer;
use crate::{Diagnostic, ExportDiagnostic, ExportPolicy};
const DIAGNOSTIC_SERIALIZATION_FALLBACK: &str = r#"{
"diagprint_error": "diagnostic JSON serialization failed"
}"#;
const REPORT_SERIALIZATION_FALLBACK: &str = r#"[
{
"diagprint_error": "diagnostic report JSON serialization failed"
}
]"#;
#[derive(Debug, Default, Clone, Copy)]
pub struct JsonRenderer;
impl JsonRenderer {
pub fn try_render_with_policy(
&self,
diagnostic: &Diagnostic,
policy: &ExportPolicy,
) -> serde_json::Result<String> {
let export = ExportDiagnostic::with_policy(diagnostic, policy);
serde_json::to_string_pretty(&export)
}
pub fn render_with_policy(&self, diagnostic: &Diagnostic, policy: &ExportPolicy) -> String {
self.try_render_with_policy(diagnostic, policy)
.unwrap_or_else(|_| DIAGNOSTIC_SERIALIZATION_FALLBACK.to_owned())
}
pub fn try_render_report_with_policy<'a>(
&self,
diagnostics: impl IntoIterator<Item = &'a Diagnostic>,
policy: &ExportPolicy,
) -> serde_json::Result<String> {
let diagnostics = diagnostics
.into_iter()
.map(|diagnostic| ExportDiagnostic::with_policy(diagnostic, policy))
.collect::<Vec<_>>();
serde_json::to_string_pretty(&diagnostics)
}
pub fn render_report_with_policy<'a>(
&self,
diagnostics: impl IntoIterator<Item = &'a Diagnostic>,
policy: &ExportPolicy,
) -> String {
self.try_render_report_with_policy(diagnostics, policy)
.unwrap_or_else(|_| REPORT_SERIALIZATION_FALLBACK.to_owned())
}
}
impl Renderer for JsonRenderer {
fn render(&self, diagnostic: &Diagnostic) -> String {
self.render_with_policy(diagnostic, &ExportPolicy::default())
}
}