From cb28b36b9bb25e3f49762466897b79493f3fa150 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 10 Mar 2025 15:38:25 -0500 Subject: [PATCH] refactor(toml): Centralize target descriptions 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. --- src/cargo/util/toml/targets.rs | 87 +++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 34 deletions(-) diff --git a/src/cargo/util/toml/targets.rs b/src/cargo/util/toml/targets.rs index 9c71b4d766d..23f72d75610 100644 --- a/src/cargo/util/toml/targets.rs +++ b/src/cargo/util/toml/targets.rs @@ -32,6 +32,18 @@ const DEFAULT_TEST_DIR_NAME: &'static str = "tests"; const DEFAULT_BENCH_DIR_NAME: &'static str = "benches"; const DEFAULT_EXAMPLE_DIR_NAME: &'static str = "examples"; +const TARGET_KIND_HUMAN_LIB: &str = "library"; +const TARGET_KIND_HUMAN_BIN: &str = "binary"; +const TARGET_KIND_HUMAN_EXAMPLE: &str = "example"; +const TARGET_KIND_HUMAN_TEST: &str = "test"; +const TARGET_KIND_HUMAN_BENCH: &str = "benchmark"; + +const TARGET_KIND_LIB: &str = "lib"; +const TARGET_KIND_BIN: &str = "bin"; +const TARGET_KIND_EXAMPLE: &str = "example"; +const TARGET_KIND_TEST: &str = "test"; +const TARGET_KIND_BENCH: &str = "bench"; + #[tracing::instrument(skip_all)] pub(super) fn to_targets( features: &Features, @@ -141,8 +153,8 @@ pub fn normalize_lib( // Check early to improve error messages validate_lib_name(&lib, warnings)?; - validate_proc_macro(&lib, "library", edition, warnings)?; - validate_crate_types(&lib, "library", edition, warnings)?; + validate_proc_macro(&lib, TARGET_KIND_HUMAN_LIB, edition, warnings)?; + validate_crate_types(&lib, TARGET_KIND_HUMAN_LIB, edition, warnings)?; if let Some(PathValue(path)) = &lib.path { lib.path = Some(PathValue(paths::normalize_path(path).into())); @@ -164,8 +176,8 @@ pub fn normalize_lib( // Check early to improve error messages validate_lib_name(&lib, warnings)?; - validate_proc_macro(&lib, "library", edition, warnings)?; - validate_crate_types(&lib, "library", edition, warnings)?; + validate_proc_macro(&lib, TARGET_KIND_HUMAN_LIB, edition, warnings)?; + validate_crate_types(&lib, TARGET_KIND_HUMAN_LIB, edition, warnings)?; if lib.path.is_none() { if let Some(inferred) = inferred { @@ -285,8 +297,8 @@ pub fn normalize_bins( autodiscover, edition, warnings, - "binary", - "bin", + TARGET_KIND_HUMAN_BIN, + TARGET_KIND_BIN, "autobins", ); @@ -297,21 +309,28 @@ pub fn normalize_bins( validate_bin_crate_types(bin, edition, warnings, errors)?; validate_bin_proc_macro(bin, edition, warnings, errors)?; - let path = target_path(bin, &inferred, "bin", package_root, edition, &mut |_| { - if let Some(legacy_path) = - legacy_bin_path(package_root, name_or_panic(bin), has_lib) - { - warnings.push(format!( - "path `{}` was erroneously implicitly accepted for binary `{}`,\n\ + let path = target_path( + bin, + &inferred, + TARGET_KIND_BIN, + package_root, + edition, + &mut |_| { + if let Some(legacy_path) = + legacy_bin_path(package_root, name_or_panic(bin), has_lib) + { + warnings.push(format!( + "path `{}` was erroneously implicitly accepted for binary `{}`,\n\ please set bin.path in Cargo.toml", - legacy_path.display(), - name_or_panic(bin) - )); - Some(legacy_path) - } else { - None - } - }); + legacy_path.display(), + name_or_panic(bin) + )); + Some(legacy_path) + } else { + None + } + }, + ); let path = match path { Ok(path) => paths::normalize_path(&path).into(), Err(e) => anyhow::bail!("{}", e), @@ -339,7 +358,7 @@ fn to_bin_targets( } } - validate_unique_names(&bins, "binary")?; + validate_unique_names(&bins, TARGET_KIND_HUMAN_BIN)?; let mut result = Vec::new(); for bin in bins { @@ -391,8 +410,8 @@ pub fn normalize_examples( let mut inferred = || infer_from_directory(&package_root, Path::new(DEFAULT_EXAMPLE_DIR_NAME)); let targets = normalize_targets( - "example", - "example", + TARGET_KIND_HUMAN_EXAMPLE, + TARGET_KIND_EXAMPLE, toml_examples, &mut inferred, package_root, @@ -412,7 +431,7 @@ fn to_example_targets( package_root: &Path, edition: Edition, ) -> CargoResult> { - validate_unique_names(&targets, "example")?; + validate_unique_names(&targets, TARGET_KIND_EXAMPLE)?; let mut result = Vec::new(); for toml in targets { @@ -448,8 +467,8 @@ pub fn normalize_tests( let mut inferred = || infer_from_directory(&package_root, Path::new(DEFAULT_TEST_DIR_NAME)); let targets = normalize_targets( - "test", - "test", + TARGET_KIND_HUMAN_TEST, + TARGET_KIND_TEST, toml_tests, &mut inferred, package_root, @@ -469,7 +488,7 @@ fn to_test_targets( package_root: &Path, edition: Edition, ) -> CargoResult> { - validate_unique_names(&targets, "test")?; + validate_unique_names(&targets, TARGET_KIND_TEST)?; let mut result = Vec::new(); for toml in targets { @@ -513,8 +532,8 @@ pub fn normalize_benches( let mut inferred = || infer_from_directory(&package_root, Path::new(DEFAULT_BENCH_DIR_NAME)); let targets = normalize_targets_with_legacy_path( - "benchmark", - "bench", + TARGET_KIND_HUMAN_BENCH, + TARGET_KIND_BENCH, toml_benches, &mut inferred, package_root, @@ -536,7 +555,7 @@ fn to_bench_targets( package_root: &Path, edition: Edition, ) -> CargoResult> { - validate_unique_names(&targets, "bench")?; + validate_unique_names(&targets, TARGET_KIND_BENCH)?; let mut result = Vec::new(); for toml in targets { @@ -1074,7 +1093,7 @@ fn name_or_panic(target: &TomlTarget) -> &str { } fn validate_lib_name(target: &TomlTarget, warnings: &mut Vec) -> CargoResult<()> { - validate_target_name(target, "library", "lib", warnings)?; + validate_target_name(target, TARGET_KIND_HUMAN_LIB, TARGET_KIND_LIB, warnings)?; let name = name_or_panic(target); if name.contains('-') { anyhow::bail!("library target names cannot contain hyphens: {}", name) @@ -1084,7 +1103,7 @@ fn validate_lib_name(target: &TomlTarget, warnings: &mut Vec) -> CargoRe } fn validate_bin_name(bin: &TomlTarget, warnings: &mut Vec) -> CargoResult<()> { - validate_target_name(bin, "binary", "bin", warnings)?; + validate_target_name(bin, TARGET_KIND_HUMAN_BIN, TARGET_KIND_BIN, warnings)?; let name = name_or_panic(bin).to_owned(); if restricted_names::is_conflicting_artifact_name(&name) { anyhow::bail!( @@ -1139,7 +1158,7 @@ fn validate_bin_proc_macro( name )); } else { - validate_proc_macro(target, "binary", edition, warnings)?; + validate_proc_macro(target, TARGET_KIND_HUMAN_BIN, edition, warnings)?; } Ok(()) } @@ -1177,7 +1196,7 @@ fn validate_bin_crate_types( crate_types.join(", ") )); } else { - validate_crate_types(target, "binary", edition, warnings)?; + validate_crate_types(target, TARGET_KIND_HUMAN_BIN, edition, warnings)?; } } Ok(())