error_stack/compat/
anyhow.rs

1use anyhow::Error as AnyhowError;
2
3use crate::{Frame, IntoReportCompat, Report};
4
5impl<T> IntoReportCompat for Result<T, AnyhowError> {
6    type Err = AnyhowError;
7    type Ok = T;
8
9    #[track_caller]
10    fn into_report(self) -> Result<T, Report<AnyhowError>> {
11        match self {
12            Ok(value) => Ok(value),
13            Err(anyhow) => {
14                #[cfg(feature = "std")]
15                let sources = anyhow
16                    .chain()
17                    .skip(1)
18                    .map(ToString::to_string)
19                    .collect::<alloc::vec::Vec<_>>();
20
21                #[cfg_attr(not(feature = "std"), expect(unused_mut))]
22                let mut report: Report<AnyhowError> =
23                    Report::from_frame(Frame::from_anyhow(anyhow, alloc::boxed::Box::new([])));
24
25                #[cfg(feature = "std")]
26                for source in sources {
27                    report = report.attach(source);
28                }
29
30                Err(report)
31            }
32        }
33    }
34}