From df9ca8713d2816c40e0b3d31d4ec1b8543f10fb1 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Mon, 4 Feb 2019 16:27:34 +0100 Subject: [PATCH] Extract & re-use filter_targets in cargo_compile Also drop part of the docs in `list_rule_targets`, as that info is now on `Proposal`. --- src/cargo/ops/cargo_compile.rs | 99 +++++++++++++++------------------- 1 file changed, 43 insertions(+), 56 deletions(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 7fec9a06e6b..1f8df9626aa 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -619,24 +619,16 @@ fn generate_targets<'a>( } => { if lib { let mut libs = Vec::new(); - for pkg in packages { - for target in pkg.targets().iter().filter(|t| t.is_lib()) { - if build_config.mode == CompileMode::Doctest && !target.doctestable() { - ws.config() - .shell() - .warn(format!( - "doc tests are not supported for crate type(s) `{}` in package `{}`", - target.rustc_crate_types().join(", "), - pkg.name() - ))?; - } else { - libs.push(Proposal { - pkg, - target, - requires_features: false, - mode: build_config.mode, - }); - } + for proposal in filter_targets(packages, Target::is_lib, false, build_config.mode) { + let Proposal { target, pkg, .. } = proposal; + if build_config.mode == CompileMode::Doctest && !target.doctestable() { + ws.config().shell().warn(format!( + "doc tests are not supported for crate type(s) `{}` in package `{}`", + target.rustc_crate_types().join(", "), + pkg.name() + ))?; + } else { + libs.push(proposal) } } if !all_targets && libs.is_empty() { @@ -655,7 +647,7 @@ fn generate_targets<'a>( // If --tests was specified, add all targets that would be // generated by `cargo test`. - let test_filter = match *tests { + let test_filter = match tests { FilterRule::All => Target::tested, FilterRule::Just(_) => Target::is_test, }; @@ -666,7 +658,7 @@ fn generate_targets<'a>( }; // If --benches was specified, add all targets that would be // generated by `cargo bench`. - let bench_filter = match *benches { + let bench_filter = match benches { FilterRule::All => Target::benched, FilterRule::Just(_) => Target::is_bench, }; @@ -795,10 +787,7 @@ fn filter_default_targets(targets: &[Target], mode: CompileMode) -> Vec<&Target> } } -/// Returns a list of targets based on command-line target selection flags. -/// The return value is a list of `(Package, Target, bool, CompileMode)` -/// tuples. The `bool` value indicates whether or not all required features -/// *must* be present. +/// Returns a list of proposed targets based on command-line target selection flags. fn list_rule_targets<'a>( packages: &[&'a Package], rule: &FilterRule, @@ -806,25 +795,14 @@ fn list_rule_targets<'a>( is_expected_kind: fn(&Target) -> bool, mode: CompileMode, ) -> CargoResult>> { - let mut result = Vec::new(); - match *rule { + let mut proposals = Vec::new(); + match rule { FilterRule::All => { - for pkg in packages { - for target in pkg.targets() { - if is_expected_kind(target) { - result.push(Proposal { - pkg, - target, - requires_features: false, - mode, - }); - } - } - } + proposals.extend(filter_targets(packages, is_expected_kind, false, mode)) } - FilterRule::Just(ref names) => { + FilterRule::Just(names) => { for name in names { - result.extend(find_named_targets( + proposals.extend(find_named_targets( packages, name, target_desc, @@ -834,7 +812,7 @@ fn list_rule_targets<'a>( } } } - Ok(result) + Ok(proposals) } /// Find the targets for a specifically named target. @@ -845,20 +823,9 @@ fn find_named_targets<'a>( is_expected_kind: fn(&Target) -> bool, mode: CompileMode, ) -> CargoResult>> { - let mut result = Vec::new(); - for pkg in packages { - for target in pkg.targets() { - if target.name() == target_name && is_expected_kind(target) { - result.push(Proposal { - pkg, - target, - requires_features: true, - mode, - }); - } - } - } - if result.is_empty() { + let filter = |t: &Target| t.name() == target_name && is_expected_kind(t); + let proposals = filter_targets(packages, filter, true, mode); + if proposals.is_empty() { let suggestion = packages .iter() .flat_map(|pkg| { @@ -880,5 +847,25 @@ fn find_named_targets<'a>( None => failure::bail!("no {} target named `{}`", target_desc, target_name), } } - Ok(result) + Ok(proposals) +} + +fn filter_targets<'a>( + packages: &[&'a Package], + predicate: impl Fn(&Target) -> bool, + requires_features: bool, + mode: CompileMode, +) -> Vec> { + let mut proposals = Vec::new(); + for pkg in packages { + for target in pkg.targets().iter().filter(|t| predicate(t)) { + proposals.push(Proposal { + pkg, + target, + requires_features, + mode, + }); + } + } + proposals }