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
use core::error::Error;
use crate::{Attachment, IntoReport, OpaqueAttachment, Report};
/// Extension trait for [`Result`] to provide context information on
/// [`Report`]s.
pub trait ResultExt {
/// The context type of the [`Result`] which becomes the `C` in `Report<C>`.
type Context: ?Sized;
/// Type of the [`Ok`] value in the [`Result`].
type Ok;
/// Adds a new printable attachment to the [`Report`] inside the [`Result`].
///
/// Applies [`Report::attach`] on the [`Err`] variant, refer to it for more
/// information.
fn attach<A>(self, attachment: A) -> Result<Self::Ok, Report<Self::Context>>
where
A: Attachment;
/// Lazily adds a new printable attachment to the [`Report`] inside the [`Result`].
///
/// Applies [`Report::attach`] on the [`Err`] variant, refer to it for more
/// information.
fn attach_with<A, F>(self, attachment: F) -> Result<Self::Ok, Report<Self::Context>>
where
A: Attachment,
F: FnOnce() -> A;
/// Adds a new attachment to the [`Report`] inside the [`Result`].
///
/// Applies [`Report::attach_opaque`] on the [`Err`] variant, refer to it for more information.
fn attach_opaque<A>(self, attachment: A) -> Result<Self::Ok, Report<Self::Context>>
where
A: OpaqueAttachment;
/// Lazily adds a new attachment to the [`Report`] inside the [`Result`].
///
/// Applies [`Report::attach_opaque`] on the [`Err`] variant, refer to it for more information.
fn attach_opaque_with<A, F>(self, attachment: F) -> Result<Self::Ok, Report<Self::Context>>
where
A: OpaqueAttachment,
F: FnOnce() -> A;
/// Changes the context of the [`Report`] inside the [`Result`].
///
/// Applies [`Report::change_context`] on the [`Err`] variant, refer to it for more information.
fn change_context<C>(self, context: C) -> Result<Self::Ok, Report<C>>
where
C: Error + Send + Sync + 'static;
/// Lazily changes the context of the [`Report`] inside the [`Result`].
///
/// Applies [`Report::change_context`] on the [`Err`] variant, refer to it for more information.
fn change_context_lazy<C, F>(self, context: F) -> Result<Self::Ok, Report<C>>
where
C: Error + Send + Sync + 'static,
F: FnOnce() -> C;
}
impl<T, E> ResultExt for Result<T, E>
where
E: IntoReport,
{
type Context = E::Context;
type Ok = T;
#[track_caller]
fn attach<A>(self, attachment: A) -> Result<T, Report<E::Context>>
where
A: Attachment,
{
match self {
Ok(value) => Ok(value),
Err(error) => Err(error.into_report().attach(attachment)),
}
}
#[track_caller]
fn attach_with<A, F>(self, attachment: F) -> Result<T, Report<E::Context>>
where
A: Attachment,
F: FnOnce() -> A,
{
match self {
Ok(value) => Ok(value),
Err(error) => Err(error.into_report().attach(attachment())),
}
}
#[track_caller]
fn attach_opaque<A>(self, attachment: A) -> Result<T, Report<E::Context>>
where
A: OpaqueAttachment,
{
match self {
Ok(value) => Ok(value),
Err(error) => Err(error.into_report().attach_opaque(attachment)),
}
}
#[track_caller]
fn attach_opaque_with<A, F>(self, attachment: F) -> Result<T, Report<E::Context>>
where
A: OpaqueAttachment,
F: FnOnce() -> A,
{
match self {
Ok(value) => Ok(value),
Err(error) => Err(error.into_report().attach_opaque(attachment())),
}
}
#[track_caller]
fn change_context<C>(self, context: C) -> Result<T, Report<C>>
where
C: Error + Send + Sync + 'static,
{
match self {
Ok(value) => Ok(value),
Err(error) => Err(error.into_report().change_context(context)),
}
}
#[track_caller]
fn change_context_lazy<C, F>(self, context: F) -> Result<T, Report<C>>
where
C: Error + Send + Sync + 'static,
F: FnOnce() -> C,
{
match self {
Ok(value) => Ok(value),
Err(error) => Err(error.into_report().change_context(context())),
}
}
}