Merge pull request #186 from dtolnay/defaultrun

Touch up PR 185
This commit is contained in:
David Tolnay 2023-07-16 13:45:52 -07:00 committed by GitHub
commit 8d8139502b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -282,49 +282,49 @@ fn apply_args(cmd: &mut Command, args: &Expand, color: &Coloring, outfile: &Path
line.arg("--no-default-features");
}
let mut no_build_target = true;
let mut has_explicit_build_target = false;
if args.lib {
line.arg("--lib");
no_build_target = false;
has_explicit_build_target = true;
}
if let Some(bin) = &args.bin {
line.arg("--bin");
line.args(bin);
no_build_target = false;
has_explicit_build_target = true;
}
if let Some(example) = &args.example {
line.arg("--example");
line.args(example);
no_build_target = false;
has_explicit_build_target = true;
}
if let Some(test) = &args.test {
line.arg("--test");
line.args(test);
no_build_target = false;
has_explicit_build_target = true;
}
if let Some(bench) = &args.bench {
line.arg("--bench");
line.args(bench);
no_build_target = false;
has_explicit_build_target = true;
}
if no_build_target {
match cargo_metadata(&args.manifest_path) {
if !has_explicit_build_target {
match cargo_metadata(args.manifest_path.as_deref()) {
Ok(cargo_metadata) => {
if let Some(root_package) = cargo_metadata.root_package() {
if let Some(ref default_run) = root_package.default_run {
if let Some(default_run) = &root_package.default_run {
line.arg("--bin");
line.args(Some(default_run));
line.arg(default_run);
}
}
},
}
Err(err) => {
let _ = writeln!(io::stderr(), "WARNING: run cargo metadata fail: {}", err);
},
}
}
}
@ -505,13 +505,12 @@ fn get_color(args: &Expand, config: &Config) -> Coloring {
Coloring::Auto // default
}
fn cargo_metadata(manifest_path: &Option<PathBuf>) -> cargo_metadata::Result<cargo_metadata::Metadata> {
fn cargo_metadata(
manifest_path: Option<&Path>,
) -> cargo_metadata::Result<cargo_metadata::Metadata> {
let mut cmd = cargo_metadata::MetadataCommand::new();
match manifest_path {
Some(manifest_path) => {
cmd.manifest_path(manifest_path);
},
None => {},
if let Some(manifest_path) = manifest_path {
cmd.manifest_path(manifest_path);
}
cmd.exec()
}
}