use crate::engine::bytecode::DropKeepError;
use alloc::boxed::Box;
use core::fmt::{self, Display};
#[derive(Debug)]
pub struct TranslationError {
inner: Box<TranslationErrorInner>,
}
impl TranslationError {
pub fn unsupported_block_type(block_type: wasmparser::BlockType) -> Self {
Self {
inner: Box::new(TranslationErrorInner::UnsupportedBlockType(block_type)),
}
}
pub fn unsupported_value_type(value_type: wasmparser::ValType) -> Self {
Self {
inner: Box::new(TranslationErrorInner::UnsupportedValueType(value_type)),
}
}
}
impl From<wasmparser::BinaryReaderError> for TranslationError {
fn from(error: wasmparser::BinaryReaderError) -> Self {
Self {
inner: Box::new(TranslationErrorInner::Validate(error)),
}
}
}
impl From<DropKeepError> for TranslationError {
fn from(error: DropKeepError) -> Self {
Self {
inner: Box::new(TranslationErrorInner::DropKeep(error)),
}
}
}
impl Display for TranslationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &*self.inner {
TranslationErrorInner::Validate(error) => error.fmt(f),
TranslationErrorInner::UnsupportedBlockType(error) => {
write!(f, "encountered unsupported Wasm block type: {error:?}")
}
TranslationErrorInner::UnsupportedValueType(error) => {
write!(f, "encountered unsupported Wasm value type: {error:?}")
}
TranslationErrorInner::DropKeep(error) => error.fmt(f),
}
}
}
#[derive(Debug)]
enum TranslationErrorInner {
Validate(wasmparser::BinaryReaderError),
UnsupportedBlockType(wasmparser::BlockType),
UnsupportedValueType(wasmparser::ValType),
DropKeep(DropKeepError),
}