error_stack/compat/mod.rs
1//! Compatibility module to convert errors from other libraries into [`Report`].
2//!
3//! In order to convert these error types, use [`IntoReportCompat::into_report()`].
4
5use crate::Report;
6
7#[cfg(feature = "anyhow")]
8mod anyhow;
9#[cfg(feature = "eyre")]
10mod eyre;
11
12/// Compatibility trait to convert from external libraries to [`Report`].
13///
14/// **Note**: It's not possible to implement [`Context`] on other error libraries' types from within
15/// `error-stack` as the trait has a blanket implementation relying on [`Error`]. Thus, implementing
16/// the trait would violate the orphan rule; the upstream crate could implement [`Error`] and this
17/// would imply an implementation for [`Context`]. This also implies, that it's not possible to
18/// implement [`ResultExt`] from within `error-stack`.
19///
20/// [`ResultExt`]: crate::ResultExt
21/// [`Context`]: crate::Context
22/// [`Error`]: core::error::Error
23pub trait IntoReportCompat: Sized {
24 /// Type of the [`Ok`] value in the [`Result`]
25 type Ok;
26
27 /// Type of the resulting [`Err`] variant wrapped inside a [`Report<E>`].
28 ///
29 /// [`Report<E>`]: crate::Report
30 type Err;
31
32 /// Converts the [`Err`] variant of the [`Result`] to a [`Report`]
33 ///
34 /// [`Report`]: crate::Report
35 fn into_report(self) -> Result<Self::Ok, Report<Self::Err>>;
36}