use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Instant, SystemTime};
use rayon::prelude::*;
use super::{Collected, RunOptions, not_enabled, note};
use crate::classify::Finding;
use crate::config::{Resolved, Resolver};
use crate::error::Result;
use crate::size::Measured;
fn keep_survivors(
root: &Path,
resolver: &Resolver,
findings: Vec<Finding>,
options: &RunOptions,
collected: &mut Collected,
) -> Result<Vec<(Finding, Arc<Resolved>)>> {
let now = SystemTime::now();
let started = Instant::now();
let mut survivors: Vec<(Finding, Arc<Resolved>)> = Vec::with_capacity(findings.len());
for finding in findings {
let dir = finding.path.parent().unwrap_or(root);
let config = resolver.for_dir(dir)?;
if let Some(id) = finding.artifact()
&& !config.is_enabled(&finding.path, id)
{
let reason = not_enabled(id);
note(collected, options.verbose, finding.path, reason);
continue;
}
let keep = config.keep_for(&finding.path, finding.ecosystem());
let held = keep
.min_age
.and_then(|_| crate::size::newest_mtime(&finding.path))
.and_then(|modified| keep.holds_by_age(modified, now));
match held {
Some(reason) => note(collected, options.verbose, finding.path, reason),
None => survivors.push((finding, config)),
}
}
collected.policy += started.elapsed();
Ok(survivors)
}
pub(super) fn select(
root: &Path,
resolver: &Resolver,
findings: Vec<Finding>,
options: &RunOptions,
collected: &mut Collected,
) -> Result<Selected> {
let survivors = keep_survivors(root, resolver, findings, options, collected)?;
let needs_size = |finding: &Finding, config: &Resolved| {
let keep = config.keep_for(&finding.path, finding.ecosystem());
options.dry_run || keep.min_size.is_some() || keep.max_size.is_some()
};
let sizes: Vec<Option<Measured>> = if survivors.iter().any(|(finding, config)| needs_size(finding, config)) {
let started = Instant::now();
let sizes = survivors
.par_iter()
.map(|(finding, config)| needs_size(finding, config).then(|| crate::size::measure_fully(&finding.path)))
.collect();
collected.size += started.elapsed();
sizes
} else {
vec![None; survivors.len()]
};
let started = Instant::now();
let mut to_remove: Vec<(Finding, Option<Measured>)> = Vec::with_capacity(survivors.len());
for ((finding, config), measured) in survivors.into_iter().zip(sizes) {
let keep = config.keep_for(&finding.path, finding.ecosystem());
match measured.and_then(|size| keep.holds_by_size(size.bytes)) {
Some(reason) => note(collected, options.verbose, finding.path, reason),
None => to_remove.push((finding, measured)),
}
}
let selected = split_covered(to_remove);
collected.policy += started.elapsed();
Ok(selected)
}
fn split_covered(mut to_remove: Vec<(Finding, Option<Measured>)>) -> Selected {
to_remove.sort_by(|(left, _), (right, _)| left.path.cmp(&right.path));
let mut covering: Option<PathBuf> = None;
let mut covered = Vec::new();
to_remove.retain(|(finding, measured)| {
if let Some(outer) = covering.as_ref().filter(|outer| finding.path.starts_with(outer)) {
covered.push((finding.clone(), *measured, outer.clone()));
return false;
}
covering = Some(finding.path.clone());
true
});
Selected { to_remove, covered }
}
pub(super) struct Selected {
pub(super) to_remove: Vec<(Finding, Option<Measured>)>,
pub(super) covered: Vec<(Finding, Option<Measured>, PathBuf)>,
}