Skip to content

Commit d83cf8e

Browse files
committed
implement dropsection
1 parent ce5ccf3 commit d83cf8e

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

libchisel/src/dropsection.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
use std::collections::HashMap;
2+
use std::error::Error;
3+
14
use parity_wasm::elements::{Module, Section};
25

3-
use super::{ChiselModule, ModuleError, ModuleKind, ModuleTranslator};
6+
use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleTranslator};
47

58
/// Enum on which ModuleTranslator is implemented.
69
pub enum DropSection {
@@ -29,6 +32,36 @@ impl<'a> ChiselModule<'a> for DropSection {
2932
}
3033
}
3134

35+
impl From<std::num::ParseIntError> for ModuleError {
36+
fn from(error: std::num::ParseIntError) -> Self {
37+
ModuleError::Custom(error.description().to_string())
38+
}
39+
}
40+
41+
impl ModuleConfig for DropSection {
42+
fn with_defaults() -> Result<Self, ModuleError> {
43+
Err(ModuleError::NotSupported)
44+
}
45+
46+
fn with_config(config: &HashMap<String, String>) -> Result<Self, ModuleError> {
47+
if let Some((key, val)) = config.iter().next() {
48+
return match key.as_str() {
49+
"names" => Ok(DropSection::NamesSection),
50+
"custom_by_name" => Ok(DropSection::CustomSectionByName(val.clone())),
51+
"custom_by_index" => {
52+
Ok(DropSection::CustomSectionByIndex(str::parse::<usize>(val)?))
53+
}
54+
"unknown_by_index" => Ok(DropSection::UnknownSectionByIndex(str::parse::<usize>(
55+
val,
56+
)?)),
57+
_ => Err(ModuleError::NotSupported),
58+
};
59+
} else {
60+
Err(ModuleError::NotFound)
61+
}
62+
}
63+
}
64+
3265
// TODO: consider upstreaming this
3366
fn custom_section_index_for(module: &Module, name: &str) -> Option<usize> {
3467
module.sections().iter().position(|e| match e {

libchisel/src/fromwat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use parity_wasm::elements::Module;
22
use wabt::Wat2Wasm;
33

4-
use super::{ChiselModule, ModuleCreator, ModuleError, ModuleKind};
4+
use super::{ChiselModule, ModuleConfig, ModuleCreator, ModuleError, ModuleKind};
55

66
/// Struct on which ModuleCreator is implemented.
77
pub struct FromWat<'a> {

0 commit comments

Comments
 (0)