use alloc::string::{
String,
ToString,
};
use core::fmt::Display;
use anyhow::Error;
use thiserror::Error;
use crate::error::WrapError;
#[derive(Error, Debug)]
#[error("{message}")]
pub struct GeneralError {
message: String,
}
impl GeneralError {
pub fn new<M>(message: M) -> Self
where
M: Display,
{
Self {
message: message.to_string(),
}
}
}
#[derive(Error, Debug)]
#[error("{target} not found")]
pub struct NotFoundError {
target: String,
}
impl NotFoundError {
pub fn new<M>(target: M) -> Self
where
M: Display,
{
Self {
target: target.to_string(),
}
}
}
#[derive(Error, Debug)]
#[error("failed to borrow {target}")]
pub struct BorrowFailedError {
#[source]
error: anyhow::Error,
target: String,
}
impl BorrowFailedError {
pub fn new<E, M>(error: E, target: M) -> Self
where
E: Into<anyhow::Error>,
M: Display,
{
Self {
error: error.into(),
target: target.to_string(),
}
}
}
#[derive(Error, Debug)]
#[error("integer overflow")]
pub struct IntegerOverflowError {
#[source]
error: anyhow::Error,
}
impl IntegerOverflowError {
pub fn wrap(error: anyhow::Error) -> Self {
Self { error }
}
}
#[track_caller]
pub fn general_error<M>(message: M) -> Error
where
M: Display,
{
GeneralError::new(message).wrap_error()
}
#[track_caller]
pub fn not_found_error<M>(target: M) -> Error
where
M: Display,
{
NotFoundError::new(target).wrap_error()
}
#[track_caller]
pub fn borrow_failed_error<E, M>(error: E, target: M) -> Error
where
E: Into<anyhow::Error>,
M: Display,
{
BorrowFailedError::new(error, target).wrap_error()
}
#[track_caller]
pub fn integer_overflow_error<E>(error: E) -> Error
where
E: Into<anyhow::Error>,
{
IntegerOverflowError::wrap(error.into()).wrap_error()
}