Skip to content

Commit 40d8ff7

Browse files
authored
Merge pull request #14 from dhil/wasmfx-merge
Merge with upstream
2 parents ec016d9 + 637c00c commit 40d8ff7

File tree

8 files changed

+59
-44
lines changed

8 files changed

+59
-44
lines changed

Cargo.lock

Lines changed: 17 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ heck = { version = "0.5" }
2727
pulldown-cmark = { version = "0.9", default-features = false }
2828
clap = { version = "4.3.19", features = ["derive"] }
2929
indexmap = "2.0.0"
30+
prettyplease = "0.2.20"
31+
syn = { version = "2.0", features = ["printing"] }
3032

3133
wasmparser = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.209.0" }
3234
wasm-metadata = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.209.0" }

crates/core/src/source.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ impl Source {
118118
pub fn as_mut_string(&mut self) -> &mut String {
119119
&mut self.s
120120
}
121+
122+
pub fn as_str(&self) -> &str {
123+
&self.s
124+
}
121125
}
122126

123127
impl Write for Source {

crates/guest-rust/macro/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ test = false
1717

1818
[dependencies]
1919
proc-macro2 = "1.0"
20-
syn = { version = "2.0", features = ["printing"] }
2120
quote = "1"
2221
wit-bindgen-core = { workspace = true }
2322
wit-bindgen-rust = { workspace = true }
2423
anyhow = { workspace = true }
24+
syn = { workspace = true }
25+
prettyplease = { workspace = true }
26+

crates/guest-rust/macro/src/lib.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,12 @@ impl Config {
191191
let n = INVOCATION.fetch_add(1, Relaxed);
192192
let path = root.join(format!("{world_name}{n}.rs"));
193193

194-
std::fs::write(&path, &src).unwrap();
195-
196194
// optimistically format the code but don't require success
197-
drop(
198-
std::process::Command::new("rustfmt")
199-
.arg(&path)
200-
.arg("--edition=2021")
201-
.output(),
202-
);
195+
let contents = match fmt(&src) {
196+
Ok(formatted) => formatted,
197+
Err(_) => src.clone(),
198+
};
199+
std::fs::write(&path, contents.as_bytes()).unwrap();
203200

204201
src = format!("include!({path:?});");
205202
}
@@ -472,3 +469,9 @@ fn with_field_parse(input: ParseStream<'_>) -> Result<(String, String)> {
472469

473470
Ok((interface, buf))
474471
}
472+
473+
/// Format a valid Rust string
474+
fn fmt(input: &str) -> Result<String> {
475+
let syntax_tree = syn::parse_file(&input)?;
476+
Ok(prettyplease::unparse(&syntax_tree))
477+
}

crates/rust/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ wasm-metadata = { workspace = true }
2323
heck = { workspace = true }
2424
clap = { workspace = true, optional = true }
2525
indexmap = { workspace = true }
26+
syn = { workspace = true }
27+
prettyplease = { workspace = true }
2628

2729
[dev-dependencies]
2830
wit-bindgen = { path = '../guest-rust' }

crates/rust/src/lib.rs

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ use heck::*;
44
use indexmap::IndexSet;
55
use std::collections::{BTreeMap, HashMap, HashSet};
66
use std::fmt::{self, Write as _};
7-
use std::io::{Read, Write};
87
use std::mem;
9-
use std::process::{Command, Stdio};
108
use std::str::FromStr;
119
use wit_bindgen_core::abi::{Bitcast, WasmType};
1210
use wit_bindgen_core::{
@@ -86,9 +84,9 @@ fn parse_with(s: &str) -> Result<(String, String), String> {
8684
#[derive(Default, Debug, Clone)]
8785
#[cfg_attr(feature = "clap", derive(clap::Args))]
8886
pub struct Opts {
89-
/// Whether or not `rustfmt` is executed to format generated code.
87+
/// Whether or not a formatter is executed to format generated code.
9088
#[cfg_attr(feature = "clap", arg(long))]
91-
pub rustfmt: bool,
89+
pub format: bool,
9290

9391
/// If true, code generation should qualify any features that depend on
9492
/// `std` with `cfg(feature = "std")`.
@@ -1022,28 +1020,9 @@ impl WorldGenerator for RustWasm {
10221020
}
10231021

10241022
let mut src = mem::take(&mut self.src);
1025-
if self.opts.rustfmt {
1026-
let mut child = Command::new("rustfmt")
1027-
.arg("--edition=2018")
1028-
.stdin(Stdio::piped())
1029-
.stdout(Stdio::piped())
1030-
.spawn()
1031-
.expect("failed to spawn `rustfmt`");
1032-
child
1033-
.stdin
1034-
.take()
1035-
.unwrap()
1036-
.write_all(src.as_bytes())
1037-
.unwrap();
1038-
src.as_mut_string().truncate(0);
1039-
child
1040-
.stdout
1041-
.take()
1042-
.unwrap()
1043-
.read_to_string(src.as_mut_string())
1044-
.unwrap();
1045-
let status = child.wait().unwrap();
1046-
assert!(status.success());
1023+
if self.opts.format {
1024+
let syntax_tree = syn::parse_file(src.as_str()).unwrap();
1025+
*src.as_mut_string() = prettyplease::unparse(&syntax_tree);
10471026
}
10481027

10491028
let module_name = name.to_snake_case();

src/bin/wit-bindgen.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,24 @@ struct Common {
7272
#[clap(long = "out-dir")]
7373
out_dir: Option<PathBuf>,
7474

75-
/// WIT document to generate bindings for.
76-
#[clap(value_name = "DOCUMENT", index = 1)]
75+
/// Location of WIT file(s) to generate bindings for.
76+
///
77+
/// This path can be either a directory containing `*.wit` files, a `*.wit`
78+
/// file itself, or a `*.wasm` file which is a wasm-encoded WIT package.
79+
/// Most of the time it's likely to be a directory containing `*.wit` files
80+
/// with an optional `deps` folder inside of it.
81+
#[clap(value_name = "WIT", index = 1)]
7782
wit: PathBuf,
7883

79-
/// World within the WIT document specified to generate bindings for.
84+
/// Optionally specified world that bindings are generated for.
8085
///
81-
/// This can either be `foo` which is the default world in document `foo` or
82-
/// it's `foo.bar` which is the world named `bar` within document `foo`.
86+
/// Bindings are always generated for a world but this option can be omitted
87+
/// when the WIT package pointed to by the `WIT` option only has a single
88+
/// world. If there's more than one world in the package then this option
89+
/// must be specified to name the world that bindings are generated for.
90+
/// This option can also use the fully qualified syntax such as
91+
/// `wasi:http/proxy` to select a world from a dependency of the main WIT
92+
/// package.
8393
#[clap(short, long)]
8494
world: Option<String>,
8595

0 commit comments

Comments
 (0)