radix_wasmi/module/
error.rs

1use super::ReadError;
2use crate::engine::TranslationError;
3use alloc::boxed::Box;
4use core::{
5    fmt,
6    fmt::{Debug, Display},
7};
8use wasmparser::BinaryReaderError as ParserError;
9
10/// Errors that may occur upon reading, parsing and translating Wasm modules.
11#[derive(Debug)]
12pub enum ModuleError {
13    /// Encountered when there is a problem with the Wasm input stream.
14    Read(ReadError),
15    /// Encountered when there is a Wasm parsing error.
16    Parser(ParserError),
17    /// Encountered when there is a Wasm to `wasmi` translation error.
18    Translation(TranslationError),
19    /// Encountered when unsupported Wasm proposal definitions are used.
20    Unsupported { message: Box<str> },
21}
22
23impl ModuleError {
24    pub(crate) fn unsupported(definition: impl Debug) -> Self {
25        Self::Unsupported {
26            message: format!("{definition:?}").into(),
27        }
28    }
29}
30
31impl Display for ModuleError {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            ModuleError::Read(error) => Display::fmt(error, f),
35            ModuleError::Parser(error) => Display::fmt(error, f),
36            ModuleError::Translation(error) => Display::fmt(error, f),
37            ModuleError::Unsupported { message } => {
38                write!(f, "encountered unsupported Wasm proposal item: {message:?}",)
39            }
40        }
41    }
42}
43
44impl From<ReadError> for ModuleError {
45    fn from(error: ReadError) -> Self {
46        Self::Read(error)
47    }
48}
49
50impl From<ParserError> for ModuleError {
51    fn from(error: ParserError) -> Self {
52        Self::Parser(error)
53    }
54}
55
56impl From<TranslationError> for ModuleError {
57    fn from(error: TranslationError) -> Self {
58        Self::Translation(error)
59    }
60}