Skip to content

Commit 21f4080

Browse files
authored
refactor(toml): Centralize target descriptions (#15291)
I was going to add another duplicate and was wondering what was the right value and figured it'd be better if I just did this. <!-- Thanks for submitting a pull request 🎉! Here are some tips for you: * If this is your first contribution, read "Cargo Contribution Guide" first: https://doc.crates.io/contrib/ * Run `cargo fmt --all` to format your code changes. * Small commits and pull requests are always preferable and easy to review. * If your idea is large and needs feedback from the community, read how: https://doc.crates.io/contrib/process/#working-on-large-features * Cargo takes care of compatibility. Read our design principles: https://doc.crates.io/contrib/design.html * When changing help text of cargo commands, follow the steps to generate docs: https://github.com/rust-lang/cargo/tree/master/src/doc#building-the-man-pages * If your PR is not finished, set it as "draft" PR or add "WIP" in its title. * It's ok to use the CI resources to test your PR, but please don't abuse them. ### What does this PR try to resolve? Explain the motivation behind this change. A clear overview along with an in-depth explanation are helpful. You can use `Fixes #<issue number>` to associate this PR to an existing issue. ### How should we test and review this PR? Demonstrate how you test this change and guide reviewers through your PR. With a smooth review process, a pull request usually gets reviewed quicker. If you don't know how to write and run your tests, please read the guide: https://doc.crates.io/contrib/tests ### Additional information Other information you want to mention in this PR, such as prior arts, future extensions, an unresolved problem, or a TODO list. -->
2 parents 3897c7c + cb28b36 commit 21f4080

File tree

1 file changed

+53
-34
lines changed

1 file changed

+53
-34
lines changed

src/cargo/util/toml/targets.rs

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ const DEFAULT_TEST_DIR_NAME: &'static str = "tests";
3232
const DEFAULT_BENCH_DIR_NAME: &'static str = "benches";
3333
const DEFAULT_EXAMPLE_DIR_NAME: &'static str = "examples";
3434

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+
3547
#[tracing::instrument(skip_all)]
3648
pub(super) fn to_targets(
3749
features: &Features,
@@ -141,8 +153,8 @@ pub fn normalize_lib(
141153
// Check early to improve error messages
142154
validate_lib_name(&lib, warnings)?;
143155

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)?;
146158

147159
if let Some(PathValue(path)) = &lib.path {
148160
lib.path = Some(PathValue(paths::normalize_path(path).into()));
@@ -164,8 +176,8 @@ pub fn normalize_lib(
164176
// Check early to improve error messages
165177
validate_lib_name(&lib, warnings)?;
166178

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)?;
169181

170182
if lib.path.is_none() {
171183
if let Some(inferred) = inferred {
@@ -285,8 +297,8 @@ pub fn normalize_bins(
285297
autodiscover,
286298
edition,
287299
warnings,
288-
"binary",
289-
"bin",
300+
TARGET_KIND_HUMAN_BIN,
301+
TARGET_KIND_BIN,
290302
"autobins",
291303
);
292304

@@ -297,21 +309,28 @@ pub fn normalize_bins(
297309
validate_bin_crate_types(bin, edition, warnings, errors)?;
298310
validate_bin_proc_macro(bin, edition, warnings, errors)?;
299311

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\
306324
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+
);
315334
let path = match path {
316335
Ok(path) => paths::normalize_path(&path).into(),
317336
Err(e) => anyhow::bail!("{}", e),
@@ -339,7 +358,7 @@ fn to_bin_targets(
339358
}
340359
}
341360

342-
validate_unique_names(&bins, "binary")?;
361+
validate_unique_names(&bins, TARGET_KIND_HUMAN_BIN)?;
343362

344363
let mut result = Vec::new();
345364
for bin in bins {
@@ -391,8 +410,8 @@ pub fn normalize_examples(
391410
let mut inferred = || infer_from_directory(&package_root, Path::new(DEFAULT_EXAMPLE_DIR_NAME));
392411

393412
let targets = normalize_targets(
394-
"example",
395-
"example",
413+
TARGET_KIND_HUMAN_EXAMPLE,
414+
TARGET_KIND_EXAMPLE,
396415
toml_examples,
397416
&mut inferred,
398417
package_root,
@@ -412,7 +431,7 @@ fn to_example_targets(
412431
package_root: &Path,
413432
edition: Edition,
414433
) -> CargoResult<Vec<Target>> {
415-
validate_unique_names(&targets, "example")?;
434+
validate_unique_names(&targets, TARGET_KIND_EXAMPLE)?;
416435

417436
let mut result = Vec::new();
418437
for toml in targets {
@@ -448,8 +467,8 @@ pub fn normalize_tests(
448467
let mut inferred = || infer_from_directory(&package_root, Path::new(DEFAULT_TEST_DIR_NAME));
449468

450469
let targets = normalize_targets(
451-
"test",
452-
"test",
470+
TARGET_KIND_HUMAN_TEST,
471+
TARGET_KIND_TEST,
453472
toml_tests,
454473
&mut inferred,
455474
package_root,
@@ -469,7 +488,7 @@ fn to_test_targets(
469488
package_root: &Path,
470489
edition: Edition,
471490
) -> CargoResult<Vec<Target>> {
472-
validate_unique_names(&targets, "test")?;
491+
validate_unique_names(&targets, TARGET_KIND_TEST)?;
473492

474493
let mut result = Vec::new();
475494
for toml in targets {
@@ -513,8 +532,8 @@ pub fn normalize_benches(
513532
let mut inferred = || infer_from_directory(&package_root, Path::new(DEFAULT_BENCH_DIR_NAME));
514533

515534
let targets = normalize_targets_with_legacy_path(
516-
"benchmark",
517-
"bench",
535+
TARGET_KIND_HUMAN_BENCH,
536+
TARGET_KIND_BENCH,
518537
toml_benches,
519538
&mut inferred,
520539
package_root,
@@ -536,7 +555,7 @@ fn to_bench_targets(
536555
package_root: &Path,
537556
edition: Edition,
538557
) -> CargoResult<Vec<Target>> {
539-
validate_unique_names(&targets, "bench")?;
558+
validate_unique_names(&targets, TARGET_KIND_BENCH)?;
540559

541560
let mut result = Vec::new();
542561
for toml in targets {
@@ -1074,7 +1093,7 @@ fn name_or_panic(target: &TomlTarget) -> &str {
10741093
}
10751094

10761095
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)?;
10781097
let name = name_or_panic(target);
10791098
if name.contains('-') {
10801099
anyhow::bail!("library target names cannot contain hyphens: {}", name)
@@ -1084,7 +1103,7 @@ fn validate_lib_name(target: &TomlTarget, warnings: &mut Vec<String>) -> CargoRe
10841103
}
10851104

10861105
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)?;
10881107
let name = name_or_panic(bin).to_owned();
10891108
if restricted_names::is_conflicting_artifact_name(&name) {
10901109
anyhow::bail!(
@@ -1139,7 +1158,7 @@ fn validate_bin_proc_macro(
11391158
name
11401159
));
11411160
} else {
1142-
validate_proc_macro(target, "binary", edition, warnings)?;
1161+
validate_proc_macro(target, TARGET_KIND_HUMAN_BIN, edition, warnings)?;
11431162
}
11441163
Ok(())
11451164
}
@@ -1177,7 +1196,7 @@ fn validate_bin_crate_types(
11771196
crate_types.join(", ")
11781197
));
11791198
} else {
1180-
validate_crate_types(target, "binary", edition, warnings)?;
1199+
validate_crate_types(target, TARGET_KIND_HUMAN_BIN, edition, warnings)?;
11811200
}
11821201
}
11831202
Ok(())

0 commit comments

Comments
 (0)