marine_it_parser/
extractor.rs

1/*
2 * Copyright 2020 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17mod it;
18
19pub use it::*;
20
21use crate::interface::ModuleInterface;
22use crate::it_interface::IModuleInterface;
23use crate::ParserResult;
24use crate::ITParserError;
25
26use marine_module_interface::it_interface;
27use marine_module_interface::interface;
28use marine_it_interfaces::MITInterfaces;
29use std::path::Path;
30
31pub fn module_interface<P>(module_path: P) -> ParserResult<ModuleInterface>
32where
33    P: AsRef<Path>,
34{
35    create_mit_with(module_path, |it| interface::get_interface(&it))
36}
37
38pub fn module_it_interface<P>(module_path: P) -> ParserResult<IModuleInterface>
39where
40    P: AsRef<Path>,
41{
42    create_mit_with(module_path, |it| it_interface::get_interface(&it))
43}
44
45fn create_mit_with<P, T, E>(
46    module_path: P,
47    transformer: impl FnOnce(MITInterfaces<'_>) -> std::result::Result<T, E>,
48) -> ParserResult<T>
49where
50    P: AsRef<Path>,
51    ITParserError: From<E>,
52{
53    let module = walrus::ModuleConfig::new()
54        .parse_file(module_path)
55        .map_err(ITParserError::CorruptedWasmFile)?;
56    let raw_custom_section = extract_custom_section(&module)?;
57    let custom_section_bytes = raw_custom_section.as_ref();
58    let it = extract_it_from_bytes(custom_section_bytes)?;
59
60    let mit = MITInterfaces::new(it);
61
62    transformer(mit).map_err(Into::into)
63}