@@ -45,6 +45,7 @@ struct RustWasm {
45
45
46
46
rt_module : IndexSet < RuntimeItem > ,
47
47
export_macros : Vec < ( String , String ) > ,
48
+ with : HashMap < String , String > ,
48
49
}
49
50
50
51
#[ derive( PartialEq , Eq , Clone , Copy , Hash , Debug ) ]
@@ -67,33 +68,18 @@ enum RuntimeItem {
67
68
BoxType ,
68
69
}
69
70
70
- #[ cfg( feature = "clap" ) ]
71
- fn iterate_hashmap_string ( s : & str ) -> impl Iterator < Item = Result < ( & str , & str ) , String > > {
72
- s. split ( ',' ) . map ( move |entry| {
73
- entry. split_once ( '=' ) . ok_or_else ( || {
74
- format ! ( "expected string of form `<key>=<value>[,<key>=<value>...]`; got `{s}`" )
75
- } )
76
- } )
77
- }
78
-
79
71
#[ derive( Debug , Clone , Hash , PartialEq , Eq , PartialOrd , Ord ) ]
80
72
pub enum ExportKey {
81
73
World ,
82
74
Name ( String ) ,
83
75
}
84
76
85
77
#[ cfg( feature = "clap" ) ]
86
- fn parse_with ( s : & str ) -> Result < HashMap < String , String > , String > {
87
- if s. is_empty ( ) {
88
- Ok ( HashMap :: default ( ) )
89
- } else {
90
- iterate_hashmap_string ( s)
91
- . map ( |entry| {
92
- let ( key, value) = entry?;
93
- Ok ( ( key. to_owned ( ) , value. to_owned ( ) ) )
94
- } )
95
- . collect ( )
96
- }
78
+ fn parse_with ( s : & str ) -> Result < ( String , String ) , String > {
79
+ let ( k, v) = s. split_once ( '=' ) . ok_or_else ( || {
80
+ format ! ( "expected string of form `<key>=<value>[,<key>=<value>...]`; got `{s}`" )
81
+ } ) ?;
82
+ Ok ( ( k. to_string ( ) , v. to_string ( ) ) )
97
83
}
98
84
99
85
#[ derive( Default , Debug , Clone ) ]
@@ -166,8 +152,12 @@ pub struct Opts {
166
152
pub additional_derive_attributes : Vec < String > ,
167
153
168
154
/// Remapping of interface names to rust module names.
169
- #[ cfg_attr( feature = "clap" , arg( long, value_parser = parse_with, default_value = "" ) ) ]
170
- pub with : HashMap < String , String > ,
155
+ ///
156
+ /// Argument must be of the form `k=v` and this option can be passed
157
+ /// multiple times or one option can be comma separated, for example
158
+ /// `k1=v1,k2=v2`.
159
+ #[ cfg_attr( feature = "clap" , arg( long, value_parser = parse_with, value_delimiter = ',' ) ) ]
160
+ pub with : Vec < ( String , String ) > ,
171
161
172
162
/// Add the specified suffix to the name of the custome section containing
173
163
/// the component type.
@@ -284,7 +274,7 @@ impl RustWasm {
284
274
is_export : bool ,
285
275
) -> bool {
286
276
let with_name = resolve. name_world_key ( name) ;
287
- let entry = if let Some ( remapped_path) = self . opts . with . get ( & with_name) {
277
+ let entry = if let Some ( remapped_path) = self . with . get ( & with_name) {
288
278
let name = format ! ( "__with_name{}" , self . with_name_counter) ;
289
279
self . used_with_opts . insert ( with_name) ;
290
280
self . with_name_counter += 1 ;
@@ -870,10 +860,8 @@ impl WorldGenerator for RustWasm {
870
860
self . opts. additional_derive_attributes
871
861
) ;
872
862
}
873
- if !self . opts . with . is_empty ( ) {
874
- let mut with = self . opts . with . iter ( ) . collect :: < Vec < _ > > ( ) ;
875
- with. sort ( ) ;
876
- uwriteln ! ( self . src, "// * with {with:?}" ) ;
863
+ for ( k, v) in self . opts . with . iter ( ) {
864
+ uwriteln ! ( self . src, "// * with {k:?} = {v:?}" ) ;
877
865
}
878
866
if let Some ( default) = & self . opts . default_bindings_module {
879
867
uwriteln ! ( self . src, "// * default-bindings-module: {default:?}" ) ;
@@ -889,6 +877,10 @@ impl WorldGenerator for RustWasm {
889
877
}
890
878
self . types . analyze ( resolve) ;
891
879
self . world = Some ( world) ;
880
+
881
+ for ( k, v) in self . opts . with . iter ( ) {
882
+ self . with . insert ( k. clone ( ) , v. clone ( ) ) ;
883
+ }
892
884
}
893
885
894
886
fn import_interface (
@@ -1110,7 +1102,7 @@ impl WorldGenerator for RustWasm {
1110
1102
let module_name = name. to_snake_case ( ) ;
1111
1103
files. push ( & format ! ( "{module_name}.rs" ) , src. as_bytes ( ) ) ;
1112
1104
1113
- let remapping_keys = self . opts . with . keys ( ) . cloned ( ) . collect :: < HashSet < String > > ( ) ;
1105
+ let remapping_keys = self . with . keys ( ) . cloned ( ) . collect :: < HashSet < String > > ( ) ;
1114
1106
1115
1107
let mut unused_keys = remapping_keys
1116
1108
. difference ( & self . used_with_opts )
0 commit comments