use core::fmt::Display;
use crate::{
error::{AdHocError, Error, ErrorData, StashedErrors, WrappedError},
stash::{ErrorStash, StashWithErrors},
};
pub trait IntoEyreResult<T, I>
where
I: IntoEyreReport,
{
fn into_eyre_result(self) -> Result<T, eyre::Report>;
}
pub trait IntoEyreReport {
fn into_eyre_report(self) -> eyre::Report;
}
impl<F, M, I> IntoEyreResult<(), Error<I>> for ErrorStash<F, M, I>
where
F: FnOnce() -> M,
M: Display,
Error<I>: IntoEyreReport,
{
#[track_caller]
fn into_eyre_result(self) -> Result<(), eyre::Report> {
let result: Result<(), Error<I>> = self.into();
result.map_err(IntoEyreReport::into_eyre_report)
}
}
impl<I: Display> IntoEyreReport for StashWithErrors<I> {
#[track_caller]
fn into_eyre_report(self) -> eyre::Report {
let err = Error::<I>::from(self);
eyre::eyre!(format!("{err:#}"))
}
}
impl<I: Display> IntoEyreReport for Error<I> {
fn into_eyre_report(self) -> eyre::Report {
match self.into() {
ErrorData::Stashed(inner) => inner.into_eyre_report(),
ErrorData::Wrapped(inner) => inner.into_eyre_report(),
ErrorData::AdHoc(inner) => inner.into_eyre_report(),
}
}
}
impl<I: Display> IntoEyreReport for StashedErrors<I> {
fn into_eyre_report(self) -> eyre::Report {
eyre::eyre!(format!("{self:#}"))
}
}
impl<I: Display> IntoEyreReport for WrappedError<I> {
fn into_eyre_report(self) -> eyre::Report {
eyre::eyre!(format!("{self:#}"))
}
}
impl IntoEyreReport for AdHocError {
fn into_eyre_report(self) -> eyre::Report {
eyre::eyre!(format!("{self:#}"))
}
}