#![expect(deprecated, reason = "We use `Context` to maintain compatibility")]
use alloc::string::{String, ToString as _};
#[cfg(nightly)]
use core::error::Request;
use core::{error::Error, fmt};
use crate::Report;
#[deprecated(note = "Use `core::error::Error` instead", since = "0.6.0")]
pub trait Context: fmt::Display + fmt::Debug + Send + Sync + 'static {
#[cfg(nightly)]
#[expect(unused_variables)]
fn provide<'a>(&'a self, request: &mut Request<'a>) {}
#[doc(hidden)]
fn __source(&self) -> Option<&(dyn Error + 'static)> {
None
}
}
pub(crate) struct SourceContext(String);
impl SourceContext {
pub(crate) fn from_error(value: &dyn Error) -> Self {
Self(value.to_string())
}
}
impl fmt::Debug for SourceContext {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, fmt)
}
}
impl fmt::Display for SourceContext {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, fmt)
}
}
impl Context for SourceContext {}
impl<C> From<C> for Report<C>
where
C: Context,
{
#[track_caller]
#[inline]
fn from(context: C) -> Self {
Self::new(context)
}
}
impl<C: Error + Send + Sync + 'static> Context for C {
#[cfg(nightly)]
fn provide<'a>(&'a self, request: &mut Request<'a>) {
Error::provide(self, request);
}
#[doc(hidden)]
#[inline]
fn __source(&self) -> Option<&(dyn Error + 'static)> {
self.source()
}
}
pub trait OpaqueAttachment: Send + Sync + 'static {}
impl<T: Send + Sync + 'static> OpaqueAttachment for T {}
#[diagnostic::on_unimplemented(
message = "to attach this type to a `Report` it must implement `fmt::Display` and `fmt::Debug`",
note = "if you want to attach a type that is not printable, use `attach_opaque` instead"
)]
pub trait Attachment: OpaqueAttachment + fmt::Display + fmt::Debug {}
impl<T: OpaqueAttachment + fmt::Display + fmt::Debug> Attachment for T {}