@@ -32,6 +32,18 @@ const DEFAULT_TEST_DIR_NAME: &'static str = "tests";
32
32
const DEFAULT_BENCH_DIR_NAME : & ' static str = "benches" ;
33
33
const DEFAULT_EXAMPLE_DIR_NAME : & ' static str = "examples" ;
34
34
35
+ const TARGET_KIND_HUMAN_LIB : & str = "library" ;
36
+ const TARGET_KIND_HUMAN_BIN : & str = "binary" ;
37
+ const TARGET_KIND_HUMAN_EXAMPLE : & str = "example" ;
38
+ const TARGET_KIND_HUMAN_TEST : & str = "test" ;
39
+ const TARGET_KIND_HUMAN_BENCH : & str = "benchmark" ;
40
+
41
+ const TARGET_KIND_LIB : & str = "lib" ;
42
+ const TARGET_KIND_BIN : & str = "bin" ;
43
+ const TARGET_KIND_EXAMPLE : & str = "example" ;
44
+ const TARGET_KIND_TEST : & str = "test" ;
45
+ const TARGET_KIND_BENCH : & str = "bench" ;
46
+
35
47
#[ tracing:: instrument( skip_all) ]
36
48
pub ( super ) fn to_targets (
37
49
features : & Features ,
@@ -141,8 +153,8 @@ pub fn normalize_lib(
141
153
// Check early to improve error messages
142
154
validate_lib_name ( & lib, warnings) ?;
143
155
144
- validate_proc_macro ( & lib, "library" , edition, warnings) ?;
145
- validate_crate_types ( & lib, "library" , edition, warnings) ?;
156
+ validate_proc_macro ( & lib, TARGET_KIND_HUMAN_LIB , edition, warnings) ?;
157
+ validate_crate_types ( & lib, TARGET_KIND_HUMAN_LIB , edition, warnings) ?;
146
158
147
159
if let Some ( PathValue ( path) ) = & lib. path {
148
160
lib. path = Some ( PathValue ( paths:: normalize_path ( path) . into ( ) ) ) ;
@@ -164,8 +176,8 @@ pub fn normalize_lib(
164
176
// Check early to improve error messages
165
177
validate_lib_name ( & lib, warnings) ?;
166
178
167
- validate_proc_macro ( & lib, "library" , edition, warnings) ?;
168
- validate_crate_types ( & lib, "library" , edition, warnings) ?;
179
+ validate_proc_macro ( & lib, TARGET_KIND_HUMAN_LIB , edition, warnings) ?;
180
+ validate_crate_types ( & lib, TARGET_KIND_HUMAN_LIB , edition, warnings) ?;
169
181
170
182
if lib. path . is_none ( ) {
171
183
if let Some ( inferred) = inferred {
@@ -285,8 +297,8 @@ pub fn normalize_bins(
285
297
autodiscover,
286
298
edition,
287
299
warnings,
288
- "binary" ,
289
- "bin" ,
300
+ TARGET_KIND_HUMAN_BIN ,
301
+ TARGET_KIND_BIN ,
290
302
"autobins" ,
291
303
) ;
292
304
@@ -297,21 +309,28 @@ pub fn normalize_bins(
297
309
validate_bin_crate_types ( bin, edition, warnings, errors) ?;
298
310
validate_bin_proc_macro ( bin, edition, warnings, errors) ?;
299
311
300
- let path = target_path ( bin, & inferred, "bin" , package_root, edition, & mut |_| {
301
- if let Some ( legacy_path) =
302
- legacy_bin_path ( package_root, name_or_panic ( bin) , has_lib)
303
- {
304
- warnings. push ( format ! (
305
- "path `{}` was erroneously implicitly accepted for binary `{}`,\n \
312
+ let path = target_path (
313
+ bin,
314
+ & inferred,
315
+ TARGET_KIND_BIN ,
316
+ package_root,
317
+ edition,
318
+ & mut |_| {
319
+ if let Some ( legacy_path) =
320
+ legacy_bin_path ( package_root, name_or_panic ( bin) , has_lib)
321
+ {
322
+ warnings. push ( format ! (
323
+ "path `{}` was erroneously implicitly accepted for binary `{}`,\n \
306
324
please set bin.path in Cargo.toml",
307
- legacy_path. display( ) ,
308
- name_or_panic( bin)
309
- ) ) ;
310
- Some ( legacy_path)
311
- } else {
312
- None
313
- }
314
- } ) ;
325
+ legacy_path. display( ) ,
326
+ name_or_panic( bin)
327
+ ) ) ;
328
+ Some ( legacy_path)
329
+ } else {
330
+ None
331
+ }
332
+ } ,
333
+ ) ;
315
334
let path = match path {
316
335
Ok ( path) => paths:: normalize_path ( & path) . into ( ) ,
317
336
Err ( e) => anyhow:: bail!( "{}" , e) ,
@@ -339,7 +358,7 @@ fn to_bin_targets(
339
358
}
340
359
}
341
360
342
- validate_unique_names ( & bins, "binary" ) ?;
361
+ validate_unique_names ( & bins, TARGET_KIND_HUMAN_BIN ) ?;
343
362
344
363
let mut result = Vec :: new ( ) ;
345
364
for bin in bins {
@@ -391,8 +410,8 @@ pub fn normalize_examples(
391
410
let mut inferred = || infer_from_directory ( & package_root, Path :: new ( DEFAULT_EXAMPLE_DIR_NAME ) ) ;
392
411
393
412
let targets = normalize_targets (
394
- "example" ,
395
- "example" ,
413
+ TARGET_KIND_HUMAN_EXAMPLE ,
414
+ TARGET_KIND_EXAMPLE ,
396
415
toml_examples,
397
416
& mut inferred,
398
417
package_root,
@@ -412,7 +431,7 @@ fn to_example_targets(
412
431
package_root : & Path ,
413
432
edition : Edition ,
414
433
) -> CargoResult < Vec < Target > > {
415
- validate_unique_names ( & targets, "example" ) ?;
434
+ validate_unique_names ( & targets, TARGET_KIND_EXAMPLE ) ?;
416
435
417
436
let mut result = Vec :: new ( ) ;
418
437
for toml in targets {
@@ -448,8 +467,8 @@ pub fn normalize_tests(
448
467
let mut inferred = || infer_from_directory ( & package_root, Path :: new ( DEFAULT_TEST_DIR_NAME ) ) ;
449
468
450
469
let targets = normalize_targets (
451
- "test" ,
452
- "test" ,
470
+ TARGET_KIND_HUMAN_TEST ,
471
+ TARGET_KIND_TEST ,
453
472
toml_tests,
454
473
& mut inferred,
455
474
package_root,
@@ -469,7 +488,7 @@ fn to_test_targets(
469
488
package_root : & Path ,
470
489
edition : Edition ,
471
490
) -> CargoResult < Vec < Target > > {
472
- validate_unique_names ( & targets, "test" ) ?;
491
+ validate_unique_names ( & targets, TARGET_KIND_TEST ) ?;
473
492
474
493
let mut result = Vec :: new ( ) ;
475
494
for toml in targets {
@@ -513,8 +532,8 @@ pub fn normalize_benches(
513
532
let mut inferred = || infer_from_directory ( & package_root, Path :: new ( DEFAULT_BENCH_DIR_NAME ) ) ;
514
533
515
534
let targets = normalize_targets_with_legacy_path (
516
- "benchmark" ,
517
- "bench" ,
535
+ TARGET_KIND_HUMAN_BENCH ,
536
+ TARGET_KIND_BENCH ,
518
537
toml_benches,
519
538
& mut inferred,
520
539
package_root,
@@ -536,7 +555,7 @@ fn to_bench_targets(
536
555
package_root : & Path ,
537
556
edition : Edition ,
538
557
) -> CargoResult < Vec < Target > > {
539
- validate_unique_names ( & targets, "bench" ) ?;
558
+ validate_unique_names ( & targets, TARGET_KIND_BENCH ) ?;
540
559
541
560
let mut result = Vec :: new ( ) ;
542
561
for toml in targets {
@@ -1074,7 +1093,7 @@ fn name_or_panic(target: &TomlTarget) -> &str {
1074
1093
}
1075
1094
1076
1095
fn validate_lib_name ( target : & TomlTarget , warnings : & mut Vec < String > ) -> CargoResult < ( ) > {
1077
- validate_target_name ( target, "library" , "lib" , warnings) ?;
1096
+ validate_target_name ( target, TARGET_KIND_HUMAN_LIB , TARGET_KIND_LIB , warnings) ?;
1078
1097
let name = name_or_panic ( target) ;
1079
1098
if name. contains ( '-' ) {
1080
1099
anyhow:: bail!( "library target names cannot contain hyphens: {}" , name)
@@ -1084,7 +1103,7 @@ fn validate_lib_name(target: &TomlTarget, warnings: &mut Vec<String>) -> CargoRe
1084
1103
}
1085
1104
1086
1105
fn validate_bin_name ( bin : & TomlTarget , warnings : & mut Vec < String > ) -> CargoResult < ( ) > {
1087
- validate_target_name ( bin, "binary" , "bin" , warnings) ?;
1106
+ validate_target_name ( bin, TARGET_KIND_HUMAN_BIN , TARGET_KIND_BIN , warnings) ?;
1088
1107
let name = name_or_panic ( bin) . to_owned ( ) ;
1089
1108
if restricted_names:: is_conflicting_artifact_name ( & name) {
1090
1109
anyhow:: bail!(
@@ -1139,7 +1158,7 @@ fn validate_bin_proc_macro(
1139
1158
name
1140
1159
) ) ;
1141
1160
} else {
1142
- validate_proc_macro ( target, "binary" , edition, warnings) ?;
1161
+ validate_proc_macro ( target, TARGET_KIND_HUMAN_BIN , edition, warnings) ?;
1143
1162
}
1144
1163
Ok ( ( ) )
1145
1164
}
@@ -1177,7 +1196,7 @@ fn validate_bin_crate_types(
1177
1196
crate_types. join( ", " )
1178
1197
) ) ;
1179
1198
} else {
1180
- validate_crate_types ( target, "binary" , edition, warnings) ?;
1199
+ validate_crate_types ( target, TARGET_KIND_HUMAN_BIN , edition, warnings) ?;
1181
1200
}
1182
1201
}
1183
1202
Ok ( ( ) )
0 commit comments