File tree Expand file tree Collapse file tree 5 files changed +59
-24
lines changed
Expand file tree Collapse file tree 5 files changed +59
-24
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,16 @@ impl Config<'_> {
2323 } ;
2424 let tokens = format ! ( "{preamble}{allow}{tokens}" ) ;
2525
26+ if let Some ( result) = self . rustfmt ( & tokens) {
27+ result
28+ } else {
29+ self . warnings
30+ . add ( "failed to format output with `rustfmt`" . to_string ( ) ) ;
31+ tokens
32+ }
33+ }
34+
35+ fn rustfmt ( & self , tokens : & str ) -> Option < String > {
2636 let mut cmd = std:: process:: Command :: new ( "rustfmt" ) ;
2737 cmd. stdin ( std:: process:: Stdio :: piped ( ) ) ;
2838 cmd. stdout ( std:: process:: Stdio :: piped ( ) ) ;
@@ -33,32 +43,16 @@ impl Config<'_> {
3343 cmd. arg ( self . rustfmt ) ;
3444 }
3545
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-
46+ let mut child = cmd. spawn ( ) . ok ( ) ?;
47+ let mut stdin = child. stdin . take ( ) ?;
48+ stdin. write_all ( tokens. as_bytes ( ) ) . ok ( ) ?;
4849 drop ( stdin) ;
49-
50- let Ok ( output) = child. wait_with_output ( ) else {
51- return tokens;
52- } ;
50+ let output = child. wait_with_output ( ) . ok ( ) ?;
5351
5452 if !output. status . success ( ) {
55- return tokens ;
53+ return None ;
5654 }
5755
58- if let Ok ( result) = String :: from_utf8 ( output. stdout ) {
59- result
60- } else {
61- tokens
62- }
56+ String :: from_utf8 ( output. stdout ) . ok ( )
6357 }
6458}
Original file line number Diff line number Diff line change 11// This package was generated by `tool_bindgen`.
22
3+ #[ rustfmt:: skip] pub mod rustfmt_25;
34pub mod bool;
45pub mod bool_event;
56pub mod bool_event_sans_reference;
Original file line number Diff line number Diff line change 1+ #[ repr( C ) ]
2+ #[ derive(
3+ Clone ,
4+ Copy ,
5+ Debug ,
6+ Default ,
7+ PartialEq ,
8+ ) ]
9+ pub struct POINT {
10+ pub x : i32 ,
11+ pub y : i32 ,
12+ }
Original file line number Diff line number Diff line change @@ -182,3 +182,20 @@ fn skip_method() {
182182 ] )
183183 . unwrap ( ) ;
184184}
185+
186+ #[ test]
187+ #[ should_panic( expected = "failed to format output with `rustfmt`" ) ]
188+ fn rustfmt_failure ( ) {
189+ let mut path = std:: env:: temp_dir ( ) ;
190+ path. push ( "rustfmt_failure" ) ;
191+
192+ windows_bindgen:: bindgen ( [
193+ "--out" ,
194+ & path. to_string_lossy ( ) ,
195+ "--rustfmt" ,
196+ "fail=true" ,
197+ "--filter" ,
198+ "POINT" ,
199+ ] )
200+ . unwrap ( ) ;
201+ }
Original file line number Diff line number Diff line change @@ -176,6 +176,9 @@ fn main() {
176176 "--out comment_no_allow.rs --in default --filter GetTickCount --sys --flat --no-allow" ,
177177 ) ;
178178
179+ // Tests for rustfmt
180+ bindgen ( "--out rustfmt_25.rs --rustfmt max_width=25,newline_style=Unix --filter POINT --flat --no-comment --no-allow" . split_whitespace ( ) ) . unwrap ( ) ;
181+
179182 write_lib ( ) ;
180183 println ! ( "Finished in {:.2}s" , time. elapsed( ) . as_secs_f32( ) ) ;
181184}
@@ -185,7 +188,7 @@ fn main() {
185188// those modules.
186189
187190fn write_lib ( ) {
188- let mods = std:: fs:: read_dir ( "." )
191+ let mut mods: Vec < String > = std:: fs:: read_dir ( "." )
189192 . unwrap ( )
190193 . flatten ( )
191194 . filter_map ( |entry| {
@@ -195,13 +198,21 @@ fn write_lib() {
195198
196199 if path == "lib.rs" {
197200 None
201+ } else if path. starts_with ( "rustfmt" ) {
202+ Some ( format ! (
203+ "\n #[rustfmt::skip] pub mod {};" ,
204+ path. strip_suffix( ".rs" ) . unwrap( )
205+ ) )
198206 } else {
199207 Some ( format ! ( "\n pub mod {};" , path. strip_suffix( ".rs" ) . unwrap( ) ) )
200208 }
201209 } else {
202210 None
203211 }
204- } ) ;
212+ } )
213+ . collect ( ) ;
214+
215+ mods. sort ( ) ;
205216
206217 let mut lib_rs = "// This package was generated by `tool_bindgen`.\n " . to_string ( ) ;
207218 lib_rs. extend ( mods) ;
You can’t perform that action at this time.
0 commit comments