Skip to content

Commit 6dc308b

Browse files
jakelangaxic
authored andcommitted
Alternate with_config impl for DropSection
1 parent b82f9f9 commit 6dc308b

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

libchisel/src/dropsection.rs

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use parity_wasm::elements::{Module, Section};
66
use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleTranslator};
77

88
/// Enum on which ModuleTranslator is implemented.
9+
#[derive(Debug)]
910
pub enum DropSection {
1011
NamesSection,
1112
/// Name of the custom section.
@@ -44,20 +45,40 @@ impl ModuleConfig for DropSection {
4445
}
4546

4647
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)?))
48+
// Query all possible modes
49+
let modes: [(&'static str, Option<&String>); 4] = [
50+
("names", config.get("names".into())),
51+
("custom_by_name", config.get("custom_by_name".into())),
52+
("custom_by_index", config.get("custom_by_index".into())),
53+
("unknown_by_index", config.get("unknown_by_index".into())),
54+
];
55+
56+
// Filter out modes which were provided.
57+
let mut matches: Vec<(&'static str, &String)> = modes
58+
.iter()
59+
.filter_map(|(k, v)| {
60+
if let Some(v) = v {
61+
Some((*k, *v))
62+
} else {
63+
None
5364
}
54-
"unknown_by_index" => Ok(DropSection::UnknownSectionByIndex(str::parse::<usize>(
55-
val,
56-
)?)),
57-
_ => Err(ModuleError::NotSupported),
58-
};
59-
} else {
60-
Err(ModuleError::NotFound)
65+
})
66+
.collect();
67+
68+
// Reject multiple modes
69+
if matches.len() != 1 {
70+
return Err(ModuleError::NotSupported);
71+
}
72+
73+
let (mode, val) = matches.pop().expect("Verified that one match is present");
74+
match mode {
75+
"names" => Ok(DropSection::NamesSection),
76+
"custom_by_name" => Ok(DropSection::CustomSectionByName(val.clone())),
77+
"custom_by_index" => Ok(DropSection::CustomSectionByIndex(str::parse::<usize>(val)?)),
78+
"unknown_by_index" => Ok(DropSection::UnknownSectionByIndex(str::parse::<usize>(
79+
val,
80+
)?)),
81+
_ => panic!("Only one of the above was present in the array"),
6182
}
6283
}
6384
}
@@ -291,4 +312,14 @@ mod tests {
291312
assert!(custom_section_index_for(&module, "name").is_none());
292313
assert!(custom_section_index_for(&module1, "name").is_none());
293314
}
315+
316+
#[test]
317+
fn with_config_multiple_modes() {
318+
let mut conf = HashMap::new();
319+
conf.insert("names".to_string(), "".to_string());
320+
conf.insert("custom_by_name".to_string(), "name".to_string());
321+
322+
let module = DropSection::with_config(&conf);
323+
assert_eq!(module.unwrap_err(), ModuleError::NotSupported);
324+
}
294325
}

0 commit comments

Comments
 (0)