1+ use std:: collections:: HashMap ;
2+ use std:: error:: Error ;
3+
14use 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.
9+ #[ derive( Debug ) ]
610pub enum DropSection {
711 NamesSection ,
812 /// Name of the custom section.
@@ -29,6 +33,58 @@ impl<'a> ChiselModule<'a> for DropSection {
2933 }
3034}
3135
36+ impl From < std:: num:: ParseIntError > for ModuleError {
37+ fn from ( error : std:: num:: ParseIntError ) -> Self {
38+ ModuleError :: Custom ( error. description ( ) . to_string ( ) )
39+ }
40+ }
41+
42+ impl ModuleConfig for DropSection {
43+ fn with_defaults ( ) -> Result < Self , ModuleError > {
44+ Err ( ModuleError :: NotSupported )
45+ }
46+
47+ fn with_config ( config : & HashMap < String , String > ) -> Result < Self , ModuleError > {
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
64+ }
65+ } )
66+ . collect ( ) ;
67+
68+ // Reject multiple modes
69+ if matches. len ( ) != 1 {
70+ return Err ( ModuleError :: Custom (
71+ "Only one mode allowed at a time" . to_string ( ) ,
72+ ) ) ;
73+ }
74+
75+ let ( mode, val) = matches. pop ( ) . expect ( "Verified that one match is present" ) ;
76+ match mode {
77+ "names" => Ok ( DropSection :: NamesSection ) ,
78+ "custom_by_name" => Ok ( DropSection :: CustomSectionByName ( val. clone ( ) ) ) ,
79+ "custom_by_index" => Ok ( DropSection :: CustomSectionByIndex ( str:: parse :: < usize > ( val) ?) ) ,
80+ "unknown_by_index" => Ok ( DropSection :: UnknownSectionByIndex ( str:: parse :: < usize > (
81+ val,
82+ ) ?) ) ,
83+ _ => panic ! ( "Only one of the above was present in the array" ) ,
84+ }
85+ }
86+ }
87+
3288// TODO: consider upstreaming this
3389fn custom_section_index_for ( module : & Module , name : & str ) -> Option < usize > {
3490 module. sections ( ) . iter ( ) . position ( |e| match e {
@@ -258,4 +314,17 @@ mod tests {
258314 assert ! ( custom_section_index_for( & module, "name" ) . is_none( ) ) ;
259315 assert ! ( custom_section_index_for( & module1, "name" ) . is_none( ) ) ;
260316 }
317+
318+ #[ test]
319+ fn with_config_multiple_modes ( ) {
320+ let mut conf = HashMap :: new ( ) ;
321+ conf. insert ( "names" . to_string ( ) , "" . to_string ( ) ) ;
322+ conf. insert ( "custom_by_name" . to_string ( ) , "name" . to_string ( ) ) ;
323+
324+ let module = DropSection :: with_config ( & conf) ;
325+ assert_eq ! (
326+ module. unwrap_err( ) ,
327+ ModuleError :: Custom ( "Only one mode allowed at a time" . to_string( ) )
328+ ) ;
329+ }
261330}
0 commit comments