#![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()
}
}