marine_it_parser/
deleter.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
17use super::errors::ITParserError;
18use super::custom::IT_SECTION_NAME;
19
20use walrus::ModuleConfig;
21
22use std::path::PathBuf;
23
24/// Delete all custom sections with IT from provided Wasm file.
25pub fn delete_it_section_from_file(
26    in_wasm_path: PathBuf,
27    out_wasm_path: PathBuf,
28) -> Result<(), ITParserError> {
29    let module = ModuleConfig::new()
30        .parse_file(in_wasm_path)
31        .map_err(ITParserError::CorruptedWasmFile)?;
32
33    let mut module = delete_it_section(module);
34
35    module
36        .emit_wasm_file(&out_wasm_path)
37        .map_err(ITParserError::WasmEmitError)?;
38
39    Ok(())
40}
41
42/// Delete all custom sections with IT from provided Wasm module.
43pub fn delete_it_section(mut wasm_module: walrus::Module) -> walrus::Module {
44    let wit_section_ids = wasm_module
45        .customs
46        .iter()
47        .filter_map(|(id, section)| {
48            if section.name() == IT_SECTION_NAME {
49                Some(id)
50            } else {
51                None
52            }
53        })
54        .collect::<Vec<_>>();
55
56    for id in wit_section_ids {
57        wasm_module.customs.delete(id);
58    }
59
60    wasm_module
61}