use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;
use std::str::FromStr;
use crate::model::names::{ModuleName, ProjectName, RecordName};
use crate::model::snapshot::{Snapshot, state};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PinScope {
modules: BTreeSet<ModuleName>,
records: BTreeSet<RecordName>,
path_prefixes: Vec<String>,
}
impl PinScope {
pub fn from_snapshot(
project: ProjectName,
snapshot: &Snapshot<state::Canonical>,
extra_modules: impl IntoIterator<Item = ModuleName>,
) -> Self {
let mut modules: BTreeSet<ModuleName> =
snapshot.modules().iter().map(|m| m.name.clone()).collect();
modules.extend(extra_modules);
let records: BTreeSet<RecordName> = snapshot.records().map(|r| r.name.clone()).collect();
Self {
modules,
records,
path_prefixes: vec![format!("deps/{project}/")],
}
}
pub fn contains_module(&self, module: &ModuleName) -> bool {
self.modules.contains(module)
}
pub fn contains_record(&self, record: &RecordName) -> bool {
self.records.contains(record)
}
pub fn modules(&self) -> &BTreeSet<ModuleName> {
&self.modules
}
pub fn records(&self) -> &BTreeSet<RecordName> {
&self.records
}
pub fn rewrite_path<'p>(&self, path: &'p Path) -> Option<&'p Path> {
let s = path.to_str()?;
for prefix in &self.path_prefixes {
if let Some(rest) = s.strip_prefix(prefix.as_str()) {
return Some(Path::new(rest));
}
}
if self.path_prefixes.is_empty() || !s.starts_with("deps/") {
return Some(path);
}
None
}
}
pub fn parse_module_names<'a>(raw: impl IntoIterator<Item = &'a String>) -> Vec<ModuleName> {
raw.into_iter()
.filter_map(|s| match ModuleName::from_str(s) {
Ok(name) => Some(name),
Err(e) => {
tracing::warn!(value = %s, error = %e, "ignoring invalid module name in config");
None
}
})
.collect()
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct UntrackedTally {
counts: BTreeMap<ModuleName, usize>,
}
impl UntrackedTally {
pub fn record(&mut self, module: ModuleName) {
*self.counts.entry(module).or_insert(0) += 1;
}
pub fn into_map(self) -> BTreeMap<ModuleName, usize> {
self.counts
}
}