Skip to main content

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};
9
10// set_panic_hook is only available with fancy or fancy-no-backtrace features,
11// not with fancy-no-syscall. Since we use fancy-no-syscall to avoid syscall
12// dependencies, we provide a no-op stub for compatibility.
13#[cfg(feature = "std")]
14pub fn set_panic_hook() {
15    // No-op when using fancy-no-syscall feature
16    // The fancy-no-syscall feature doesn't include panic hook support
17}
18
19pub type ReportHandlerOpts = miette::MietteHandlerOpts;
20
21#[cfg(feature = "std")]
22pub type DefaultReportHandler = miette::GraphicalReportHandler;
23
24#[cfg(not(feature = "std"))]
25pub type DefaultReportHandler = miette::DebugReportHandler;
26
27/// A type that can be used to render a [super::Diagnostic] via [core::fmt::Display]
28pub struct PrintDiagnostic<D, R = DefaultReportHandler> {
29    handler: R,
30    diag: D,
31}
32
33impl<D: AsRef<dyn super::Diagnostic>> PrintDiagnostic<D> {
34    pub fn new(diag: D) -> Self {
35        Self { handler: Default::default(), diag }
36    }
37    #[cfg(feature = "std")]
38    pub fn new_without_color(diag: D) -> Self {
39        Self {
40            handler: DefaultReportHandler::new_themed(GraphicalTheme::none()),
41            diag,
42        }
43    }
44    #[cfg(not(feature = "std"))]
45    pub fn new_without_color(diag: D) -> Self {
46        Self::new(diag)
47    }
48}
49
50impl<D: AsRef<dyn super::Diagnostic>> PrintDiagnostic<D, NarratableReportHandler> {
51    pub fn narrated(diag: D) -> Self {
52        Self {
53            handler: NarratableReportHandler::default(),
54            diag,
55        }
56    }
57}
58
59impl<D: AsRef<dyn super::Diagnostic>> PrintDiagnostic<D, JSONReportHandler> {
60    pub fn json(diag: D) -> Self {
61        Self { handler: JSONReportHandler, diag }
62    }
63}
64
65impl<D: AsRef<dyn super::Diagnostic>> fmt::Display for PrintDiagnostic<D> {
66    fn fmt(&self, f: &mut fmt::Formatter) -> core::fmt::Result {
67        self.handler.render_report(f, self.diag.as_ref())
68    }
69}
70
71impl<D: AsRef<dyn super::Diagnostic>> fmt::Display for PrintDiagnostic<D, NarratableReportHandler> {
72    fn fmt(&self, f: &mut fmt::Formatter) -> core::fmt::Result {
73        self.handler.render_report(f, self.diag.as_ref())
74    }
75}
76
77impl<D: AsRef<dyn super::Diagnostic>> fmt::Display for PrintDiagnostic<D, JSONReportHandler> {
78    fn fmt(&self, f: &mut fmt::Formatter) -> core::fmt::Result {
79        self.handler.render_report(f, self.diag.as_ref())
80    }
81}