@@ -84,9 +84,9 @@ fn execute() -> i32 {
84
84
return success;
85
85
}
86
86
87
- let workspace_hitlist = WorkspaceHitlist :: from_matches ( & matches) ;
87
+ let strategy = CargoFmtStrategy :: from_matches ( & matches) ;
88
88
89
- match format_crate ( verbosity, & workspace_hitlist ) {
89
+ match format_crate ( verbosity, & strategy ) {
90
90
Err ( e) => {
91
91
print_usage_to_stderr ( & opts, & e. to_string ( ) ) ;
92
92
failure
@@ -127,9 +127,9 @@ pub enum Verbosity {
127
127
128
128
fn format_crate (
129
129
verbosity : Verbosity ,
130
- workspace_hitlist : & WorkspaceHitlist ,
130
+ strategy : & CargoFmtStrategy ,
131
131
) -> Result < ExitStatus , io:: Error > {
132
- let targets = get_targets ( workspace_hitlist ) ?;
132
+ let targets = get_targets ( strategy ) ?;
133
133
134
134
// Currently only bin and lib files get formatted
135
135
let files: Vec < _ > = targets
@@ -227,37 +227,33 @@ impl Hash for Target {
227
227
}
228
228
229
229
#[ derive( Debug , PartialEq , Eq ) ]
230
- pub enum WorkspaceHitlist {
230
+ pub enum CargoFmtStrategy {
231
+ /// Format every packages and dependencies.
231
232
All ,
233
+ /// Format pacakges that are specified by the command line argument.
232
234
Some ( Vec < String > ) ,
233
- None ,
235
+ /// Format the root packages only.
236
+ Root ,
234
237
}
235
238
236
- impl WorkspaceHitlist {
237
- pub fn get_some ( & self ) -> Option < & [ String ] > {
238
- if let WorkspaceHitlist :: Some ( ref hitlist) = * self {
239
- Some ( hitlist)
240
- } else {
241
- None
242
- }
243
- }
244
-
245
- pub fn from_matches ( matches : & Matches ) -> WorkspaceHitlist {
239
+ impl CargoFmtStrategy {
240
+ pub fn from_matches ( matches : & Matches ) -> CargoFmtStrategy {
246
241
match ( matches. opt_present ( "all" ) , matches. opt_present ( "p" ) ) {
247
- ( false , false ) => WorkspaceHitlist :: None ,
248
- ( true , _) => WorkspaceHitlist :: All ,
249
- ( false , true ) => WorkspaceHitlist :: Some ( matches. opt_strs ( "p" ) ) ,
242
+ ( false , false ) => CargoFmtStrategy :: Root ,
243
+ ( true , _) => CargoFmtStrategy :: All ,
244
+ ( false , true ) => CargoFmtStrategy :: Some ( matches. opt_strs ( "p" ) ) ,
250
245
}
251
246
}
252
247
}
253
248
254
- fn get_targets ( workspace_hitlist : & WorkspaceHitlist ) -> Result < HashSet < Target > , io:: Error > {
249
+ /// Based on the specified CargoFmtStrategy, returns a set of main source files.
250
+ fn get_targets ( strategy : & CargoFmtStrategy ) -> Result < HashSet < Target > , io:: Error > {
255
251
let mut targets = HashSet :: new ( ) ;
256
252
257
- match * workspace_hitlist {
258
- WorkspaceHitlist :: None => get_targets_root_only ( & mut targets) ?,
259
- WorkspaceHitlist :: All => get_targets_recursive ( None , & mut targets, & mut HashSet :: new ( ) ) ?,
260
- WorkspaceHitlist :: Some ( ref hitlist) => get_targets_with_hitlist ( hitlist, & mut targets) ?,
253
+ match * strategy {
254
+ CargoFmtStrategy :: Root => get_targets_root_only ( & mut targets) ?,
255
+ CargoFmtStrategy :: All => get_targets_recursive ( None , & mut targets, & mut HashSet :: new ( ) ) ?,
256
+ CargoFmtStrategy :: Some ( ref hitlist) => get_targets_with_hitlist ( hitlist, & mut targets) ?,
261
257
}
262
258
263
259
if targets. is_empty ( ) {
@@ -317,25 +313,25 @@ fn get_targets_recursive(
317
313
}
318
314
319
315
fn get_targets_with_hitlist (
320
- target_names : & [ String ] ,
316
+ hitlist : & [ String ] ,
321
317
targets : & mut HashSet < Target > ,
322
318
) -> Result < ( ) , io:: Error > {
323
319
let metadata = get_cargo_metadata ( None ) ?;
324
320
325
- let mut hitlist : HashSet < & String > = HashSet :: from_iter ( target_names ) ;
321
+ let mut workspace_hitlist : HashSet < & String > = HashSet :: from_iter ( hitlist ) ;
326
322
327
323
for package in metadata. packages {
328
324
for target in package. targets {
329
- if hitlist . remove ( & target. name ) {
325
+ if workspace_hitlist . remove ( & target. name ) {
330
326
targets. insert ( Target :: from_target ( & target) ) ;
331
327
}
332
328
}
333
329
}
334
330
335
- if hitlist . is_empty ( ) {
331
+ if workspace_hitlist . is_empty ( ) {
336
332
Ok ( ( ) )
337
333
} else {
338
- let package = hitlist . iter ( ) . next ( ) . unwrap ( ) ;
334
+ let package = workspace_hitlist . iter ( ) . next ( ) . unwrap ( ) ;
339
335
Err ( io:: Error :: new (
340
336
io:: ErrorKind :: InvalidInput ,
341
337
format ! ( "package `{}` is not a member of the workspace" , package) ,
0 commit comments