1use core::fmt::{Debug, Display};
2
3use alloc::string::{String, ToString};
4use wasmparser::Encoding;
5
6#[derive(Debug)]
7pub enum ParseError {
9 InvalidType,
11 UnsupportedSection(String),
13 DuplicateSection(String),
15 EmptySection(String),
17 UnsupportedOperator(String),
19 ParseError {
21 message: String,
23 offset: usize,
25 },
26 InvalidEncoding(Encoding),
28 InvalidLocalCount {
30 expected: u32,
32 actual: u32,
34 },
35 EndNotReached,
37 Other(String),
39}
40
41impl Display for ParseError {
42 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
43 match self {
44 Self::InvalidType => write!(f, "invalid type"),
45 Self::UnsupportedSection(section) => write!(f, "unsupported section: {section}"),
46 Self::DuplicateSection(section) => write!(f, "duplicate section: {section}"),
47 Self::EmptySection(section) => write!(f, "empty section: {section}"),
48 Self::UnsupportedOperator(operator) => write!(f, "unsupported operator: {operator}"),
49 Self::ParseError { message, offset } => {
50 write!(f, "error parsing module: {message} at offset {offset}")
51 }
52 Self::InvalidEncoding(encoding) => write!(f, "invalid encoding: {encoding:?}"),
53 Self::InvalidLocalCount { expected, actual } => {
54 write!(f, "invalid local count: expected {expected}, actual {actual}")
55 }
56 Self::EndNotReached => write!(f, "end of module not reached"),
57 Self::Other(message) => write!(f, "unknown error: {message}"),
58 }
59 }
60}
61
62#[cfg(any(feature = "std", all(not(feature = "std"), feature = "nightly")))]
63impl crate::std::error::Error for ParseError {}
64
65impl From<wasmparser::BinaryReaderError> for ParseError {
66 fn from(value: wasmparser::BinaryReaderError) -> Self {
67 Self::ParseError { message: value.message().to_string(), offset: value.offset() }
68 }
69}
70
71pub(crate) type Result<T, E = ParseError> = core::result::Result<T, E>;