@@ -41,6 +41,10 @@ pub struct Opts {
41
41
#[ structopt( short = "p" , long = "package" , value_name = "package" ) ]
42
42
packages : Vec < String > ,
43
43
44
+ /// Specify path to Cargo.toml
45
+ #[ structopt( long = "manifest-path" , value_name = "manifest-path" ) ]
46
+ manifest_path : Option < String > ,
47
+
44
48
/// Options passed to rustfmt
45
49
// 'raw = true' to make `--` explicit.
46
50
#[ structopt( name = "rustfmt_options" , raw( raw = "true" ) ) ]
@@ -97,7 +101,26 @@ fn execute() -> i32 {
97
101
98
102
let strategy = CargoFmtStrategy :: from_opts ( & opts) ;
99
103
100
- handle_command_status ( format_crate ( verbosity, & strategy, opts. rustfmt_options ) )
104
+ if let Some ( specified_manifest_path) = opts. manifest_path {
105
+ if !specified_manifest_path. ends_with ( "Cargo.toml" ) {
106
+ print_usage_to_stderr ( "the manifest-path must be a path to a Cargo.toml file" ) ;
107
+ return FAILURE ;
108
+ }
109
+ let manifest_path = PathBuf :: from ( specified_manifest_path) ;
110
+ handle_command_status ( format_crate (
111
+ verbosity,
112
+ & strategy,
113
+ opts. rustfmt_options ,
114
+ Some ( & manifest_path) ,
115
+ ) )
116
+ } else {
117
+ handle_command_status ( format_crate (
118
+ verbosity,
119
+ & strategy,
120
+ opts. rustfmt_options ,
121
+ None ,
122
+ ) )
123
+ }
101
124
}
102
125
103
126
fn print_usage_to_stderr ( reason : & str ) {
@@ -149,8 +172,9 @@ fn format_crate(
149
172
verbosity : Verbosity ,
150
173
strategy : & CargoFmtStrategy ,
151
174
rustfmt_args : Vec < String > ,
175
+ manifest_path : Option < & Path > ,
152
176
) -> Result < i32 , io:: Error > {
153
- let targets = get_targets ( strategy) ?;
177
+ let targets = get_targets ( strategy, manifest_path ) ?;
154
178
155
179
// Currently only bin and lib files get formatted.
156
180
run_rustfmt ( & targets, & rustfmt_args, verbosity)
@@ -227,13 +251,20 @@ impl CargoFmtStrategy {
227
251
}
228
252
229
253
/// Based on the specified `CargoFmtStrategy`, returns a set of main source files.
230
- fn get_targets ( strategy : & CargoFmtStrategy ) -> Result < BTreeSet < Target > , io:: Error > {
254
+ fn get_targets (
255
+ strategy : & CargoFmtStrategy ,
256
+ manifest_path : Option < & Path > ,
257
+ ) -> Result < BTreeSet < Target > , io:: Error > {
231
258
let mut targets = BTreeSet :: new ( ) ;
232
259
233
260
match * strategy {
234
- CargoFmtStrategy :: Root => get_targets_root_only ( & mut targets) ?,
235
- CargoFmtStrategy :: All => get_targets_recursive ( None , & mut targets, & mut BTreeSet :: new ( ) ) ?,
236
- CargoFmtStrategy :: Some ( ref hitlist) => get_targets_with_hitlist ( hitlist, & mut targets) ?,
261
+ CargoFmtStrategy :: Root => get_targets_root_only ( manifest_path, & mut targets) ?,
262
+ CargoFmtStrategy :: All => {
263
+ get_targets_recursive ( manifest_path, & mut targets, & mut BTreeSet :: new ( ) ) ?
264
+ }
265
+ CargoFmtStrategy :: Some ( ref hitlist) => {
266
+ get_targets_with_hitlist ( manifest_path, hitlist, & mut targets) ?
267
+ }
237
268
}
238
269
239
270
if targets. is_empty ( ) {
@@ -246,12 +277,24 @@ fn get_targets(strategy: &CargoFmtStrategy) -> Result<BTreeSet<Target>, io::Erro
246
277
}
247
278
}
248
279
249
- fn get_targets_root_only ( targets : & mut BTreeSet < Target > ) -> Result < ( ) , io:: Error > {
250
- let metadata = get_cargo_metadata ( None , false ) ?;
251
- let current_dir = env:: current_dir ( ) ?. canonicalize ( ) ?;
252
- let current_dir_manifest = current_dir. join ( "Cargo.toml" ) ;
280
+ fn get_targets_root_only (
281
+ manifest_path : Option < & Path > ,
282
+ targets : & mut BTreeSet < Target > ,
283
+ ) -> Result < ( ) , io:: Error > {
284
+ let metadata = get_cargo_metadata ( manifest_path, false ) ?;
253
285
let workspace_root_path = PathBuf :: from ( & metadata. workspace_root ) . canonicalize ( ) ?;
254
- let in_workspace_root = workspace_root_path == current_dir;
286
+ let ( in_workspace_root, current_dir_manifest) = if let Some ( target_manifest) = manifest_path {
287
+ (
288
+ workspace_root_path == target_manifest,
289
+ target_manifest. canonicalize ( ) ?,
290
+ )
291
+ } else {
292
+ let current_dir = env:: current_dir ( ) ?. canonicalize ( ) ?;
293
+ (
294
+ workspace_root_path == current_dir,
295
+ current_dir. join ( "Cargo.toml" ) ,
296
+ )
297
+ } ;
255
298
256
299
let package_targets = match metadata. packages . len ( ) {
257
300
1 => metadata. packages . into_iter ( ) . next ( ) . unwrap ( ) . targets ,
@@ -319,10 +362,11 @@ fn get_targets_recursive(
319
362
}
320
363
321
364
fn get_targets_with_hitlist (
365
+ manifest_path : Option < & Path > ,
322
366
hitlist : & [ String ] ,
323
367
targets : & mut BTreeSet < Target > ,
324
368
) -> Result < ( ) , io:: Error > {
325
- let metadata = get_cargo_metadata ( None , false ) ?;
369
+ let metadata = get_cargo_metadata ( manifest_path , false ) ?;
326
370
327
371
let mut workspace_hitlist: BTreeSet < & String > = BTreeSet :: from_iter ( hitlist) ;
328
372
0 commit comments