@@ -81,17 +81,38 @@ impl Diff {
8181}
8282
8383// will be used in future PRs, just added to make the compiler happy
84- pub struct CheckDiffRunners {
85- feature_runner : RustfmtRunner ,
86- src_runner : RustfmtRunner ,
84+ pub struct CheckDiffRunners < F , S > {
85+ feature_runner : F ,
86+ src_runner : S ,
87+ }
88+
89+ pub trait CodeFormatter {
90+ fn format_code < ' a > (
91+ & self ,
92+ code : & ' a str ,
93+ config : & Option < Vec < String > > ,
94+ ) -> Result < String , CheckDiffError > ;
8795}
8896
8997pub struct RustfmtRunner {
9098 ld_library_path : String ,
9199 binary_path : PathBuf ,
92100}
93101
94- impl CheckDiffRunners {
102+ impl < F , S > CheckDiffRunners < F , S > {
103+ pub fn new ( feature_runner : F , src_runner : S ) -> Self {
104+ Self {
105+ feature_runner,
106+ src_runner,
107+ }
108+ }
109+ }
110+
111+ impl < F , S > CheckDiffRunners < F , S >
112+ where
113+ F : CodeFormatter ,
114+ S : CodeFormatter ,
115+ {
95116 /// Creates a diff generated by running the source and feature binaries on the same file path
96117 pub fn create_diff (
97118 & self ,
@@ -106,14 +127,6 @@ impl CheckDiffRunners {
106127 feature_format,
107128 } )
108129 }
109-
110- pub fn get_feature_runner ( self ) -> RustfmtRunner {
111- self . feature_runner
112- }
113-
114- pub fn get_src_runner ( self ) -> RustfmtRunner {
115- self . src_runner
116- }
117130}
118131
119132impl RustfmtRunner {
@@ -131,14 +144,16 @@ impl RustfmtRunner {
131144 let binary_version = std:: str:: from_utf8 ( & command. stdout ) ?. trim ( ) ;
132145 return Ok ( binary_version. to_string ( ) ) ;
133146 }
147+ }
134148
149+ impl CodeFormatter for RustfmtRunner {
135150 // Run rusfmt to see if a diff is produced. Runs on the code specified
136151 //
137152 // Parameters:
138153 // code: Code to run the binary on
139154 // config: Any additional configuration options to pass to rustfmt
140155 //
141- pub fn format_code < ' a > (
156+ fn format_code < ' a > (
142157 & self ,
143158 code : & ' a str ,
144159 config : & Option < Vec < String > > ,
@@ -281,8 +296,12 @@ pub fn change_directory_to_path(dest: &Path) -> io::Result<()> {
281296 return Ok ( ( ) ) ;
282297}
283298
284- pub fn get_ld_library_path ( ) -> Result < String , CheckDiffError > {
285- let Ok ( command) = Command :: new ( "rustc" ) . args ( [ "--print" , "sysroot" ] ) . output ( ) else {
299+ pub fn get_ld_library_path ( dir : & Path ) -> Result < String , CheckDiffError > {
300+ let Ok ( command) = Command :: new ( "rustc" )
301+ . current_dir ( dir)
302+ . args ( [ "--print" , "sysroot" ] )
303+ . output ( )
304+ else {
286305 return Err ( CheckDiffError :: FailedCommand ( "Error getting sysroot" ) ) ;
287306 } ;
288307 let sysroot = std:: str:: from_utf8 ( & command. stdout ) ?. trim_end ( ) ;
@@ -303,15 +322,19 @@ pub fn get_cargo_version() -> Result<String, CheckDiffError> {
303322
304323/// Obtains the ld_lib path and then builds rustfmt from source
305324/// If that operation succeeds, the source is then copied to the output path specified
306- pub fn build_rustfmt_from_src ( binary_path : PathBuf ) -> Result < RustfmtRunner , CheckDiffError > {
325+ pub fn build_rustfmt_from_src (
326+ binary_path : PathBuf ,
327+ dir : & Path ,
328+ ) -> Result < RustfmtRunner , CheckDiffError > {
307329 //Because we're building standalone binaries we need to set `LD_LIBRARY_PATH` so each
308330 // binary can find it's runtime dependencies.
309331 // See https://github.com/rust-lang/rustfmt/issues/5675
310332 // This will prepend the `LD_LIBRARY_PATH` for the master rustfmt binary
311- let ld_lib_path = get_ld_library_path ( ) ?;
333+ let ld_lib_path = get_ld_library_path ( & dir ) ?;
312334
313335 info ! ( "Building rustfmt from source" ) ;
314336 let Ok ( _) = Command :: new ( "cargo" )
337+ . current_dir ( dir)
315338 . args ( [ "build" , "-q" , "--release" , "--bin" , "rustfmt" ] )
316339 . output ( )
317340 else {
@@ -320,7 +343,7 @@ pub fn build_rustfmt_from_src(binary_path: PathBuf) -> Result<RustfmtRunner, Che
320343 ) ) ;
321344 } ;
322345
323- std:: fs:: copy ( "target/release/rustfmt" , & binary_path) ?;
346+ std:: fs:: copy ( dir . join ( "target/release/rustfmt" ) , & binary_path) ?;
324347
325348 return Ok ( RustfmtRunner {
326349 ld_library_path : ld_lib_path,
@@ -337,7 +360,7 @@ pub fn compile_rustfmt(
337360 remote_repo_url : String ,
338361 feature_branch : String ,
339362 commit_hash : Option < String > ,
340- ) -> Result < CheckDiffRunners , CheckDiffError > {
363+ ) -> Result < CheckDiffRunners < RustfmtRunner , RustfmtRunner > , CheckDiffError > {
341364 const RUSTFMT_REPO : & str = "https://github.com/rust-lang/rustfmt.git" ;
342365
343366 clone_git_repo ( RUSTFMT_REPO , dest) ?;
@@ -347,14 +370,14 @@ pub fn compile_rustfmt(
347370
348371 let cargo_version = get_cargo_version ( ) ?;
349372 info ! ( "Compiling with {}" , cargo_version) ;
350- let src_runner = build_rustfmt_from_src ( dest. join ( "src_rustfmt" ) ) ?;
373+ let src_runner = build_rustfmt_from_src ( dest. join ( "src_rustfmt" ) , dest ) ?;
351374 let should_detach = commit_hash. is_some ( ) ;
352375 git_switch (
353376 commit_hash. unwrap_or ( feature_branch) . as_str ( ) ,
354377 should_detach,
355378 ) ?;
356379
357- let feature_runner = build_rustfmt_from_src ( dest. join ( "feature_rustfmt" ) ) ?;
380+ let feature_runner = build_rustfmt_from_src ( dest. join ( "feature_rustfmt" ) , dest ) ?;
358381 info ! ( "RUSFMT_BIN {}" , src_runner. get_binary_version( ) ?) ;
359382 info ! (
360383 "Runtime dependencies for (src) rustfmt -- LD_LIBRARY_PATH: {}" ,
@@ -388,7 +411,11 @@ pub fn search_for_rs_files(repo: &Path) -> impl Iterator<Item = PathBuf> {
388411
389412/// Calculates the number of errors when running the compiled binary and the feature binary on the
390413/// repo specified with the specific configs.
391- pub fn check_diff ( config : Option < Vec < String > > , runners : CheckDiffRunners , repo : & Path ) -> i32 {
414+ pub fn check_diff (
415+ config : Option < Vec < String > > ,
416+ runners : CheckDiffRunners < RustfmtRunner , RustfmtRunner > ,
417+ repo : & Path ,
418+ ) -> i32 {
392419 let mut errors = 0 ;
393420 let iter = search_for_rs_files ( repo) ;
394421 for file in iter {
0 commit comments