@@ -92,7 +92,7 @@ pub use codegen::EnumVariation;
92
92
use std:: borrow:: Cow ;
93
93
use std:: fs:: { File , OpenOptions } ;
94
94
use std:: io:: { self , Write } ;
95
- use std:: iter;
95
+ use std:: { env , iter} ;
96
96
use std:: path:: { Path , PathBuf } ;
97
97
use std:: process:: { Command , Stdio } ;
98
98
use std:: sync:: Arc ;
@@ -1200,7 +1200,7 @@ impl Builder {
1200
1200
/// Generate the Rust bindings using the options built up thus far.
1201
1201
pub fn generate ( mut self ) -> Result < Bindings , ( ) > {
1202
1202
// Add any extra arguments from the environment to the clang command line.
1203
- if let Some ( extra_clang_args) = std :: env:: var ( "BINDGEN_EXTRA_CLANG_ARGS" ) . ok ( ) {
1203
+ if let Some ( extra_clang_args) = env:: var ( "BINDGEN_EXTRA_CLANG_ARGS" ) . ok ( ) {
1204
1204
// Try to parse it with shell quoting. If we fail, make it one single big argument.
1205
1205
if let Some ( strings) = shlex:: split ( & extra_clang_args) {
1206
1206
self . options . clang_args . extend ( strings) ;
@@ -1899,6 +1899,21 @@ impl Bindings {
1899
1899
Ok ( ( ) )
1900
1900
}
1901
1901
1902
+ /// Gets the rustfmt path to rustfmt the generated bindings.
1903
+ fn rustfmt_path < ' a > ( & ' a self ) -> io:: Result < Cow < ' a , PathBuf > > {
1904
+ debug_assert ! ( self . options. rustfmt_bindings) ;
1905
+ if let Some ( ref p) = self . options . rustfmt_path {
1906
+ return Ok ( Cow :: Borrowed ( p) ) ;
1907
+ }
1908
+ if let Ok ( rustfmt) = env:: var ( "RUSTFMT" ) {
1909
+ return Ok ( Cow :: Owned ( rustfmt. into ( ) ) ) ;
1910
+ }
1911
+ match which:: which ( "rustfmt" ) {
1912
+ Ok ( p) => Ok ( Cow :: Owned ( p) ) ,
1913
+ Err ( e) => Err ( io:: Error :: new ( io:: ErrorKind :: Other , format ! ( "{}" , e) ) ) ,
1914
+ }
1915
+ }
1916
+
1902
1917
/// Checks if rustfmt_bindings is set and runs rustfmt on the string
1903
1918
fn rustfmt_generated_string < ' a > (
1904
1919
& self ,
@@ -1911,18 +1926,7 @@ impl Bindings {
1911
1926
return Ok ( Cow :: Borrowed ( source) ) ;
1912
1927
}
1913
1928
1914
- let rustfmt = match self . options . rustfmt_path {
1915
- Some ( ref p) => Cow :: Borrowed ( p) ,
1916
- None => {
1917
- let path = which:: which ( "rustfmt" )
1918
- . map_err ( |e| {
1919
- io:: Error :: new ( io:: ErrorKind :: Other , format ! ( "{}" , e) )
1920
- } ) ?;
1921
-
1922
- Cow :: Owned ( path)
1923
- }
1924
- } ;
1925
-
1929
+ let rustfmt = self . rustfmt_path ( ) ?;
1926
1930
let mut cmd = Command :: new ( & * rustfmt) ;
1927
1931
1928
1932
cmd
0 commit comments