use alloc::format;
use core::fmt::{
Arguments,
Display,
};
use anyhow::Error;
use crate::error::{
context::ContextError,
general_error,
not_found_error,
};
pub trait WrapError {
#[track_caller]
fn wrap_error(self) -> Error;
#[track_caller]
fn wrap_error_with_message<M>(self, message: M) -> Error
where
M: Display;
}
impl<E> WrapError for E
where
E: Into<Error>,
{
#[track_caller]
fn wrap_error(self) -> Error {
self.into()
}
#[track_caller]
fn wrap_error_with_message<M>(self, message: M) -> Error
where
M: Display,
{
Into::<Error>::into(self).context(ContextError::new(message))
}
}
pub trait WrapResultError<T> {
#[track_caller]
fn wrap_error(self) -> Result<T, Error>;
#[track_caller]
fn wrap_error_with_message<M>(self, message: M) -> Result<T, Error>
where
M: Display;
#[track_caller]
fn wrap_error_with_format<'a>(self, args: Arguments<'a>) -> Result<T, Error>;
}
impl<T, E> WrapResultError<T> for Result<T, E>
where
E: WrapError,
{
#[track_caller]
fn wrap_error(self) -> Result<T, Error> {
match self {
Ok(val) => Ok(val),
Err(error) => Err(error.wrap_error()),
}
}
#[track_caller]
fn wrap_error_with_message<M>(self, message: M) -> Result<T, Error>
where
M: Display,
{
match self {
Ok(val) => Ok(val),
Err(error) => Err(error.wrap_error_with_message(message)),
}
}
#[track_caller]
fn wrap_error_with_format<'a>(self, args: Arguments<'a>) -> Result<T, Error> {
match self {
Ok(val) => Ok(val),
Err(error) => Err(error.wrap_error_with_message(format!("{args}"))),
}
}
}
pub trait WrapOptionError<T> {
#[track_caller]
fn wrap_expectation<M>(self, message: M) -> Result<T, Error>
where
M: Display;
#[track_caller]
fn wrap_expectation_with_format<'a>(self, args: Arguments<'a>) -> Result<T, Error>;
#[track_caller]
fn wrap_not_found_error<M>(self, message: M) -> Result<T, Error>
where
M: Display;
#[track_caller]
fn wrap_not_found_error_with_format<'a>(self, args: Arguments<'a>) -> Result<T, Error>;
}
impl<T> WrapOptionError<T> for Option<T> {
fn wrap_expectation<M>(self, message: M) -> Result<T, Error>
where
M: Display,
{
match self {
Some(val) => Ok(val),
None => Err(general_error(message)),
}
}
#[track_caller]
fn wrap_expectation_with_format<'a>(self, args: Arguments<'a>) -> Result<T, Error> {
match self {
Some(val) => Ok(val),
None => Err(general_error(format!("{args}"))),
}
}
#[track_caller]
fn wrap_not_found_error<M>(self, message: M) -> Result<T, Error>
where
M: Display,
{
match self {
Some(val) => Ok(val),
None => Err(not_found_error(message)),
}
}
#[track_caller]
fn wrap_not_found_error_with_format<'a>(self, args: Arguments<'a>) -> Result<T, Error> {
match self {
Some(val) => Ok(val),
None => Err(not_found_error(format!("{args}"))),
}
}
}