Skip to content

Commit 58ade72

Browse files
authored
replace rustfmt with prettyplease for codegen (#971)
* replace `rustfmt` with `prettyplease` for codegen * rename the `rustfmt` cli option to `format`
1 parent 76c5401 commit 58ade72

File tree

7 files changed

+44
-39
lines changed

7 files changed

+44
-39
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 = "0.209.0"
3234
wasm-encoder = "0.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();

0 commit comments

Comments
 (0)