#![allow(missing_debug_implementations, missing_docs)]
use crate::{EyreHandler, Report};
use core::fmt::{Debug, Display};
#[cfg(feature = "std")]
use crate::StdError;
pub struct Adhoc;
pub trait AdhocKind: Sized {
#[inline]
fn eyre_kind(&self) -> Adhoc {
Adhoc
}
}
impl<T> AdhocKind for &T where T: ?Sized + Display + Debug + Send + Sync + 'static {}
impl Adhoc {
pub fn new<M, H: EyreHandler>(self, message: M) -> Report<H>
where
M: Display + Debug + Send + Sync + 'static,
{
Report::from_adhoc(message)
}
}
pub struct Trait<H>(std::marker::PhantomData<H>);
pub trait TraitKind<H>: Sized {
#[inline]
fn eyre_kind(&self) -> Trait<H> {
Trait(std::marker::PhantomData)
}
}
impl<E, H> TraitKind<H> for E
where
E: Into<Report<H>>,
H: EyreHandler,
{
}
impl<H> Trait<H> {
pub fn new<E>(self, error: E) -> Report<H>
where
E: Into<Report<H>>,
H: EyreHandler,
{
error.into()
}
}
#[cfg(feature = "std")]
pub struct Boxed;
#[cfg(feature = "std")]
pub trait BoxedKind: Sized {
#[inline]
fn eyre_kind(&self) -> Boxed {
Boxed
}
}
#[cfg(feature = "std")]
impl BoxedKind for Box<dyn StdError + Send + Sync> {}
#[cfg(feature = "std")]
impl Boxed {
pub fn new<H: EyreHandler>(self, error: Box<dyn StdError + Send + Sync>) -> Report<H> {
Report::from_boxed(error)
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::eyre;
use std::num::ParseIntError;
struct NonDefaultHandler;
impl EyreHandler for NonDefaultHandler {
#[allow(unused_variables)]
fn default(error: &(dyn StdError + 'static)) -> Self {
Self
}
fn debug(
&self,
_error: &(dyn StdError + 'static),
_f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
Ok(())
}
}
fn _parse(s: &str) -> Result<i32, ParseIntError> {
s.parse::<i32>()
}
fn _throw_error() -> Result<(), Report<NonDefaultHandler>> {
match _parse("abc") {
Ok(_) => Ok(()),
Err(e) => Err(eyre!(e).wrap_err("try parsing an actual number")),
}
}
}