11use std:: env;
2+ use std:: fs:: OpenOptions ;
23use std:: io;
34use std:: path:: { Path , PathBuf } ;
45use std:: process:: Command ;
56use std:: str:: Utf8Error ;
67use tracing:: info;
8+ use walkdir:: WalkDir ;
79
810pub enum CheckDiffError {
911 /// Git related errors
@@ -18,6 +20,9 @@ pub enum CheckDiffError {
1820 FailedBinaryVersioning ( PathBuf ) ,
1921 /// Error when obtaining cargo version
2022 FailedCargoVersion ( & ' static str ) ,
23+ /// Error when trying to find rust files
24+ FailedFindingRustFiles ( & ' static str ) ,
25+ FailedWritingToFile ( & ' static str ) ,
2126 IO ( std:: io:: Error ) ,
2227}
2328
@@ -88,7 +93,12 @@ impl RustfmtRunner {
8893 // output_path: Output file path for the diff
8994 // config: Any additional configuration options to pass to rustfmt
9095 //
91- fn create_diff ( & self , output_path : & Path , config : Option < Vec < String > > ) {
96+ #[ allow( dead_code) ]
97+ fn create_diff (
98+ & self ,
99+ output_path : & Path ,
100+ config : Option < Vec < String > > ,
101+ ) -> Result < ( ) , CheckDiffError > {
92102 let config_arg: String = match config {
93103 Some ( configs) => {
94104 let mut result = String :: new ( ) ;
@@ -106,6 +116,34 @@ impl RustfmtRunner {
106116 "error_on_line_overflow=false,error_on_unformatted=false{}" ,
107117 config_arg. as_str( )
108118 ) ;
119+
120+ // walks the "." directory and finds all files with .rs extensions
121+ for entry in WalkDir :: new ( "." ) . into_iter ( ) . filter_map ( |e| e. ok ( ) ) {
122+ let path = entry. path ( ) ;
123+ if path. is_file ( ) && path. extension ( ) . map_or ( false , |ext| ext == "rs" ) {
124+ let file = OpenOptions :: new ( )
125+ . write ( true )
126+ . append ( true )
127+ . open ( output_path) ?;
128+ let Ok ( _) = Command :: new ( & self . binary_path )
129+ . env ( "LD_LIBRARY_PATH" , & self . ld_library_path )
130+ . args ( [
131+ "--unstable-features" ,
132+ "--skip-children" ,
133+ "--check" ,
134+ "--color=always" ,
135+ config. as_str ( ) ,
136+ ] )
137+ . stdout ( file)
138+ . output ( )
139+ else {
140+ return Err ( CheckDiffError :: FailedWritingToFile (
141+ "Failed to write to file" ,
142+ ) ) ;
143+ } ;
144+ }
145+ }
146+ Ok ( ( ) )
109147 }
110148}
111149
0 commit comments