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