Skip to content

Commit

Permalink
Factor out PackageFilter::matches
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcefrog committed Nov 8, 2023
1 parent d52213a commit 7f0445c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ src/visit.rs: replace attr_is_mutants_skip -> bool with false
src/workspace.rs: replace <impl Debug for Workspace>::fmt -> fmt::Result with Ok(Default::default())
src/workspace.rs: replace <impl Debug for Workspace>::fmt -> fmt::Result with Err(::anyhow::anyhow!("mutated!"))
src/workspace.rs: replace PackageFilter::explicit -> PackageFilter with Default::default()
src/workspace.rs: replace PackageFilter::matches -> bool with true
src/workspace.rs: replace PackageFilter::matches -> bool with false
src/workspace.rs: replace Workspace::open -> Result<Self> with Ok(Default::default())
src/workspace.rs: replace Workspace::open -> Result<Self> with Err(::anyhow::anyhow!("mutated!"))
src/workspace.rs: replace Workspace::packages -> Result<Vec<Arc<Package>>> with Ok(vec![])
Expand All @@ -344,8 +346,6 @@ src/workspace.rs: replace Workspace::discover -> Result<Discovered> with Err(::a
src/workspace.rs: replace Workspace::mutants -> Result<Vec<Mutant>> with Ok(vec![])
src/workspace.rs: replace Workspace::mutants -> Result<Vec<Mutant>> with Ok(vec![Default::default()])
src/workspace.rs: replace Workspace::mutants -> Result<Vec<Mutant>> with Err(::anyhow::anyhow!("mutated!"))
src/workspace.rs: replace filter_package_metadata -> Vec<&'m cargo_metadata::Package> with vec![]
src/workspace.rs: replace filter_package_metadata -> Vec<&'m cargo_metadata::Package> with vec![&Default::default()]
src/workspace.rs: replace direct_package_sources -> Result<Vec<Utf8PathBuf>> with Ok(vec![])
src/workspace.rs: replace direct_package_sources -> Result<Vec<Utf8PathBuf>> with Ok(vec![Default::default()])
src/workspace.rs: replace direct_package_sources -> Result<Vec<Utf8PathBuf>> with Err(::anyhow::anyhow!("mutated!"))
Expand Down
32 changes: 14 additions & 18 deletions src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ impl PackageFilter {
pub fn explicit<S: ToString, I: IntoIterator<Item = S>>(names: I) -> PackageFilter {
PackageFilter::Explicit(names.into_iter().map(|s| s.to_string()).collect_vec())
}

pub fn matches(&self, package_metadata: &cargo_metadata::Package) -> bool {
match self {
PackageFilter::All => true,
PackageFilter::Explicit(include_names) => {
include_names.contains(&package_metadata.name)
}
PackageFilter::Auto(..) => todo!(),
}
}
}

/// A package and the top source files within it.
Expand Down Expand Up @@ -77,8 +87,11 @@ impl Workspace {
/// Find all the packages and their top source files.
fn package_tops(&self, package_filter: &PackageFilter) -> Result<Vec<PackageTop>> {
let mut tops = Vec::new();
for package_metadata in filter_package_metadata(&self.metadata, package_filter)
for package_metadata in self
.metadata
.workspace_packages()
.into_iter()
.filter(|pmeta| package_filter.matches(pmeta))
.sorted_by_key(|p| &p.name)
{
check_interrupted()?;
Expand Down Expand Up @@ -160,23 +173,6 @@ impl Workspace {
}
}

fn filter_package_metadata<'m>(
metadata: &'m cargo_metadata::Metadata,
package_filter: &PackageFilter,
) -> Vec<&'m cargo_metadata::Package> {
metadata
.workspace_packages()
.iter()
.filter(move |pmeta| match package_filter {
PackageFilter::All => true,
PackageFilter::Explicit(include_names) => include_names.contains(&pmeta.name),
PackageFilter::Auto(..) => todo!(),
})
.sorted_by_key(|pm| &pm.name)
.copied()
.collect()
}

/// Find all the files that are named in the `path` of targets in a Cargo manifest that should be tested.
///
/// These are the starting points for discovering source files.
Expand Down

0 comments on commit 7f0445c

Please sign in to comment.