error_stack/compat/
anyhow.rs

1use anyhow::Error as AnyhowError;
2
3use crate::{Frame, IntoReportCompat, Report, Result};
4
5impl<T> IntoReportCompat for core::result::Result<T, AnyhowError> {
6    type Err = AnyhowError;
7    type Ok = T;
8
9    #[track_caller]
10    fn into_report(self) -> Result<T, AnyhowError> {
11        match self {
12            Ok(t) => Ok(t),
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"), allow(unused_mut))]
22                let mut report =
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_printable(source);
28                }
29
30                Err(report)
31            }
32        }
33    }
34}