6
6
use cargo_metadata;
7
7
use getopts;
8
8
9
- use std:: collections:: { HashMap , HashSet } ;
9
+ use std:: cmp:: Ordering ;
10
+ use std:: collections:: { BTreeMap , BTreeSet } ;
10
11
use std:: env;
11
12
use std:: fs;
12
13
use std:: hash:: { Hash , Hasher } ;
@@ -122,7 +123,7 @@ fn handle_command_status(status: Result<i32, io::Error>, opts: &getopts::Options
122
123
}
123
124
124
125
fn get_version ( verbosity : Verbosity ) -> Result < i32 , io:: Error > {
125
- run_rustfmt ( & HashSet :: new ( ) , & [ String :: from ( "--version" ) ] , verbosity)
126
+ run_rustfmt ( & BTreeSet :: new ( ) , & [ String :: from ( "--version" ) ] , verbosity)
126
127
}
127
128
128
129
fn format_crate ( verbosity : Verbosity , strategy : & CargoFmtStrategy ) -> Result < i32 , io:: Error > {
@@ -131,7 +132,7 @@ fn format_crate(verbosity: Verbosity, strategy: &CargoFmtStrategy) -> Result<i32
131
132
. iter ( )
132
133
. any ( |s| [ "--print-config" , "-h" , "--help" , "-V" , "--version" ] . contains ( & s. as_str ( ) ) )
133
134
{
134
- HashSet :: new ( )
135
+ BTreeSet :: new ( )
135
136
} else {
136
137
get_targets ( strategy) ?
137
138
} ;
@@ -175,6 +176,18 @@ impl PartialEq for Target {
175
176
}
176
177
}
177
178
179
+ impl PartialOrd for Target {
180
+ fn partial_cmp ( & self , other : & Target ) -> Option < Ordering > {
181
+ Some ( self . path . cmp ( & other. path ) )
182
+ }
183
+ }
184
+
185
+ impl Ord for Target {
186
+ fn cmp ( & self , other : & Target ) -> Ordering {
187
+ self . path . cmp ( & other. path )
188
+ }
189
+ }
190
+
178
191
impl Eq for Target { }
179
192
180
193
impl Hash for Target {
@@ -204,12 +217,12 @@ impl CargoFmtStrategy {
204
217
}
205
218
206
219
/// Based on the specified `CargoFmtStrategy`, returns a set of main source files.
207
- fn get_targets ( strategy : & CargoFmtStrategy ) -> Result < HashSet < Target > , io:: Error > {
208
- let mut targets = HashSet :: new ( ) ;
220
+ fn get_targets ( strategy : & CargoFmtStrategy ) -> Result < BTreeSet < Target > , io:: Error > {
221
+ let mut targets = BTreeSet :: new ( ) ;
209
222
210
223
match * strategy {
211
224
CargoFmtStrategy :: Root => get_targets_root_only ( & mut targets) ?,
212
- CargoFmtStrategy :: All => get_targets_recursive ( None , & mut targets, & mut HashSet :: new ( ) ) ?,
225
+ CargoFmtStrategy :: All => get_targets_recursive ( None , & mut targets, & mut BTreeSet :: new ( ) ) ?,
213
226
CargoFmtStrategy :: Some ( ref hitlist) => get_targets_with_hitlist ( hitlist, & mut targets) ?,
214
227
}
215
228
@@ -223,7 +236,7 @@ fn get_targets(strategy: &CargoFmtStrategy) -> Result<HashSet<Target>, io::Error
223
236
}
224
237
}
225
238
226
- fn get_targets_root_only ( targets : & mut HashSet < Target > ) -> Result < ( ) , io:: Error > {
239
+ fn get_targets_root_only ( targets : & mut BTreeSet < Target > ) -> Result < ( ) , io:: Error > {
227
240
let metadata = get_cargo_metadata ( None ) ?;
228
241
let current_dir = env:: current_dir ( ) ?. canonicalize ( ) ?;
229
242
let current_dir_manifest = current_dir. join ( "Cargo.toml" ) ;
@@ -243,8 +256,8 @@ fn get_targets_root_only(targets: &mut HashSet<Target>) -> Result<(), io::Error>
243
256
244
257
fn get_targets_recursive (
245
258
manifest_path : Option < & Path > ,
246
- mut targets : & mut HashSet < Target > ,
247
- visited : & mut HashSet < String > ,
259
+ mut targets : & mut BTreeSet < Target > ,
260
+ visited : & mut BTreeSet < String > ,
248
261
) -> Result < ( ) , io:: Error > {
249
262
let metadata = get_cargo_metadata ( manifest_path) ?;
250
263
@@ -275,11 +288,11 @@ fn get_targets_recursive(
275
288
276
289
fn get_targets_with_hitlist (
277
290
hitlist : & [ String ] ,
278
- targets : & mut HashSet < Target > ,
291
+ targets : & mut BTreeSet < Target > ,
279
292
) -> Result < ( ) , io:: Error > {
280
293
let metadata = get_cargo_metadata ( None ) ?;
281
294
282
- let mut workspace_hitlist: HashSet < & String > = HashSet :: from_iter ( hitlist) ;
295
+ let mut workspace_hitlist: BTreeSet < & String > = BTreeSet :: from_iter ( hitlist) ;
283
296
284
297
for package in metadata. packages {
285
298
if workspace_hitlist. remove ( & package. name ) {
@@ -300,34 +313,30 @@ fn get_targets_with_hitlist(
300
313
}
301
314
}
302
315
303
- fn add_targets ( target_paths : & [ cargo_metadata:: Target ] , targets : & mut HashSet < Target > ) {
316
+ fn add_targets ( target_paths : & [ cargo_metadata:: Target ] , targets : & mut BTreeSet < Target > ) {
304
317
for target in target_paths {
305
318
targets. insert ( Target :: from_target ( target) ) ;
306
319
}
307
320
}
308
321
309
322
fn run_rustfmt (
310
- targets : & HashSet < Target > ,
323
+ targets : & BTreeSet < Target > ,
311
324
fmt_args : & [ String ] ,
312
325
verbosity : Verbosity ,
313
326
) -> Result < i32 , io:: Error > {
314
- let default_edition = String :: from ( "2015" ) ;
315
- let mut by_edition = targets
327
+ let by_edition = targets
316
328
. iter ( )
317
329
. inspect ( |t| {
318
330
if verbosity == Verbosity :: Verbose {
319
331
println ! ( "[{} ({})] {:?}" , t. kind, t. edition, t. path)
320
332
}
321
333
} )
322
- . map ( |t| ( & t. edition , & t. path ) )
323
- . fold ( HashMap :: new ( ) , |mut h, t| {
324
- h. entry ( t. 0 ) . or_insert_with ( Vec :: new) . push ( t. 1 ) ;
334
+ . fold ( BTreeMap :: new ( ) , |mut h, t| {
335
+ h. entry ( & t. edition ) . or_insert_with ( Vec :: new) . push ( & t. path ) ;
325
336
h
326
337
} ) ;
327
- if by_edition. is_empty ( ) {
328
- by_edition. insert ( & default_edition, Vec :: new ( ) ) ;
329
- }
330
338
339
+ let mut status = vec ! [ ] ;
331
340
for ( edition, files) in by_edition {
332
341
let stdout = if verbosity == Verbosity :: Quiet {
333
342
std:: process:: Stdio :: null ( )
@@ -357,13 +366,14 @@ fn run_rustfmt(
357
366
_ => e,
358
367
} ) ?;
359
368
360
- let status = command. wait ( ) ?;
361
- if !status. success ( ) {
362
- return Ok ( status. code ( ) . unwrap_or ( FAILURE ) ) ;
363
- }
369
+ status. push ( command. wait ( ) ?) ;
364
370
}
365
371
366
- Ok ( SUCCESS )
372
+ Ok ( status
373
+ . iter ( )
374
+ . filter_map ( |s| if s. success ( ) { None } else { s. code ( ) } )
375
+ . next ( )
376
+ . unwrap_or ( SUCCESS ) )
367
377
}
368
378
369
379
fn get_cargo_metadata ( manifest_path : Option < & Path > ) -> Result < cargo_metadata:: Metadata , io:: Error > {
0 commit comments