use std::{num::TryFromIntError, string::FromUtf8Error};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum RuntimeError {
#[error("Requested allocation size is too large")]
AllocationTooLarge,
#[error("Requested allocation alignment is invalid")]
InvalidAlignment,
#[error("Memory allocation failed")]
AllocationFailed,
#[error("Attempt to deallocate an invalid address")]
DeallocateInvalidAddress,
#[error("Function `{_0}` could not be found in the module's exports")]
FunctionNotFound(String),
#[error("Export `{_0}` is not a function")]
NotAFunction(String),
#[error("Failed to load `memory` export")]
MissingMemory,
#[error("Unexpected type for `memory` export")]
NotMemory,
#[error("Failed to load string from non-UTF-8 bytes: {0}")]
InvalidString(#[from] FromUtf8Error),
#[error("Invalid address read: {0}")]
InvalidNumber(#[from] TryFromIntError),
#[error("Unexpected variant discriminant {discriminant} for `{type_name}`")]
InvalidVariant {
type_name: &'static str,
discriminant: i64,
},
#[error("Error reported by host function handler: {_0}")]
Custom(#[source] anyhow::Error),
#[cfg(with_wasmer)]
#[error(transparent)]
Wasmer(#[from] wasmer::RuntimeError),
#[cfg(with_wasmer)]
#[error(transparent)]
WasmerMemory(#[from] wasmer::MemoryAccessError),
#[cfg(with_wasmtime)]
#[error(transparent)]
Wasmtime(anyhow::Error),
#[cfg(with_wasmtime)]
#[error(transparent)]
WasmtimeTrap(#[from] wasmtime::Trap),
}