1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use alloc::{boxed::Box, format, string::String};
use core::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
use core::fmt::{Debug, Display, Formatter};
use super::source::{BoxDetail, ErrorSource};
use super::tracer::ErrorMessageTracer;
pub struct ErrorReport<Detail, Trace>(pub Detail, pub Trace);
impl<Detail, Trace> ErrorSource<Trace> for ErrorReport<Detail, Trace> {
type Source = Self;
type Detail = Detail;
fn error_details(ErrorReport(detail, trace): Self) -> (Detail, Option<Trace>) {
(detail, Some(trace))
}
}
impl<Detail, Trace> ErrorSource<Trace> for BoxDetail<Detail> {
type Source = ErrorReport<Detail, Trace>;
type Detail = Box<Detail>;
fn error_details(ErrorReport(detail, trace): Self::Source) -> (Self::Detail, Option<Trace>) {
(Box::new(detail), Some(trace))
}
}
impl<Detail, Trace> ErrorReport<Detail, Trace> {
pub fn new(detail: Detail, trace: Trace) -> Self {
ErrorReport(detail, trace)
}
pub fn detail(&self) -> &Detail {
&self.0
}
pub fn trace(&self) -> &Trace {
&self.1
}
pub fn add_trace<E: Display>(self, message: &E) -> Self
where
Trace: ErrorMessageTracer,
{
let detail = self.0;
let trace = self.1.add_message(message);
ErrorReport(detail, trace)
}
pub fn trace_from<E, Cont>(source: E::Source, cont: Cont) -> Self
where
Detail: Display,
E: ErrorSource<Trace>,
Trace: ErrorMessageTracer,
Cont: FnOnce(E::Detail) -> Detail,
{
let (detail1, m_trace1) = E::error_details(source);
let detail2 = cont(detail1);
match m_trace1 {
Some(trace1) => {
let trace2 = trace1.add_message(&detail2);
ErrorReport(detail2, trace2)
}
None => {
let trace2 = Trace::new_message(&detail2);
ErrorReport(detail2, trace2)
}
}
}
}
impl<Detail, Trace> Debug for ErrorReport<Detail, Trace>
where
Trace: Debug,
{
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
self.trace().fmt(f)
}
}
impl<Detail, Trace> Display for ErrorReport<Detail, Trace>
where
Trace: Display,
{
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
self.trace().fmt(f)
}
}
impl<Detail, Trace> Clone for ErrorReport<Detail, Trace>
where
Detail: Clone,
Trace: Display + ErrorMessageTracer,
{
fn clone(&self) -> Self {
ErrorReport(self.0.clone(), Trace::new_message(&self.1))
}
}
impl<Detail, Trace> PartialEq for ErrorReport<Detail, Trace>
where
Detail: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0)
}
}
impl<Detail, Trace> Eq for ErrorReport<Detail, Trace> where Detail: Eq {}
impl<Detail, Trace> PartialOrd for ErrorReport<Detail, Trace>
where
Detail: PartialOrd,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.0.partial_cmp(&other.0)
}
}
impl<Detail, Trace> Ord for ErrorReport<Detail, Trace>
where
Detail: Ord,
{
fn cmp(&self, other: &Self) -> Ordering {
self.0.cmp(&other.0)
}
}
#[cfg(feature = "std")]
impl<Detail, Trace> std::error::Error for ErrorReport<Detail, Trace>
where
Detail: Display,
Trace: Debug + Display,
Trace: ErrorMessageTracer,
{
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.trace().as_error()
}
}
#[cfg(feature = "serde")]
impl<Detail, Trace> serde::Serialize for ErrorReport<Detail, Trace>
where
Detail: serde::Serialize,
Trace: Display,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
(&self.0, format!("{}", self.1)).serialize(serializer)
}
}
#[cfg(feature = "serde")]
impl<'de, Detail, Trace> serde::Deserialize<'de> for ErrorReport<Detail, Trace>
where
Detail: serde::Deserialize<'de>,
Trace: ErrorMessageTracer,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let (detail, message) = <(Detail, String)>::deserialize(deserializer)?;
Ok(ErrorReport::new(detail, Trace::new_message(&message)))
}
}