Skip to content

Commit 5ab5480

Browse files
author
Guy Bedford
authored
cli: default naming, default string encoding flags (#399)
* cli: default naming, default string encoding flags * default c to utf8 * use stringencoding default, only infer name for default flag
1 parent 6ed55f3 commit 5ab5480

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

crates/gen-guest-c/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct Opts {
3131
#[cfg_attr(feature = "clap", arg(long))]
3232
pub no_helpers: bool,
3333
/// Set component string encoding
34-
#[cfg_attr(feature = "clap", arg(long))]
34+
#[cfg_attr(feature = "clap", arg(long, default_value_t = StringEncoding::default()))]
3535
pub string_encoding: StringEncoding,
3636
}
3737

crates/wit-component/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![deny(missing_docs)]
44

55
use anyhow::{bail, Result};
6+
use std::fmt::Display;
67
use std::str::FromStr;
78
use wasm_encoder::CanonicalOption;
89

@@ -37,6 +38,16 @@ impl Default for StringEncoding {
3738
}
3839
}
3940

41+
impl Display for StringEncoding {
42+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43+
match self {
44+
StringEncoding::UTF8 => write!(f, "utf8"),
45+
StringEncoding::UTF16 => write!(f, "utf16"),
46+
StringEncoding::CompactUTF16 => write!(f, "compact-utf16"),
47+
}
48+
}
49+
}
50+
4051
impl FromStr for StringEncoding {
4152
type Err = anyhow::Error;
4253

src/bin/wit-bindgen.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ struct World {
126126
/// The top-level name of the generated bindings, which may be used for
127127
/// naming modules/files/etc.
128128
#[clap(long, short)]
129-
name: String,
129+
name: Option<String>,
130130
}
131131

132132
fn parse_named_interface(s: &str) -> Result<Interface> {
@@ -283,7 +283,19 @@ fn gen_world(
283283
exports,
284284
default: world.default,
285285
};
286-
generator.generate(&world.name, &interfaces, files);
286+
let name = match &world.name {
287+
Some(name) => name,
288+
None => {
289+
if let Some(default) = &interfaces.default {
290+
&default.name
291+
} else {
292+
return Err(anyhow!(
293+
"--name flag is required unless setting the interface via --default"
294+
));
295+
}
296+
}
297+
};
298+
generator.generate(name, &interfaces, files);
287299
Ok(())
288300
}
289301

0 commit comments

Comments
 (0)