Skip to content

Commit

Permalink
Extract & re-use filter_targets in cargo_compile
Browse files Browse the repository at this point in the history
Also drop part of the docs in `list_rule_targets`, as that info is now on `Proposal`.
  • Loading branch information
dwijnand committed Feb 4, 2019
1 parent 129e762 commit df9ca87
Showing 1 changed file with 43 additions and 56 deletions.
99 changes: 43 additions & 56 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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,
};
Expand All @@ -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,
};
Expand Down Expand Up @@ -795,36 +787,22 @@ 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,
target_desc: &'static str,
is_expected_kind: fn(&Target) -> bool,
mode: CompileMode,
) -> CargoResult<Vec<Proposal<'a>>> {
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,
Expand All @@ -834,7 +812,7 @@ fn list_rule_targets<'a>(
}
}
}
Ok(result)
Ok(proposals)
}

/// Find the targets for a specifically named target.
Expand All @@ -845,20 +823,9 @@ fn find_named_targets<'a>(
is_expected_kind: fn(&Target) -> bool,
mode: CompileMode,
) -> CargoResult<Vec<Proposal<'a>>> {
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| {
Expand All @@ -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<Proposal<'a>> {
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
}

0 comments on commit df9ca87

Please sign in to comment.