use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use sha2::{Digest, Sha256};
use super::Reached;
use crate::extract::{extract_structure, language_for_extension, FileStructure};
use crate::types::Tier;
#[derive(Debug)]
pub struct ParsedFile {
pub hash: String,
pub language: Option<&'static str>,
pub structure: FileStructure,
}
impl ParsedFile {
#[must_use]
pub fn parse(rel: &str, source: &str) -> Self {
let hash = format!("{:x}", Sha256::digest(source.as_bytes()));
let language = Path::new(rel)
.extension()
.and_then(std::ffi::OsStr::to_str)
.and_then(language_for_extension);
let structure = language
.and_then(|l| extract_structure(source, l).ok())
.unwrap_or_default();
Self {
hash,
language,
structure,
}
}
}
#[derive(Debug, Clone)]
pub struct OverlaySymbol {
pub name: String,
pub kind: String,
pub file: String,
pub start_line: usize,
pub tier: Tier,
pub language: &'static str,
}
#[derive(Debug, Default)]
pub struct Overlay {
files: HashMap<String, Arc<ParsedFile>>,
symbols: Vec<OverlaySymbol>,
by_name: HashMap<String, Vec<u32>>,
callees_of: Vec<Vec<String>>,
callers_of: HashMap<String, Vec<u32>>,
}
#[must_use]
pub fn update_frontier(view: &super::TenantView<'_>, changed: &[&str], hops: u32) -> Vec<Reached> {
view.frontier(changed, hops)
}
#[must_use]
pub fn update_frontier_bounded(
view: &super::TenantView<'_>,
changed: &[&str],
hops: u32,
high_fanin_threshold: usize,
) -> Vec<Reached> {
let result = view.frontier_bounded(changed, hops, high_fanin_threshold);
if !result.bounded_seeds.is_empty() {
tracing::warn!(
seeds = ?result.bounded_seeds,
threshold = high_fanin_threshold,
"high-fan-in cascade bounded to 1 hop (FR-18 §14.2)"
);
}
result.reached
}
impl Overlay {
pub fn touch(&mut self, rel: &str, parsed: Arc<ParsedFile>) {
self.files.insert(rel.to_string(), parsed);
self.rebuild();
}
pub fn revert(&mut self, rel: &str) -> bool {
let was = self.files.remove(rel).is_some();
if was {
self.rebuild();
}
was
}
#[must_use]
pub fn is_touched(&self, rel: &str) -> bool {
self.files.contains_key(rel)
}
pub fn touched(&self) -> impl Iterator<Item = &str> {
self.files.keys().map(String::as_str)
}
#[must_use]
pub fn touched_count(&self) -> usize {
self.files.len()
}
#[must_use]
pub fn parsed(&self, rel: &str) -> Option<&Arc<ParsedFile>> {
self.files.get(rel)
}
#[must_use]
pub fn symbol_count(&self) -> usize {
self.symbols.len()
}
#[must_use]
pub fn defs(&self, name: &str) -> &[u32] {
self.by_name.get(name).map_or(&[], Vec::as_slice)
}
#[must_use]
pub fn symbol(&self, id: u32) -> &OverlaySymbol {
&self.symbols[id as usize]
}
#[must_use]
pub fn callee_names(&self, id: u32) -> &[String] {
&self.callees_of[id as usize]
}
#[must_use]
pub fn callers_of(&self, name: &str) -> &[u32] {
self.callers_of.get(name).map_or(&[], Vec::as_slice)
}
fn rebuild(&mut self) {
self.symbols.clear();
self.by_name.clear();
self.callees_of.clear();
self.callers_of.clear();
let mut rels: Vec<&String> = self.files.keys().collect();
rels.sort();
let mut per_file_ids: HashMap<&str, Vec<u32>> = HashMap::new();
for rel in &rels {
let parsed = &self.files[*rel];
let Some(language) = parsed.language else {
continue;
};
for symbol in &parsed.structure.symbols {
let id = u32::try_from(self.symbols.len()).expect("overlay id fits u32");
self.symbols.push(OverlaySymbol {
name: symbol.name.clone(),
kind: symbol.kind.as_str().to_string(),
file: (*rel).clone(),
start_line: symbol.start_line,
tier: symbol.tier,
language,
});
self.by_name
.entry(symbol.name.clone())
.or_default()
.push(id);
per_file_ids.entry(rel.as_str()).or_default().push(id);
self.callees_of.push(Vec::new());
}
}
for rel in &rels {
let parsed = &self.files[*rel];
let ids = per_file_ids
.get(rel.as_str())
.map_or(&[][..], Vec::as_slice);
for call in &parsed.structure.calls {
for &id in ids {
if self.symbols[id as usize].name == call.caller {
self.callees_of[id as usize].push(call.callee.clone());
self.callers_of
.entry(call.callee.clone())
.or_default()
.push(id);
}
}
}
}
}
}