@@ -26,6 +26,7 @@ extern crate peeking_take_while;
26
26
extern crate regex;
27
27
#[ macro_use]
28
28
extern crate lazy_static;
29
+ extern crate which;
29
30
30
31
#[ cfg( feature = "logging" ) ]
31
32
#[ macro_use]
@@ -455,6 +456,16 @@ impl Builder {
455
456
) ;
456
457
}
457
458
459
+ if !self . options . format_bindings {
460
+ output_vector. push ( "--format-bindings" . into ( ) ) ;
461
+ }
462
+
463
+ if let Some ( path) = self . options . format_configuration_file . as_ref ( ) . and_then (
464
+ |f| f. to_str ( ) ) {
465
+ output_vector. push ( "--format-configuration-file" . into ( ) ) ;
466
+ output_vector. push ( path. into ( ) ) ;
467
+ }
468
+
458
469
output_vector
459
470
}
460
471
@@ -840,6 +851,19 @@ impl Builder {
840
851
self
841
852
}
842
853
854
+ /// Set whether rustfmt should format the generated bindings.
855
+ pub fn format_bindings ( mut self , doit : bool ) -> Self {
856
+ self . options . format_bindings = doit;
857
+ self
858
+ }
859
+
860
+ /// Set the absolute path to the rustfmt configuration file, if None, the standard rustfmt
861
+ /// options are used.
862
+ pub fn format_configuration_file ( mut self , path : Option < PathBuf > ) -> Self {
863
+ self . options . format_configuration_file = path;
864
+ self
865
+ }
866
+
843
867
/// Generate the Rust bindings using the options built up thus far.
844
868
pub fn generate < ' ctx > ( mut self ) -> Result < Bindings < ' ctx > , ( ) > {
845
869
self . options . input_header = self . input_headers . pop ( ) ;
@@ -1099,6 +1123,13 @@ pub struct BindgenOptions {
1099
1123
1100
1124
/// Features to enable, derived from `rust_target`
1101
1125
rust_features : RustFeatures ,
1126
+
1127
+ /// Whether rustfmt should format the generated bindings.
1128
+ pub format_bindings : bool ,
1129
+
1130
+ /// The absolute path to the rustfmt configuration file, if None, the standard rustfmt
1131
+ /// options are used.
1132
+ pub format_configuration_file : Option < PathBuf > ,
1102
1133
}
1103
1134
1104
1135
/// TODO(emilio): This is sort of a lie (see the error message that results from
@@ -1183,6 +1214,8 @@ impl Default for BindgenOptions {
1183
1214
objc_extern_crate : false ,
1184
1215
enable_mangling : true ,
1185
1216
prepend_enum_name : true ,
1217
+ format_bindings : true ,
1218
+ format_configuration_file : None ,
1186
1219
}
1187
1220
}
1188
1221
}
@@ -1334,14 +1367,19 @@ impl<'ctx> Bindings<'ctx> {
1334
1367
1335
1368
/// Write these bindings as source text to a file.
1336
1369
pub fn write_to_file < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
1337
- let file = try!(
1338
- OpenOptions :: new ( )
1339
- . write ( true )
1340
- . truncate ( true )
1341
- . create ( true )
1342
- . open ( path)
1343
- ) ;
1344
- self . write ( Box :: new ( file) )
1370
+ {
1371
+ let file = try!(
1372
+ OpenOptions :: new ( )
1373
+ . write ( true )
1374
+ . truncate ( true )
1375
+ . create ( true )
1376
+ . open ( path. as_ref ( ) )
1377
+ ) ;
1378
+ self . write ( Box :: new ( file) ) ?;
1379
+ }
1380
+
1381
+ self . format_generated_file ( path. as_ref ( ) ) ;
1382
+ Ok ( ( ) )
1345
1383
}
1346
1384
1347
1385
/// Write these bindings as source text to the given `Write`able.
@@ -1364,6 +1402,31 @@ impl<'ctx> Bindings<'ctx> {
1364
1402
try!( eof ( & mut ps. s ) ) ;
1365
1403
ps. s . out . flush ( )
1366
1404
}
1405
+
1406
+ /// Checks if format_bindings is set and runs rustfmt on the file
1407
+ fn format_generated_file ( & self , file : & Path ) {
1408
+ if !self . context . options ( ) . format_bindings {
1409
+ return ;
1410
+ }
1411
+
1412
+ let rustfmt = if let Ok ( rustfmt) = which:: which ( "rustfmt" ) {
1413
+ rustfmt
1414
+ } else {
1415
+ error ! ( "Could not find rustfmt in the global path." ) ;
1416
+ return ;
1417
+ } ;
1418
+
1419
+ let mut cmd = Command :: new ( rustfmt) ;
1420
+
1421
+ if let Some ( path) = self . context . options ( ) . format_configuration_file . as_ref ( ) . and_then (
1422
+ |f| f. to_str ( ) ) {
1423
+ cmd. args ( & [ "--config-path" , path] ) ;
1424
+ }
1425
+
1426
+ if let Err ( e) = cmd. arg ( file) . status ( ) {
1427
+ error ! ( "Error executing rustfmt (exit code: {:?})." , e) ;
1428
+ }
1429
+ }
1367
1430
}
1368
1431
1369
1432
/// Determines whether the given cursor is in any of the files matched by the
0 commit comments