use core::error::Error;
use core::fmt;
use crate::common::reserve::ReserveFractionError;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TryBuildError {
InvalidReserveFraction(ReserveFractionError),
FunnelExponentBelowMinimum {
reserve_exponent: u32,
minimum: u32,
},
CapacityOverflow,
AllocError,
}
impl fmt::Display for TryBuildError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidReserveFraction(error) => error.fmt(f),
Self::FunnelExponentBelowMinimum {
reserve_exponent,
minimum,
} => write!(
f,
"Funnel reserve exponent {reserve_exponent} is below the minimum {minimum}"
),
Self::CapacityOverflow => f.write_str("capacity overflow"),
Self::AllocError => f.write_str("memory allocation failed"),
}
}
}
impl Error for TryBuildError {}
impl From<ReserveFractionError> for TryBuildError {
fn from(error: ReserveFractionError) -> Self {
Self::InvalidReserveFraction(error)
}
}
impl From<TryReserveError> for TryBuildError {
fn from(error: TryReserveError) -> Self {
match error {
TryReserveError::CapacityOverflow => Self::CapacityOverflow,
TryReserveError::AllocError => Self::AllocError,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TryReserveError {
CapacityOverflow,
AllocError,
}
impl fmt::Display for TryReserveError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::CapacityOverflow => f.write_str("capacity overflow"),
Self::AllocError => f.write_str("memory allocation failed"),
}
}
}
impl Error for TryReserveError {}