Skip to main content

litcheck_core/
reporting.rs

1pub use miette::{
2    DebugReportHandler, JSONReportHandler, NarratableReportHandler, ReportHandler, set_hook,
3};
4
5use crate::diagnostics::Diagnostic;
6
7#[cfg(feature = "fancy-diagnostics")]
8pub use miette::{GraphicalReportHandler, GraphicalTheme};
9#[cfg(feature = "fancy-diagnostics")]
10pub type ReportHandlerOpts = miette::MietteHandlerOpts;
11#[cfg(feature = "fancy-diagnostics")]
12pub type DefaultReportHandler = miette::GraphicalReportHandler;
13#[cfg(not(feature = "fancy-diagnostics"))]
14pub type DefaultReportHandler = miette::DebugReportHandler;
15
16pub struct PrintDiagnostic<D, R = DefaultReportHandler> {
17    handler: R,
18    diag: D,
19}
20impl<D: AsRef<dyn Diagnostic>> PrintDiagnostic<D> {
21    pub fn new(diag: D) -> Self {
22        Self {
23            handler: Default::default(),
24            diag,
25        }
26    }
27    #[cfg(feature = "fancy-diagnostics")]
28    pub fn new_without_color(diag: D) -> Self {
29        Self {
30            handler: DefaultReportHandler::new_themed(GraphicalTheme::none()),
31            diag,
32        }
33    }
34    #[cfg(not(feature = "fancy-diagnostics"))]
35    pub fn new_without_color(diag: D) -> Self {
36        Self::new(diag)
37    }
38}
39impl<D: AsRef<dyn Diagnostic>> PrintDiagnostic<D, NarratableReportHandler> {
40    pub fn narrated(diag: D) -> Self {
41        Self {
42            handler: NarratableReportHandler::default(),
43            diag,
44        }
45    }
46}
47impl<D: AsRef<dyn Diagnostic>> PrintDiagnostic<D, JSONReportHandler> {
48    pub fn json(diag: D) -> Self {
49        Self {
50            handler: JSONReportHandler,
51            diag,
52        }
53    }
54}
55impl<D: AsRef<dyn Diagnostic>> core::fmt::Display for PrintDiagnostic<D> {
56    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
57        self.handler.render_report(f, self.diag.as_ref())
58    }
59}
60impl<D: AsRef<dyn Diagnostic>> core::fmt::Display for PrintDiagnostic<D, NarratableReportHandler> {
61    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
62        self.handler.render_report(f, self.diag.as_ref())
63    }
64}
65impl<D: AsRef<dyn Diagnostic>> core::fmt::Display for PrintDiagnostic<D, JSONReportHandler> {
66    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
67        self.handler.render_report(f, self.diag.as_ref())
68    }
69}