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