#[cfg(any(feature = "std", rust_1_81))]
use alloc::string::{String, ToString};
#[cfg(rust_1_81)]
use core::error::Error;
#[cfg(nightly)]
use core::error::Request;
use core::fmt;
#[cfg(all(feature = "std", not(rust_1_81)))]
use std::error::Error;
use crate::Report;
pub trait Context: fmt::Display + fmt::Debug + Send + Sync + 'static {
#[cfg(nightly)]
#[allow(unused_variables)]
fn provide<'a>(&'a self, request: &mut Request<'a>) {}
#[doc(hidden)]
#[cfg(any(feature = "std", rust_1_81))]
fn __source(&self) -> Option<&(dyn Error + 'static)> {
None
}
}
#[cfg(any(feature = "std", rust_1_81))]
pub(crate) struct SourceContext(String);
#[cfg(any(feature = "std", rust_1_81))]
impl SourceContext {
pub(crate) fn from_error(value: &dyn Error) -> Self {
Self(value.to_string())
}
}
#[cfg(any(feature = "std", rust_1_81))]
impl fmt::Debug for SourceContext {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, fmt)
}
}
#[cfg(any(feature = "std", rust_1_81))]
impl fmt::Display for SourceContext {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, fmt)
}
}
#[cfg(any(feature = "std", rust_1_81))]
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)
}
}
#[cfg(any(feature = "std", rust_1_81))]
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()
}
}