1#![expect(deprecated, reason = "We use `Context` to maintain compatibility")]
2
3use crate::{Attachment, Context, IntoReport, OpaqueAttachment, Report};
4
5#[expect(rustdoc::invalid_html_tags, reason = "False positive")]
6#[deprecated(
43 note = "Use `core::result::Result<T, Report<C>>` instead",
44 since = "0.6.0"
45)]
46pub type Result<T, C> = core::result::Result<T, Report<C>>;
47
48pub trait ResultExt {
51 type Context: ?Sized;
53
54 type Ok;
56
57 fn attach<A>(self, attachment: A) -> core::result::Result<Self::Ok, Report<Self::Context>>
62 where
63 A: Attachment;
64
65 fn attach_with<A, F>(
70 self,
71 attachment: F,
72 ) -> core::result::Result<Self::Ok, Report<Self::Context>>
73 where
74 A: Attachment,
75 F: FnOnce() -> A;
76
77 #[deprecated(
78 note = "Use `attach_opaque_with` instead. `attach_lazy` was renamed to \
79 `attach_opaque_with` and `attach_printable_lazy` was renamed to `attach_with`",
80 since = "0.6.0"
81 )]
82 fn attach_lazy<A, F>(
83 self,
84 attachment: F,
85 ) -> core::result::Result<Self::Ok, Report<Self::Context>>
86 where
87 A: OpaqueAttachment,
88 F: FnOnce() -> A;
89
90 fn attach_opaque<A>(
94 self,
95 attachment: A,
96 ) -> core::result::Result<Self::Ok, Report<Self::Context>>
97 where
98 A: OpaqueAttachment;
99
100 fn attach_opaque_with<A, F>(
104 self,
105 attachment: F,
106 ) -> core::result::Result<Self::Ok, Report<Self::Context>>
107 where
108 A: OpaqueAttachment,
109 F: FnOnce() -> A;
110
111 #[deprecated(
112 note = "Use `attach` instead. `attach` was renamed to `attach_opaque` and \
113 `attach_printable` was renamed to `attach`",
114 since = "0.6.0"
115 )]
116 fn attach_printable<A>(
117 self,
118 attachment: A,
119 ) -> core::result::Result<Self::Ok, Report<Self::Context>>
120 where
121 A: Attachment;
122
123 #[deprecated(
124 note = "Use `attach_with` instead. `attach_lazy` was renamed to `attach_opaque_with` and \
125 `attach_printable_lazy` was renamed to `attach_with`",
126 since = "0.6.0"
127 )]
128 fn attach_printable_lazy<A, F>(
129 self,
130 attachment: F,
131 ) -> core::result::Result<Self::Ok, Report<Self::Context>>
132 where
133 A: Attachment,
134 F: FnOnce() -> A;
135
136 fn change_context<C>(self, context: C) -> core::result::Result<Self::Ok, Report<C>>
140 where
141 C: Context;
142
143 fn change_context_lazy<C, F>(self, context: F) -> core::result::Result<Self::Ok, Report<C>>
147 where
148 C: Context,
149 F: FnOnce() -> C;
150}
151
152impl<T, E> ResultExt for core::result::Result<T, E>
153where
154 E: IntoReport,
155{
156 type Context = E::Context;
157 type Ok = T;
158
159 #[track_caller]
160 fn attach<A>(self, attachment: A) -> core::result::Result<T, Report<E::Context>>
161 where
162 A: Attachment,
163 {
164 match self {
165 Ok(value) => Ok(value),
166 Err(error) => Err(error.into_report().attach(attachment)),
167 }
168 }
169
170 #[track_caller]
171 fn attach_with<A, F>(self, attachment: F) -> core::result::Result<T, Report<E::Context>>
172 where
173 A: Attachment,
174 F: FnOnce() -> A,
175 {
176 match self {
177 Ok(value) => Ok(value),
178 Err(error) => Err(error.into_report().attach(attachment())),
179 }
180 }
181
182 #[track_caller]
183 fn attach_lazy<A, F>(self, attachment: F) -> core::result::Result<T, Report<E::Context>>
184 where
185 A: OpaqueAttachment,
186 F: FnOnce() -> A,
187 {
188 match self {
189 Ok(value) => Ok(value),
190 Err(error) => Err(error.into_report().attach_opaque(attachment())),
191 }
192 }
193
194 #[track_caller]
195 fn attach_opaque<A>(self, attachment: A) -> core::result::Result<T, Report<E::Context>>
196 where
197 A: OpaqueAttachment,
198 {
199 match self {
200 Ok(value) => Ok(value),
201 Err(error) => Err(error.into_report().attach_opaque(attachment)),
202 }
203 }
204
205 #[track_caller]
206 fn attach_opaque_with<A, F>(self, attachment: F) -> core::result::Result<T, Report<E::Context>>
207 where
208 A: OpaqueAttachment,
209 F: FnOnce() -> A,
210 {
211 match self {
212 Ok(value) => Ok(value),
213 Err(error) => Err(error.into_report().attach_opaque(attachment())),
214 }
215 }
216
217 #[track_caller]
218 fn attach_printable<A>(self, attachment: A) -> core::result::Result<T, Report<E::Context>>
219 where
220 A: Attachment,
221 {
222 match self {
223 Ok(value) => Ok(value),
224 Err(error) => Err(error.into_report().attach(attachment)),
225 }
226 }
227
228 #[track_caller]
229 fn attach_printable_lazy<A, F>(
230 self,
231 attachment: F,
232 ) -> core::result::Result<T, Report<E::Context>>
233 where
234 A: Attachment,
235 F: FnOnce() -> A,
236 {
237 match self {
238 Ok(value) => Ok(value),
239 Err(error) => Err(error.into_report().attach(attachment())),
240 }
241 }
242
243 #[track_caller]
244 fn change_context<C>(self, context: C) -> core::result::Result<T, Report<C>>
245 where
246 C: Context,
247 {
248 match self {
249 Ok(value) => Ok(value),
250 Err(error) => Err(error.into_report().change_context(context)),
251 }
252 }
253
254 #[track_caller]
255 fn change_context_lazy<C, F>(self, context: F) -> core::result::Result<T, Report<C>>
256 where
257 C: Context,
258 F: FnOnce() -> C,
259 {
260 match self {
261 Ok(value) => Ok(value),
262 Err(error) => Err(error.into_report().change_context(context())),
263 }
264 }
265}