1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use wasmer_wit::decoders::wat::Error as WATError;
use std::io::Error as StdIOError;
use std::error::Error;
#[derive(Debug)]
pub enum WITParserError {
NoWITSection,
MultipleWITSections,
WITRemainderNotEmpty,
CorruptedWITSection,
IncorrectWIT(String),
CorruptedWATFile(WATError),
CorruptedWasmFile(anyhow::Error),
AstToBytesError(StdIOError),
WasmEmitError(anyhow::Error),
}
impl Error for WITParserError {}
impl std::fmt::Display for WITParserError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
match self {
WITParserError::NoWITSection => write!(f, "Loaded module doesn't contain WIT section"),
WITParserError::MultipleWITSections => write!(
f,
"Loaded module contains multiple WIT sections that is unsupported now"
),
WITParserError::WITRemainderNotEmpty => write!(
f,
"WIT section remainder isn't empty - WIT section possibly corrupted"
),
WITParserError::IncorrectWIT(err_msg) => write!(f, "{}", err_msg),
WITParserError::CorruptedWITSection => write!(f, "WIT section is corrupted"),
WITParserError::CorruptedWATFile(err) => {
write!(f, "an error occurred while parsing wat file: {}", err)
}
WITParserError::CorruptedWasmFile(err) => {
write!(f, "Failed to parse the Wasm module: {}", err)
}
WITParserError::AstToBytesError(err) => {
write!(f, "Wasm AST converting to bytes failed with: {}", err)
}
WITParserError::WasmEmitError(err) => write!(f, "Failed to emit Wasm file: {}", err),
}
}
}
impl From<WATError> for WITParserError {
fn from(err: WATError) -> Self {
WITParserError::CorruptedWATFile(err)
}
}
impl From<StdIOError> for WITParserError {
fn from(err: StdIOError) -> Self {
WITParserError::AstToBytesError(err)
}
}