use alloc::string::String;
use core::fmt;
use crate::AtCrateInfo;
use crate::at::At;
use crate::trace::AtTraceable;
pub trait ErrorAtExt: Sized {
#[track_caller]
fn start_at(self) -> At<Self>;
}
impl<E: core::error::Error> ErrorAtExt for E {
#[track_caller]
#[inline]
fn start_at(self) -> At<Self> {
At::wrap(self).at()
}
}
pub trait ResultAtExt<T, E> {
#[track_caller]
fn at(self) -> Result<T, At<E>>;
#[track_caller]
fn at_str(self, msg: &'static str) -> Result<T, At<E>>;
#[track_caller]
fn at_string(self, f: impl FnOnce() -> String) -> Result<T, At<E>>;
#[track_caller]
fn at_data<C: fmt::Display + Send + Sync + 'static>(
self,
f: impl FnOnce() -> C,
) -> Result<T, At<E>>;
#[track_caller]
fn at_debug<C: fmt::Debug + Send + Sync + 'static>(
self,
f: impl FnOnce() -> C,
) -> Result<T, At<E>>;
#[track_caller]
fn at_aside_error<Err: core::error::Error + Send + Sync + 'static>(
self,
err: Err,
) -> Result<T, At<E>>;
#[deprecated(
since = "0.1.4",
note = "Renamed to `at_aside_error()`. The attached error is NOT part of the `.source()` chain."
)]
#[track_caller]
fn at_error<Err: core::error::Error + Send + Sync + 'static>(
self,
err: Err,
) -> Result<T, At<E>>;
#[track_caller]
fn at_crate(self, info: &'static AtCrateInfo) -> Result<T, At<E>>;
#[track_caller]
fn at_fn<F: Fn()>(self, marker: F) -> Result<T, At<E>>;
#[track_caller]
fn at_named(self, name: &'static str) -> Result<T, At<E>>;
fn map_err_at<E2, F>(self, f: F) -> Result<T, At<E2>>
where
F: FnOnce(E) -> E2;
}
impl<T, E> ResultAtExt<T, E> for Result<T, At<E>> {
#[track_caller]
#[inline]
fn at(self) -> Result<T, At<E>> {
match self {
Ok(v) => Ok(v),
Err(e) => Err(e.at()),
}
}
#[track_caller]
#[inline]
fn at_str(self, msg: &'static str) -> Result<T, At<E>> {
match self {
Ok(v) => Ok(v),
Err(e) => Err(e.at_str(msg)),
}
}
#[track_caller]
#[inline]
fn at_string(self, f: impl FnOnce() -> String) -> Result<T, At<E>> {
match self {
Ok(v) => Ok(v),
Err(e) => Err(e.at_string(f)),
}
}
#[track_caller]
#[inline]
fn at_data<C: fmt::Display + Send + Sync + 'static>(
self,
f: impl FnOnce() -> C,
) -> Result<T, At<E>> {
match self {
Ok(v) => Ok(v),
Err(e) => Err(e.at_data(f)),
}
}
#[track_caller]
#[inline]
fn at_debug<C: fmt::Debug + Send + Sync + 'static>(
self,
f: impl FnOnce() -> C,
) -> Result<T, At<E>> {
match self {
Ok(v) => Ok(v),
Err(e) => Err(e.at_debug(f)),
}
}
#[track_caller]
#[inline]
fn at_aside_error<Err: core::error::Error + Send + Sync + 'static>(
self,
err: Err,
) -> Result<T, At<E>> {
match self {
Ok(v) => Ok(v),
Err(e) => Err(e.at_aside_error(err)),
}
}
#[allow(deprecated)]
#[track_caller]
#[inline]
fn at_error<Err: core::error::Error + Send + Sync + 'static>(
self,
err: Err,
) -> Result<T, At<E>> {
match self {
Ok(v) => Ok(v),
Err(e) => Err(e.at_error(err)),
}
}
#[track_caller]
#[inline]
fn at_crate(self, info: &'static AtCrateInfo) -> Result<T, At<E>> {
match self {
Ok(v) => Ok(v),
Err(e) => Err(e.at_crate(info)),
}
}
#[track_caller]
#[inline]
fn at_fn<F: Fn()>(self, marker: F) -> Result<T, At<E>> {
match self {
Ok(v) => Ok(v),
Err(e) => Err(e.at_fn(marker)),
}
}
#[track_caller]
#[inline]
fn at_named(self, name: &'static str) -> Result<T, At<E>> {
match self {
Ok(v) => Ok(v),
Err(e) => Err(e.at_named(name)),
}
}
#[inline]
fn map_err_at<E2, F>(self, f: F) -> Result<T, At<E2>>
where
F: FnOnce(E) -> E2,
{
match self {
Ok(v) => Ok(v),
Err(e) => Err(e.map_error(f)),
}
}
}
pub trait ResultAtTraceableExt<T, E: AtTraceable> {
#[track_caller]
fn at(self) -> Result<T, E>;
#[track_caller]
fn at_str(self, msg: &'static str) -> Result<T, E>;
#[track_caller]
fn at_string(self, f: impl FnOnce() -> String) -> Result<T, E>;
#[track_caller]
fn at_data<C: fmt::Display + Send + Sync + 'static>(
self,
f: impl FnOnce() -> C,
) -> Result<T, E>;
#[track_caller]
fn at_debug<C: fmt::Debug + Send + Sync + 'static>(self, f: impl FnOnce() -> C)
-> Result<T, E>;
#[track_caller]
fn at_aside_error<Err: core::error::Error + Send + Sync + 'static>(
self,
err: Err,
) -> Result<T, E>;
#[deprecated(
since = "0.1.4",
note = "Renamed to `at_aside_error()`. The attached error is NOT part of the `.source()` chain."
)]
#[track_caller]
fn at_error<Err: core::error::Error + Send + Sync + 'static>(self, err: Err) -> Result<T, E>;
#[track_caller]
fn at_crate(self, info: &'static AtCrateInfo) -> Result<T, E>;
#[track_caller]
fn at_fn<F: Fn()>(self, marker: F) -> Result<T, E>;
#[track_caller]
fn at_named(self, name: &'static str) -> Result<T, E>;
}
impl<T, E: AtTraceable> ResultAtTraceableExt<T, E> for Result<T, E> {
#[track_caller]
#[inline]
fn at(self) -> Result<T, E> {
self.map_err(|e| e.at())
}
#[track_caller]
#[inline]
fn at_str(self, msg: &'static str) -> Result<T, E> {
self.map_err(|e| e.at_str(msg))
}
#[track_caller]
#[inline]
fn at_string(self, f: impl FnOnce() -> String) -> Result<T, E> {
self.map_err(|e| e.at_string(f))
}
#[track_caller]
#[inline]
fn at_data<C: fmt::Display + Send + Sync + 'static>(
self,
f: impl FnOnce() -> C,
) -> Result<T, E> {
self.map_err(|e| e.at_data(f))
}
#[track_caller]
#[inline]
fn at_debug<C: fmt::Debug + Send + Sync + 'static>(
self,
f: impl FnOnce() -> C,
) -> Result<T, E> {
self.map_err(|e| e.at_debug(f))
}
#[track_caller]
#[inline]
fn at_aside_error<Err: core::error::Error + Send + Sync + 'static>(
self,
err: Err,
) -> Result<T, E> {
self.map_err(|e| e.at_aside_error(err))
}
#[allow(deprecated)]
#[track_caller]
#[inline]
fn at_error<Err: core::error::Error + Send + Sync + 'static>(self, err: Err) -> Result<T, E> {
self.map_err(|e| e.at_error(err))
}
#[track_caller]
#[inline]
fn at_crate(self, info: &'static AtCrateInfo) -> Result<T, E> {
self.map_err(|e| e.at_crate(info))
}
#[track_caller]
#[inline]
fn at_fn<F: Fn()>(self, marker: F) -> Result<T, E> {
self.map_err(|e| e.at_fn(marker))
}
#[track_caller]
#[inline]
fn at_named(self, name: &'static str) -> Result<T, E> {
self.map_err(|e| e.at_named(name))
}
}