@@ -1382,8 +1382,7 @@ impl<'ctx> Bindings<'ctx> {
1382
1382
self . write ( Box :: new ( file) ) ?;
1383
1383
}
1384
1384
1385
- self . format_generated_file ( path. as_ref ( ) ) ;
1386
- Ok ( ( ) )
1385
+ self . rustfmt_generated_file ( path. as_ref ( ) )
1387
1386
}
1388
1387
1389
1388
/// Write these bindings as source text to the given `Write`able.
@@ -1407,28 +1406,60 @@ impl<'ctx> Bindings<'ctx> {
1407
1406
ps. s . out . flush ( )
1408
1407
}
1409
1408
1410
- /// Checks if format_bindings is set and runs rustfmt on the file
1411
- fn format_generated_file ( & self , file : & Path ) {
1412
- if !self . context . options ( ) . format_bindings {
1413
- return ;
1409
+ /// Checks if rustfmt_bindings is set and runs rustfmt on the file
1410
+ fn rustfmt_generated_file ( & self , file : & Path ) -> io :: Result < ( ) > {
1411
+ if !self . context . options ( ) . rustfmt_bindings {
1412
+ return Ok ( ( ) ) ;
1414
1413
}
1415
1414
1416
1415
let rustfmt = if let Ok ( rustfmt) = which:: which ( "rustfmt" ) {
1417
1416
rustfmt
1418
1417
} else {
1419
- error ! ( "Could not find rustfmt in the global path." ) ;
1420
- return ;
1418
+ return Err ( io:: Error :: new (
1419
+ io:: ErrorKind :: Other ,
1420
+ "Rustfmt activated, but it could not be found in global path." ,
1421
+ ) ) ;
1421
1422
} ;
1422
1423
1423
1424
let mut cmd = Command :: new ( rustfmt) ;
1424
1425
1425
- if let Some ( path) = self . context . options ( ) . format_configuration_file . as_ref ( ) . and_then (
1426
- |f| f. to_str ( ) ) {
1426
+ if let Some ( path) = self . context
1427
+ . options ( )
1428
+ . rustfmt_configuration_file
1429
+ . as_ref ( )
1430
+ . and_then ( |f| f. to_str ( ) )
1431
+ {
1427
1432
cmd. args ( & [ "--config-path" , path] ) ;
1428
1433
}
1429
1434
1430
- if let Err ( e) = cmd. arg ( file) . status ( ) {
1431
- error ! ( "Error executing rustfmt (exit code: {:?})." , e) ;
1435
+ if let Ok ( output) = cmd. arg ( file) . output ( ) {
1436
+ if !output. status . success ( ) {
1437
+ let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
1438
+ match output. status . code ( ) {
1439
+ Some ( 2 ) => Err ( io:: Error :: new (
1440
+ io:: ErrorKind :: Other ,
1441
+ format ! ( "Rustfmt parsing errors:\n {}" , stderr) ,
1442
+ ) ) ,
1443
+ Some ( 3 ) => {
1444
+ warn ! (
1445
+ "Rustfmt could not format some lines:\n {}" ,
1446
+ stderr
1447
+ ) ;
1448
+ Ok ( ( ) )
1449
+ }
1450
+ _ => Err ( io:: Error :: new (
1451
+ io:: ErrorKind :: Other ,
1452
+ format ! ( "Internal rustfmt error:\n {}" , stderr) ,
1453
+ ) ) ,
1454
+ }
1455
+ } else {
1456
+ Ok ( ( ) )
1457
+ }
1458
+ } else {
1459
+ Err ( io:: Error :: new (
1460
+ io:: ErrorKind :: Other ,
1461
+ "Error executing rustfmt!" ,
1462
+ ) )
1432
1463
}
1433
1464
}
1434
1465
}
0 commit comments