use crate::{FuelError, LimiterError};
use core::{
error::Error,
fmt::{self, Display},
};
#[derive(Debug, Copy, Clone)]
#[non_exhaustive]
pub enum TableError {
OutOfSystemMemory,
MinimumSizeOverflow,
MaximumSizeOverflow,
ResourceLimiterDeniedAllocation,
GrowOutOfBounds,
InitOutOfBounds,
FillOutOfBounds,
SetOutOfBounds,
CopyOutOfBounds,
ElementTypeMismatch,
OutOfFuel { required_fuel: u64 },
}
impl Error for TableError {}
impl Display for TableError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let message = match self {
Self::OutOfSystemMemory => {
"tried to allocate more virtual memory than available on the system"
}
Self::MinimumSizeOverflow => "the minimum table size overflows the system bounds",
Self::MaximumSizeOverflow => "the maximum table size overflows the system bounds",
Self::ResourceLimiterDeniedAllocation => {
"a resource limiter denied to allocate or grow the table"
}
Self::GrowOutOfBounds => "out of bounds table access: `table.growth`",
Self::InitOutOfBounds => "out of bounds table access: `table.init`",
Self::FillOutOfBounds => "out of bounds table access: `table.fill`",
Self::CopyOutOfBounds => "out of bounds table access: `table.copy`",
Self::SetOutOfBounds => "out of bounds table access: `table.set`",
Self::ElementTypeMismatch => "encountered mismatching table element type",
Self::OutOfFuel { required_fuel } => {
return write!(f, "not enough fuel: required={required_fuel}");
}
};
write!(f, "{message}")
}
}
impl From<LimiterError> for TableError {
fn from(error: LimiterError) -> Self {
match error {
LimiterError::OutOfSystemMemory => Self::OutOfSystemMemory,
LimiterError::OutOfBoundsGrowth => Self::GrowOutOfBounds,
LimiterError::ResourceLimiterDeniedAllocation => Self::ResourceLimiterDeniedAllocation,
LimiterError::OutOfFuel { required_fuel } => Self::OutOfFuel { required_fuel },
}
}
}
impl From<FuelError> for TableError {
fn from(error: FuelError) -> Self {
match error {
FuelError::OutOfFuel { required_fuel } => Self::OutOfFuel { required_fuel },
FuelError::FuelMeteringDisabled => panic!("fuel metering is disabled"),
}
}
}