rootcause_internals/
handlers.rs

1//! Handlers used to implement or override the behavior of [`core::error::Error`], [`core::fmt::Display`] and
2//! [`core::fmt::Debug`] when creating an attachment or report.
3
4/// Handler used to implement or override the behavior of [`core::error::Error`], [`core::fmt::Display`] and
5/// [`core::fmt::Debug`] when creating a report.
6pub trait ContextHandler<C>: 'static {
7    /// The function used when calling [`RawReportRef::context_source`]
8    ///
9    /// [`RawReportRef::context_source`]: crate::report::RawReportRef::context_source
10    fn source(value: &C) -> Option<&(dyn core::error::Error + 'static)>;
11
12    /// The function used when calling [`RawReportRef::context_display`]
13    ///
14    /// [`RawReportRef::context_display`]: crate::report::RawReportRef::context_display
15    fn display(value: &C, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result;
16
17    /// The function used when calling [`RawReportRef::context_debug`]
18    ///
19    /// [`RawReportRef::context_debug`]: crate::report::RawReportRef::context_debug
20    fn debug(value: &C, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result;
21
22    /// The formatting style preferred by the context when formatted as part of a
23    /// report.
24    ///
25    /// # Arguments
26    ///
27    /// - `report_formatting_function`: Whether the report in which this context will be embedded is being formatted using [`Display`] formatting or [`Debug`]
28    /// - `report_formatting_alternate`: Whether the report in which this context will be embedded is being formatted using the [`alternate`] mode
29    ///
30    /// [`Display`]: core::fmt::Display
31    /// [`Debug`]: core::fmt::Debug
32    /// [`alternate`]: core::fmt::Formatter::alternate
33    fn preferred_formatting_style(
34        value: &C,
35        report_formatting_function: FormattingFunction,
36        report_formatting_alternate: bool,
37    ) -> ContextFormattingStyle {
38        let _ = (
39            value,
40            report_formatting_function,
41            report_formatting_alternate,
42        );
43        ContextFormattingStyle::default()
44    }
45}
46
47/// Handler used to implement or override the behavior of [`core::fmt::Display`] and
48/// [`core::fmt::Debug`] when creating an attachment.
49pub trait AttachmentHandler<A>: 'static {
50    /// The function used when calling [`RawAttachmentRef::attachment_display`]
51    ///
52    /// [`RawAttachmentRef::attachment_display`]: crate::attachment::RawAttachmentRef::attachment_display
53    fn display(value: &A, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result;
54
55    /// The function used when calling [`RawAttachmentRef::attachment_debug`]
56    ///
57    /// [`RawAttachmentRef::attachment_debug`]: crate::attachment::RawAttachmentRef::attachment_debug
58    fn debug(value: &A, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result;
59
60    /// The function used when calling [`RawAttachmentRef::preferred_formatting_style`]
61    ///
62    /// The formatting style preferred by the attachment when formatted as part of a
63    /// report.
64    ///
65    /// # Arguments
66    ///
67    /// - `report_formatting_function`: Whether the report in which this attachment will be embedded is being formatted using [`Display`] formatting or [`Debug`]
68    /// - `report_formatting_alternate`: Whether the report in which this attachment will be embedded is being formatted using the [`alternate`] mode
69    ///
70    /// [`Display`]: core::fmt::Display
71    /// [`Debug`]: core::fmt::Debug
72    /// [`alternate`]: core::fmt::Formatter::alternate
73    ///
74    /// [`RawAttachmentRef::preferred_formatting_style`]: crate::attachment::RawAttachmentRef::preferred_formatting_style
75    fn preferred_formatting_style(
76        value: &A,
77        report_formatting_function: FormattingFunction,
78        report_formatting_alternate: bool,
79    ) -> AttachmentFormattingStyle {
80        let _ = (
81            value,
82            report_formatting_function,
83            report_formatting_alternate,
84        );
85        AttachmentFormattingStyle::default()
86    }
87}
88
89/// Struct for contexts to specify how they prefer to be
90/// formatted when they are formatted as part of a report.
91///
92/// Whether this is respected or not, and what constitutes an "appendix" is
93/// up to the code that does the formatting for reports.
94#[derive(Copy, Clone, Debug, Default)]
95pub struct ContextFormattingStyle {
96    /// The preferred formatting function to use
97    pub function: FormattingFunction,
98}
99
100/// Struct for attachments to specify how and where the attachment prefers to be
101/// formatted when they are formatted as part of a report.
102///
103/// Whether this is respected or not, and what constitutes an "appendix" is
104/// up to the code that does the formatting for reports.
105#[derive(Copy, Clone, Debug, Default)]
106pub struct AttachmentFormattingStyle {
107    /// The preferred attachment placement
108    pub placement: AttachmentFormattingPlacement,
109    /// The preferred formatting function to use
110    pub function: FormattingFunction,
111    /// The preferred formatting priority. Higher priority means
112    /// a preference for being printed earlier in the report
113    pub priority: i32,
114}
115
116/// Enum for deciding which function to prefer when a context/attachment
117/// is formatted as part of a report.
118///
119/// Whether this is respected or not is up to the code that does the formatting for reports.
120#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
121pub enum FormattingFunction {
122    /// The context prefers to be rendered inline using the [`ContextHandler::display`]/[`AttachmentHandler::display`] methods.
123    #[default]
124    Display,
125    /// The context prefers to be rendered inline using the [`ContextHandler::debug`]/[`AttachmentHandler::debug`] methods
126    Debug,
127}
128
129/// Enum for attachments to specify the placement they prefer to be
130/// formatted when they are formatted as part of a report.
131///
132/// Whether this is respected or not, and what constitutes an "appendix" is
133/// up to the code that does the formatting for reports.
134#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
135pub enum AttachmentFormattingPlacement {
136    /// The attachment prefers to be rendered inline
137    #[default]
138    Inline,
139    /// The attachment prefers to be rendered inline under a sub-header. This
140    /// can be useful for attachments rendering as multiple lines
141    InlineWithHeader {
142        /// The header used to render the attachment below
143        header: &'static str,
144    },
145    /// The attachment prefers to be rendered as an appendix
146    Appendix {
147        /// In case the report formatter uses named appendices, then this
148        /// is the name preferred for this attachment
149        appendix_name: &'static str,
150    },
151    /// The attachment prefers not to be rendered, but would like to show up in an "$N additional opaque attachments" message
152    Opaque,
153    /// The attachment prefers not to be rendered at all
154    Hidden,
155}