1use super::*;
2use std::io::Write;
3
4impl Writer {
5 pub fn format(&self, tokens: &str) -> String {
6 let preamble = if self.config.no_comment {
7 String::new()
8 } else {
9 let version = std::env!("CARGO_PKG_VERSION");
10
11 format!(
12 r#"// Bindings generated by `windows-bindgen` {version}
13
14
15"#
16 )
17 };
18
19 let allow = if self.config.no_allow {
20 ""
21 } else {
22 "#![allow(non_snake_case, non_upper_case_globals, non_camel_case_types, dead_code, clippy::all)]\n\n"
23 };
24 let tokens = format!("{preamble}{allow}{tokens}");
25
26 let mut cmd = std::process::Command::new("rustfmt");
27 cmd.stdin(std::process::Stdio::piped());
28 cmd.stdout(std::process::Stdio::piped());
29 cmd.stderr(std::process::Stdio::null());
30
31 if !self.config.rustfmt.is_empty() {
32 cmd.arg("--config");
33 cmd.arg(&self.config.rustfmt);
34 }
35
36 let Ok(mut child) = cmd.spawn() else {
37 return tokens;
38 };
39
40 let Some(mut stdin) = child.stdin.take() else {
41 return tokens;
42 };
43
44 if stdin.write_all(tokens.as_bytes()).is_err() {
45 return tokens;
46 }
47
48 drop(stdin);
49
50 let Ok(output) = child.wait_with_output() else {
51 return tokens;
52 };
53
54 if !output.status.success() {
55 return tokens;
56 }
57
58 if let Ok(result) = String::from_utf8(output.stdout) {
59 result
60 } else {
61 tokens
62 }
63 }
64}
65