Skip to content

Commit

Permalink
Make binding generation use the same rustfmt config
Browse files Browse the repository at this point in the history
  • Loading branch information
Arlie Davis committed Jun 3, 2024
1 parent dcefad6 commit a3ec2b3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions crates/libs/bindgen/src/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ pub fn from_reader(
writer.no_inner_attributes = config.remove("no-inner-attributes").is_some();
writer.no_bindgen_comment = config.remove("no-bindgen-comment").is_some();
writer.vtbl = config.remove("vtbl").is_some();
writer.rustfmt_config = if let Some(config) = config.remove("rustfmt-config") {
config.to_string()
} else {
String::new()
};

if writer.package && writer.flatten {
return Err(Error::new(
Expand Down
17 changes: 11 additions & 6 deletions crates/libs/bindgen/src/rust/try_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ pub fn try_format(writer: &super::Writer, tokens: &str) -> String {
};
let tokens = format!("{preamble}{allow}{tokens}");

let Ok(mut child) = std::process::Command::new("rustfmt")
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::null())
.spawn()
else {
let mut cmd = std::process::Command::new("rustfmt");
cmd.stdin(std::process::Stdio::piped());
cmd.stdout(std::process::Stdio::piped());
cmd.stderr(std::process::Stdio::null());

if !writer.rustfmt_config.is_empty() {
cmd.arg("--config");
cmd.arg(writer.rustfmt_config.as_str());
}

let Ok(mut child) = cmd.spawn() else {
return tokens;
};

Expand Down
3 changes: 3 additions & 0 deletions crates/libs/bindgen/src/rust/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub struct Writer {
pub no_bindgen_comment: bool, // skips the bindgen comment at the start of the file
pub vtbl: bool, // include minimal vtbl layout support for interfaces
pub prepend: std::collections::HashMap<metadata::TypeDef, String>,
/// If this is not empty, then it is passed to rustfmt in a `--config` argument.
pub rustfmt_config: String,
}

impl Writer {
Expand All @@ -40,6 +42,7 @@ impl Writer {
no_bindgen_comment: false,
vtbl: false,
prepend: Default::default(),
rustfmt_config: String::new(),
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/tools/sys/bindings.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
--in crates/libs/bindgen/default
--out crates/libs/sys/src/lib.rs
--config package sys no-bindgen-comment
--config rustfmt-config=max_width=800,newline_style=Unix

--filter
Windows.Wdk
Expand Down
1 change: 1 addition & 0 deletions crates/tools/windows/bindings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
--config prepend:Windows.Win32.Foundation.BOOL=#[must_use]
--config prepend:Windows.Win32.Foundation.NTSTATUS=#[must_use]
--config prepend:Windows.Win32.System.Rpc.RPC_STATUS=#[must_use]
--config rustfmt-config=max_width=800,newline_style=Unix

--filter
Windows
Expand Down

0 comments on commit a3ec2b3

Please sign in to comment.