@@ -92,7 +92,7 @@ pub use codegen::EnumVariation;
9292use std:: borrow:: Cow ;
9393use std:: fs:: { File , OpenOptions } ;
9494use std:: io:: { self , Write } ;
95- use std:: iter;
95+ use std:: { env , iter} ;
9696use std:: path:: { Path , PathBuf } ;
9797use std:: process:: { Command , Stdio } ;
9898use std:: sync:: Arc ;
@@ -1200,7 +1200,7 @@ impl Builder {
12001200 /// Generate the Rust bindings using the options built up thus far.
12011201 pub fn generate ( mut self ) -> Result < Bindings , ( ) > {
12021202 // 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 ( ) {
12041204 // Try to parse it with shell quoting. If we fail, make it one single big argument.
12051205 if let Some ( strings) = shlex:: split ( & extra_clang_args) {
12061206 self . options . clang_args . extend ( strings) ;
@@ -1899,6 +1899,21 @@ impl Bindings {
18991899 Ok ( ( ) )
19001900 }
19011901
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+
19021917 /// Checks if rustfmt_bindings is set and runs rustfmt on the string
19031918 fn rustfmt_generated_string < ' a > (
19041919 & self ,
@@ -1911,18 +1926,7 @@ impl Bindings {
19111926 return Ok ( Cow :: Borrowed ( source) ) ;
19121927 }
19131928
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 ( ) ?;
19261930 let mut cmd = Command :: new ( & * rustfmt) ;
19271931
19281932 cmd
0 commit comments