Skip to main content

g_err/
report.rs

1extern crate alloc;
2
3use alloc::string::String;
4use core::fmt::{Debug, Display};
5
6mod pretty_report;
7pub use pretty_report::PrettyReport;
8
9mod markdown_report;
10pub use markdown_report::MarkdownReport;
11
12mod trace_report;
13pub use trace_report::TraceReport;
14
15#[cfg(feature = "serde")]
16mod json_report;
17
18#[cfg(feature = "serde")]
19mod json_data;
20
21#[cfg(feature = "serde")]
22pub use json_data::{DisplayJsonData, JsonData, LocationJsonData, SourceJsonData};
23
24#[cfg(feature = "serde")]
25pub use json_report::JsonReport;
26
27use crate::{Config, gerr::GErr, gerr_view::GErrView};
28
29/// Reporting trait.
30///
31/// Reports error in multiple forms of display.
32pub trait Report {
33    /// Reports error from error borrowed form [`crate::GErrView`] into String.
34    #[cfg(not(feature = "serde"))]
35    fn report<E, C: Config, D>(err: &E) -> String
36    where
37        for<'a> &'a E: Into<GErrView<'a, C, D>>,
38        C::Id: Display,
39        D: Debug;
40
41    /// Reports error from error borrowed form [`crate::GErrView`] into String.
42    ///
43    /// Supports JSON.
44    #[cfg(feature = "serde")]
45    fn report<E, C: Config, D>(err: &E) -> String
46    where
47        for<'a> &'a E: Into<GErrView<'a, C, D>>,
48        C::Id: Display + serde::Serialize,
49        D: Debug + serde::Serialize;
50}
51
52#[cfg(not(feature = "serde"))]
53impl<C: Config, D> GErr<C, D>
54where
55    C::Id: Display,
56    D: Debug,
57{
58    /// Reports error in pretty format.
59    pub fn report(&self) -> String {
60        PrettyReport::report::<_, C, D>(self)
61    }
62
63    /// Reports error as specified format.
64    pub fn report_as<R>(&self) -> String
65    where
66        R: Report,
67    {
68        R::report::<_, C, D>(self)
69    }
70}
71
72#[cfg(feature = "serde")]
73impl<C: Config, D> GErr<C, D>
74where
75    C::Id: Display + serde::Serialize,
76    D: Debug + serde::Serialize,
77{
78    /// Reports error in pretty format.
79    #[inline]
80    pub fn report(&self) -> String {
81        PrettyReport::report::<_, C, D>(self)
82    }
83
84    /// Reports error as specified format.
85    #[inline]
86    pub fn report_as<R>(&self) -> String
87    where
88        R: Report,
89    {
90        R::report::<_, C, D>(self)
91    }
92}
93
94#[cfg(feature = "serde")]
95impl<C: Config, D> GErr<C, D>
96where
97    C::Id: serde::Serialize,
98    D: serde::Serialize,
99{
100    /// JSON data of GErr.
101    #[inline]
102    pub fn json_data(&self) -> JsonData {
103        JsonReport::data(self)
104    }
105
106    /// Public JSON data of GErr.
107    #[inline]
108    pub fn display_json_data(&self) -> DisplayJsonData {
109        JsonReport::display_data(self)
110    }
111}