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
/*
 * Copyright 2020 Fluence Labs Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use wasmer_wit::decoders::wat::Error as WATError;
use std::io::Error as StdIOError;
use std::error::Error;

#[derive(Debug)]
pub enum WITParserError {
    /// WIT section is absent.
    NoWITSection,

    /// Multiple WIT sections.
    MultipleWITSections,

    /// WIT section remainder isn't empty.
    WITRemainderNotEmpty,

    /// An error occurred while parsing WIT section.
    CorruptedWITSection,

    // An error related to incorrect wit section.
    IncorrectWIT(String),

    /// An error occurred while parsing file in Wat format.
    CorruptedWATFile(WATError),

    /// An error occurred while parsing Wasm file
    CorruptedWasmFile(anyhow::Error),

    /// An error occurred while manipulating with converting ast to bytes.
    AstToBytesError(StdIOError),

    // Wasm emittig file error.
    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)
    }
}