use super::review_contract::KnowledgeSourceScope;
use crate::domain::{CoverageStatus, SourceLocation};
use crate::live_index::{PublishedGeneration, PublishedSourceSet};
use std::sync::Arc;
pub(crate) fn select_scoped_sources(
source_set: &PublishedSourceSet,
scope: KnowledgeSourceScope,
) -> Vec<Arc<PublishedGeneration>> {
let current_id = &source_set.current_source_id;
let is_git_ref = |generation: &PublishedGeneration| {
matches!(
generation.source.as_deref().map(|source| &source.location),
Some(SourceLocation::GitRef { .. })
)
};
let is_worktree = |generation: &PublishedGeneration| {
matches!(
generation.source.as_deref().map(|source| &source.location),
Some(SourceLocation::WorkingTree { .. })
)
};
match scope {
KnowledgeSourceScope::Current => source_set
.sources
.get(current_id)
.map(Arc::clone)
.into_iter()
.collect(),
KnowledgeSourceScope::LocalRefs => source_set
.sources
.iter()
.filter(|(id, generation)| *id != current_id && is_git_ref(generation))
.map(|(_, generation)| Arc::clone(generation))
.collect(),
KnowledgeSourceScope::Worktrees => source_set
.sources
.iter()
.filter(|(id, generation)| *id != current_id && is_worktree(generation))
.map(|(_, generation)| Arc::clone(generation))
.collect(),
KnowledgeSourceScope::All => {
let mut selected: Vec<Arc<PublishedGeneration>> = source_set
.sources
.get(current_id)
.map(Arc::clone)
.into_iter()
.collect();
selected.extend(
source_set
.sources
.iter()
.filter(|(id, _)| *id != current_id)
.map(|(_, generation)| Arc::clone(generation)),
);
selected
}
}
}
pub(crate) fn source_coverage(generation: &PublishedGeneration) -> CoverageStatus {
generation
.manifest
.as_deref()
.map(|manifest| manifest.coverage)
.unwrap_or(CoverageStatus::Degraded)
}
pub(crate) fn worst_source_coverage(selected: &[Arc<PublishedGeneration>]) -> CoverageStatus {
if selected
.iter()
.any(|generation| source_coverage(generation) == CoverageStatus::Degraded)
{
CoverageStatus::Degraded
} else {
CoverageStatus::Complete
}
}
pub(crate) fn source_scope_label(scope: KnowledgeSourceScope) -> &'static str {
match scope {
KnowledgeSourceScope::Current => "current",
KnowledgeSourceScope::Worktrees => "worktrees",
KnowledgeSourceScope::LocalRefs => "local_refs",
KnowledgeSourceScope::All => "all",
}
}