@@ -17,14 +17,14 @@ extern crate cargo_metadata;
17
17
extern crate getopts;
18
18
extern crate serde_json as json;
19
19
20
- use std:: collections:: HashSet ;
20
+ use std:: collections:: { HashMap , HashSet } ;
21
21
use std:: env;
22
22
use std:: fs;
23
23
use std:: hash:: { Hash , Hasher } ;
24
24
use std:: io:: { self , Write } ;
25
25
use std:: iter:: FromIterator ;
26
26
use std:: path:: { Path , PathBuf } ;
27
- use std:: process:: { Command , ExitStatus } ;
27
+ use std:: process:: Command ;
28
28
use std:: str;
29
29
30
30
use getopts:: { Matches , Options } ;
@@ -122,30 +122,21 @@ pub enum Verbosity {
122
122
Quiet ,
123
123
}
124
124
125
- fn handle_command_status ( status : Result < ExitStatus , io:: Error > , opts : & getopts:: Options ) -> i32 {
125
+ fn handle_command_status ( status : Result < i32 , io:: Error > , opts : & getopts:: Options ) -> i32 {
126
126
match status {
127
127
Err ( e) => {
128
128
print_usage_to_stderr ( opts, & e. to_string ( ) ) ;
129
129
FAILURE
130
130
}
131
- Ok ( status) => {
132
- if status. success ( ) {
133
- SUCCESS
134
- } else {
135
- status. code ( ) . unwrap_or ( FAILURE )
136
- }
137
- }
131
+ Ok ( status) => status,
138
132
}
139
133
}
140
134
141
- fn get_version ( verbosity : Verbosity ) -> Result < ExitStatus , io:: Error > {
142
- run_rustfmt ( & [ ] , & [ String :: from ( "--version" ) ] , verbosity)
135
+ fn get_version ( verbosity : Verbosity ) -> Result < i32 , io:: Error > {
136
+ run_rustfmt ( & HashSet :: new ( ) , & [ String :: from ( "--version" ) ] , verbosity)
143
137
}
144
138
145
- fn format_crate (
146
- verbosity : Verbosity ,
147
- strategy : & CargoFmtStrategy ,
148
- ) -> Result < ExitStatus , io:: Error > {
139
+ fn format_crate ( verbosity : Verbosity , strategy : & CargoFmtStrategy ) -> Result < i32 , io:: Error > {
149
140
let rustfmt_args = get_fmt_args ( ) ;
150
141
let targets = if rustfmt_args
151
142
. iter ( )
@@ -157,17 +148,7 @@ fn format_crate(
157
148
} ;
158
149
159
150
// Currently only bin and lib files get formatted
160
- let files: Vec < _ > = targets
161
- . into_iter ( )
162
- . inspect ( |t| {
163
- if verbosity == Verbosity :: Verbose {
164
- println ! ( "[{}] {:?}" , t. kind, t. path)
165
- }
166
- } )
167
- . map ( |t| t. path )
168
- . collect ( ) ;
169
-
170
- run_rustfmt ( & files, & rustfmt_args, verbosity)
151
+ run_rustfmt ( & targets, & rustfmt_args, verbosity)
171
152
}
172
153
173
154
fn get_fmt_args ( ) -> Vec < String > {
@@ -182,6 +163,8 @@ pub struct Target {
182
163
path : PathBuf ,
183
164
/// A kind of target (e.g. lib, bin, example, ...).
184
165
kind : String ,
166
+ /// Rust edition for this target.
167
+ edition : String ,
185
168
}
186
169
187
170
impl Target {
@@ -192,6 +175,7 @@ impl Target {
192
175
Target {
193
176
path : canonicalized,
194
177
kind : target. kind [ 0 ] . clone ( ) ,
178
+ edition : target. edition . clone ( ) ,
195
179
}
196
180
}
197
181
}
@@ -334,41 +318,55 @@ fn add_targets(target_paths: &[cargo_metadata::Target], targets: &mut HashSet<Ta
334
318
}
335
319
336
320
fn run_rustfmt (
337
- files : & [ PathBuf ] ,
321
+ targets : & HashSet < Target > ,
338
322
fmt_args : & [ String ] ,
339
323
verbosity : Verbosity ,
340
- ) -> Result < ExitStatus , io:: Error > {
341
- let stdout = if verbosity == Verbosity :: Quiet {
342
- std:: process:: Stdio :: null ( )
343
- } else {
344
- std:: process:: Stdio :: inherit ( )
345
- } ;
324
+ ) -> Result < i32 , io:: Error > {
325
+ let by_edition: HashMap < _ , _ > = targets
326
+ . iter ( )
327
+ . inspect ( |t| {
328
+ if verbosity == Verbosity :: Verbose {
329
+ println ! ( "[{} ({})] {:?}" , t. kind, t. edition, t. path)
330
+ }
331
+ } )
332
+ . map ( |t| ( & t. edition , vec ! [ & t. path] ) )
333
+ . collect ( ) ;
346
334
347
- if verbosity == Verbosity :: Verbose {
348
- print ! ( "rustfmt" ) ;
349
- for a in fmt_args {
350
- print ! ( " {}" , a) ;
335
+ for ( edition, files) in by_edition {
336
+ let stdout = if verbosity == Verbosity :: Quiet {
337
+ std:: process:: Stdio :: null ( )
338
+ } else {
339
+ std:: process:: Stdio :: inherit ( )
340
+ } ;
341
+
342
+ if verbosity == Verbosity :: Verbose {
343
+ print ! ( "rustfmt" ) ;
344
+ fmt_args. iter ( ) . for_each ( |f| print ! ( " {}" , f) ) ;
345
+ files. iter ( ) . for_each ( |f| print ! ( " {}" , f. display( ) ) ) ;
346
+ println ! ( ) ;
351
347
}
352
- for f in files {
353
- print ! ( " {}" , f. display( ) ) ;
348
+
349
+ let mut command = Command :: new ( "rustfmt" )
350
+ . stdout ( stdout)
351
+ . args ( files)
352
+ . args ( & [ "--edition" , edition] )
353
+ . args ( fmt_args)
354
+ . spawn ( )
355
+ . map_err ( |e| match e. kind ( ) {
356
+ io:: ErrorKind :: NotFound => io:: Error :: new (
357
+ io:: ErrorKind :: Other ,
358
+ "Could not run rustfmt, please make sure it is in your PATH." ,
359
+ ) ,
360
+ _ => e,
361
+ } ) ?;
362
+
363
+ let status = command. wait ( ) ?;
364
+ if !status. success ( ) {
365
+ return Ok ( status. code ( ) . unwrap_or ( FAILURE ) ) ;
354
366
}
355
- println ! ( ) ;
356
367
}
357
368
358
- let mut command = Command :: new ( "rustfmt" )
359
- . stdout ( stdout)
360
- . args ( files)
361
- . args ( fmt_args)
362
- . spawn ( )
363
- . map_err ( |e| match e. kind ( ) {
364
- io:: ErrorKind :: NotFound => io:: Error :: new (
365
- io:: ErrorKind :: Other ,
366
- "Could not run rustfmt, please make sure it is in your PATH." ,
367
- ) ,
368
- _ => e,
369
- } ) ?;
370
-
371
- command. wait ( )
369
+ Ok ( SUCCESS )
372
370
}
373
371
374
372
fn get_cargo_metadata ( manifest_path : Option < & Path > ) -> Result < cargo_metadata:: Metadata , io:: Error > {
0 commit comments