1use core::fmt;
2
3use crate::{Context, Report};
4
5pub type Result<T, C> = core::result::Result<T, Report<C>>;
42
43pub trait ResultExt {
46 type Context: Context;
48
49 type Ok;
51
52 fn attach<A>(self, attachment: A) -> core::result::Result<Self::Ok, Report<Self::Context>>
56 where
57 A: Send + Sync + 'static;
58
59 fn attach_lazy<A, F>(
63 self,
64 attachment: F,
65 ) -> core::result::Result<Self::Ok, Report<Self::Context>>
66 where
67 A: Send + Sync + 'static,
68 F: FnOnce() -> A;
69
70 fn attach_printable<A>(
75 self,
76 attachment: A,
77 ) -> core::result::Result<Self::Ok, Report<Self::Context>>
78 where
79 A: fmt::Display + fmt::Debug + Send + Sync + 'static;
80
81 fn attach_printable_lazy<A, F>(
86 self,
87 attachment: F,
88 ) -> core::result::Result<Self::Ok, Report<Self::Context>>
89 where
90 A: fmt::Display + fmt::Debug + Send + Sync + 'static,
91 F: FnOnce() -> A;
92
93 fn change_context<C>(self, context: C) -> core::result::Result<Self::Ok, Report<C>>
97 where
98 C: Context;
99
100 fn change_context_lazy<C, F>(self, context: F) -> core::result::Result<Self::Ok, Report<C>>
104 where
105 C: Context,
106 F: FnOnce() -> C;
107}
108
109impl<T, C> ResultExt for core::result::Result<T, C>
110where
111 C: Context,
112{
113 type Context = C;
114 type Ok = T;
115
116 #[track_caller]
117 fn attach<A>(self, attachment: A) -> Result<T, C>
118 where
119 A: Send + Sync + 'static,
120 {
121 match self {
122 Ok(value) => Ok(value),
123 Err(error) => Err(Report::new(error).attach(attachment)),
124 }
125 }
126
127 #[track_caller]
128 fn attach_lazy<A, F>(self, attachment: F) -> Result<T, C>
129 where
130 A: Send + Sync + 'static,
131 F: FnOnce() -> A,
132 {
133 match self {
134 Ok(value) => Ok(value),
135 Err(error) => Err(Report::new(error).attach(attachment())),
136 }
137 }
138
139 #[track_caller]
140 fn attach_printable<A>(self, attachment: A) -> Result<T, C>
141 where
142 A: fmt::Display + fmt::Debug + Send + Sync + 'static,
143 {
144 match self {
145 Ok(value) => Ok(value),
146 Err(error) => Err(Report::new(error).attach_printable(attachment)),
147 }
148 }
149
150 #[track_caller]
151 fn attach_printable_lazy<A, F>(self, attachment: F) -> Result<T, C>
152 where
153 A: fmt::Display + fmt::Debug + Send + Sync + 'static,
154 F: FnOnce() -> A,
155 {
156 match self {
157 Ok(value) => Ok(value),
158 Err(error) => Err(Report::new(error).attach_printable(attachment())),
159 }
160 }
161
162 #[track_caller]
163 fn change_context<C2>(self, context: C2) -> Result<T, C2>
164 where
165 C2: Context,
166 {
167 match self {
168 Ok(value) => Ok(value),
169 Err(error) => Err(Report::new(error).change_context(context)),
170 }
171 }
172
173 #[track_caller]
174 fn change_context_lazy<C2, F>(self, context: F) -> Result<T, C2>
175 where
176 C2: Context,
177 F: FnOnce() -> C2,
178 {
179 match self {
180 Ok(value) => Ok(value),
181 Err(error) => Err(Report::new(error).change_context(context())),
182 }
183 }
184}
185
186impl<T, C> ResultExt for Result<T, C>
187where
188 C: Context,
189{
190 type Context = C;
191 type Ok = T;
192
193 #[track_caller]
194 fn attach<A>(self, attachment: A) -> Self
195 where
196 A: Send + Sync + 'static,
197 {
198 match self {
200 Ok(ok) => Ok(ok),
201 Err(report) => Err(report.attach(attachment)),
202 }
203 }
204
205 #[track_caller]
206 fn attach_lazy<A, F>(self, attachment: F) -> Self
207 where
208 A: Send + Sync + 'static,
209 F: FnOnce() -> A,
210 {
211 match self {
213 Ok(ok) => Ok(ok),
214 Err(report) => Err(report.attach(attachment())),
215 }
216 }
217
218 #[track_caller]
219 fn attach_printable<A>(self, attachment: A) -> Self
220 where
221 A: fmt::Display + fmt::Debug + Send + Sync + 'static,
222 {
223 match self {
225 Ok(ok) => Ok(ok),
226 Err(report) => Err(report.attach_printable(attachment)),
227 }
228 }
229
230 #[track_caller]
231 fn attach_printable_lazy<A, F>(self, attachment: F) -> Self
232 where
233 A: fmt::Display + fmt::Debug + Send + Sync + 'static,
234 F: FnOnce() -> A,
235 {
236 match self {
238 Ok(ok) => Ok(ok),
239 Err(report) => Err(report.attach_printable(attachment())),
240 }
241 }
242
243 #[track_caller]
244 fn change_context<C2>(self, context: C2) -> Result<T, C2>
245 where
246 C2: Context,
247 {
248 match self {
250 Ok(ok) => Ok(ok),
251 Err(report) => Err(report.change_context(context)),
252 }
253 }
254
255 #[track_caller]
256 fn change_context_lazy<C2, F>(self, context: F) -> Result<T, C2>
257 where
258 C2: Context,
259 F: FnOnce() -> C2,
260 {
261 match self {
263 Ok(ok) => Ok(ok),
264 Err(report) => Err(report.change_context(context())),
265 }
266 }
267}