miden_utils_diagnostics/
reporting.rs

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