refactor(toml): Abstract out bin name validation

This commit is contained in:
Ed Page 2024-04-04 13:52:58 -05:00
parent f2b0678436
commit b061bc5e83

View File

@ -301,18 +301,17 @@ fn to_bin_targets(
// This loop performs basic checks on each of the TomlTarget in `bins`.
for bin in &bins {
validate_bin_name(bin, warnings)?;
// For each binary, check if the `filename` parameter is populated. If it is,
// check if the corresponding cargo feature has been activated.
if bin.filename.is_some() {
features.require(Feature::different_binary_name())?;
}
validate_target_name(bin, "binary", "bin", warnings)?;
let name = name_or_panic(bin).to_owned();
if let Some(crate_types) = bin.crate_types() {
if !crate_types.is_empty() {
let name = name_or_panic(bin);
errors.push(format!(
"the target `{}` is a binary and can't have any \
crate-types set (currently \"{}\")",
@ -323,20 +322,13 @@ fn to_bin_targets(
}
if bin.proc_macro() == Some(true) {
let name = name_or_panic(bin);
errors.push(format!(
"the target `{}` is a binary and can't have `proc-macro` \
set `true`",
name
));
}
if restricted_names::is_conflicting_artifact_name(&name) {
anyhow::bail!(
"the binary target name `{}` is forbidden, \
it conflicts with cargo's build directory names",
name
)
}
}
validate_unique_names(&bins, "binary")?;
@ -794,6 +786,19 @@ fn validate_lib_name(target: &TomlTarget, warnings: &mut Vec<String>) -> CargoRe
Ok(())
}
fn validate_bin_name(bin: &TomlTarget, warnings: &mut Vec<String>) -> CargoResult<()> {
validate_target_name(bin, "binary", "bin", warnings)?;
let name = name_or_panic(bin).to_owned();
if restricted_names::is_conflicting_artifact_name(&name) {
anyhow::bail!(
"the binary target name `{name}` is forbidden, \
it conflicts with cargo's build directory names",
)
}
Ok(())
}
fn validate_target_name(
target: &TomlTarget,
target_kind_human: &str,