1use alloc::string::{String, ToString};
2use core::fmt::{Debug, Display};
3use wasmparser::Encoding;
4
5#[derive(Debug)]
6pub enum ParseError {
8 InvalidType,
10 UnsupportedSection(String),
12 DuplicateSection(String),
14 EmptySection(String),
16 UnsupportedOperator(String),
18 ParseError {
20 message: String,
22 offset: usize,
24 },
25 InvalidEncoding(Encoding),
27 InvalidLocalCount {
29 expected: u32,
31 actual: u32,
33 },
34 EndNotReached,
36 Other(String),
38}
39
40impl Display for ParseError {
41 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
42 match self {
43 Self::InvalidType => write!(f, "invalid type"),
44 Self::UnsupportedSection(section) => write!(f, "unsupported section: {section}"),
45 Self::DuplicateSection(section) => write!(f, "duplicate section: {section}"),
46 Self::EmptySection(section) => write!(f, "empty section: {section}"),
47 Self::UnsupportedOperator(operator) => write!(f, "unsupported operator: {operator}"),
48 Self::ParseError { message, offset } => {
49 write!(f, "error parsing module: {message} at offset {offset}")
50 }
51 Self::InvalidEncoding(encoding) => write!(f, "invalid encoding: {encoding:?}"),
52 Self::InvalidLocalCount { expected, actual } => {
53 write!(f, "invalid local count: expected {expected}, actual {actual}")
54 }
55 Self::EndNotReached => write!(f, "end of module not reached"),
56 Self::Other(message) => write!(f, "unknown error: {message}"),
57 }
58 }
59}
60
61impl core::error::Error for ParseError {}
62
63impl From<wasmparser::BinaryReaderError> for ParseError {
64 fn from(value: wasmparser::BinaryReaderError) -> Self {
65 Self::ParseError { message: value.message().to_string(), offset: value.offset() }
66 }
67}
68
69pub(crate) type Result<T, E = ParseError> = core::result::Result<T, E>;