fce_wit_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 functions;
18mod wit;
19
20pub use functions::*;
21pub use wit::*;
22
23use crate::Result;
24use crate::WITParserError;
25
26use fce_wit_interfaces::FCEWITInterfaces;
27use std::path::Path;
28
29pub fn module_interface<P>(module_path: P) -> Result<ServiceInterface>
30where
31    P: AsRef<Path>,
32{
33    create_fce_it_with(module_path, |it| get_interface(&it))
34}
35
36pub fn module_raw_interface<P>(module_path: P) -> Result<FCEModuleInterface>
37where
38    P: AsRef<Path>,
39{
40    create_fce_it_with(module_path, |it| get_raw_interface(&it))
41}
42
43fn create_fce_it_with<P, T>(
44    module_path: P,
45    transformer: impl FnOnce(FCEWITInterfaces<'_>) -> Result<T>,
46) -> Result<T>
47where
48    P: AsRef<Path>,
49{
50    let module = walrus::ModuleConfig::new()
51        .parse_file(module_path)
52        .map_err(WITParserError::CorruptedWasmFile)?;
53    let raw_custom_section = extract_custom_section(&module)?;
54    let custom_section_bytes = raw_custom_section.as_ref();
55    let wit = extract_wit_from_bytes(custom_section_bytes)?;
56
57    let fce_interface = FCEWITInterfaces::new(wit);
58
59    transformer(fce_interface)
60}