use crate::LimiterError;
use core::{error::Error, fmt, fmt::Display};
#[derive(Debug, Copy, Clone)]
pub enum MemoryError {
OutOfSystemMemory,
OutOfBoundsGrowth,
OutOfBoundsAccess,
InvalidMemoryType,
InvalidStaticBufferSize,
ResourceLimiterDeniedAllocation,
MinimumSizeOverflow,
MaximumSizeOverflow,
OutOfFuel { required_fuel: u64 },
}
impl Error for MemoryError {}
impl Display for MemoryError {
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::OutOfBoundsGrowth => "out of bounds memory growth",
Self::OutOfBoundsAccess => "out of bounds memory access",
Self::InvalidMemoryType => "tried to create an invalid linear memory type",
Self::InvalidStaticBufferSize => "tried to use too small static buffer",
Self::ResourceLimiterDeniedAllocation => {
"a resource limiter denied to allocate or grow the linear memory"
}
Self::MinimumSizeOverflow => {
"the minimum size of the memory type overflows the system index type"
}
Self::MaximumSizeOverflow => {
"the maximum size of the memory type overflows the system index type"
}
Self::OutOfFuel { required_fuel } => {
return write!(f, "not enough fuel. required={required_fuel}");
}
};
write!(f, "{message}")
}
}
impl From<LimiterError> for MemoryError {
fn from(error: LimiterError) -> Self {
match error {
LimiterError::OutOfSystemMemory => Self::OutOfSystemMemory,
LimiterError::OutOfBoundsGrowth => Self::OutOfBoundsGrowth,
LimiterError::ResourceLimiterDeniedAllocation => Self::ResourceLimiterDeniedAllocation,
LimiterError::OutOfFuel { required_fuel } => Self::OutOfFuel { required_fuel },
}
}
}