Skip to content

Commit 29c1079

Browse files
authored
Support multiple --with arguments in the Rust generator (bytecodealliance#875)
* Support multiple `--with` arguments in the Rust generator Don't require one large `--with`, support having multiple arguments. * Remove stray debug
1 parent 9798f56 commit 29c1079

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

crates/rust/src/lib.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct RustWasm {
4545

4646
rt_module: IndexSet<RuntimeItem>,
4747
export_macros: Vec<(String, String)>,
48+
with: HashMap<String, String>,
4849
}
4950

5051
#[derive(PartialEq, Eq, Clone, Copy, Hash, Debug)]
@@ -67,33 +68,18 @@ enum RuntimeItem {
6768
BoxType,
6869
}
6970

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-
7971
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
8072
pub enum ExportKey {
8173
World,
8274
Name(String),
8375
}
8476

8577
#[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()))
9783
}
9884

9985
#[derive(Default, Debug, Clone)]
@@ -166,8 +152,12 @@ pub struct Opts {
166152
pub additional_derive_attributes: Vec<String>,
167153

168154
/// 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)>,
171161

172162
/// Add the specified suffix to the name of the custome section containing
173163
/// the component type.
@@ -284,7 +274,7 @@ impl RustWasm {
284274
is_export: bool,
285275
) -> bool {
286276
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) {
288278
let name = format!("__with_name{}", self.with_name_counter);
289279
self.used_with_opts.insert(with_name);
290280
self.with_name_counter += 1;
@@ -870,10 +860,8 @@ impl WorldGenerator for RustWasm {
870860
self.opts.additional_derive_attributes
871861
);
872862
}
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:?}");
877865
}
878866
if let Some(default) = &self.opts.default_bindings_module {
879867
uwriteln!(self.src, "// * default-bindings-module: {default:?}");
@@ -889,6 +877,10 @@ impl WorldGenerator for RustWasm {
889877
}
890878
self.types.analyze(resolve);
891879
self.world = Some(world);
880+
881+
for (k, v) in self.opts.with.iter() {
882+
self.with.insert(k.clone(), v.clone());
883+
}
892884
}
893885

894886
fn import_interface(
@@ -1110,7 +1102,7 @@ impl WorldGenerator for RustWasm {
11101102
let module_name = name.to_snake_case();
11111103
files.push(&format!("{module_name}.rs"), src.as_bytes());
11121104

1113-
let remapping_keys = self.opts.with.keys().cloned().collect::<HashSet<String>>();
1105+
let remapping_keys = self.with.keys().cloned().collect::<HashSet<String>>();
11141106

11151107
let mut unused_keys = remapping_keys
11161108
.difference(&self.used_with_opts)

0 commit comments

Comments
 (0)