use crate::corpus::Corpus;
use crate::workspace::owners::{PathMatch, matcher};
use std::collections::BTreeMap;
#[derive(Debug, Clone)]
pub struct Governing {
pub idx: usize,
pub pattern: String,
pub specificity: usize,
}
struct Claim {
pattern: String,
local: String,
matcher: Option<PathMatch>,
specificity: usize,
}
enum Claims {
Globs(Vec<Claim>),
Owner(String),
}
pub struct GoverningIndex {
docs: Vec<(usize, Claims)>,
by_prefix: BTreeMap<String, Vec<usize>>,
anywhere: Vec<usize>,
}
impl GoverningIndex {
pub fn new(c: &Corpus) -> Self {
Self::filtered(c, |_| true)
}
pub fn filtered(c: &Corpus, keep: impl Fn(usize) -> bool) -> Self {
let mut ix = GoverningIndex { docs: vec![], by_prefix: BTreeMap::new(), anywhere: vec![] };
for (i, d) in c.docs.iter().enumerate().filter(|(i, _)| keep(*i)) {
let globs = d.rel().remove("governs").unwrap_or_default();
let claims = if globs.is_empty() {
match c.owners.by_id(&d.id.owner) {
Some(o) if !o.path.is_empty() => Claims::Owner(o.path.clone()),
_ => continue,
}
} else {
Claims::Globs(globs.into_iter().map(|g| compile(&c.cfg.raw.owners, g)).collect())
};
let pos = ix.docs.len();
let mut keys: Vec<Option<String>> = match &claims {
Claims::Globs(list) => list.iter().map(|g| prefix_key(&g.local)).collect(),
Claims::Owner(o) => vec![Some(o.clone())],
};
keys.sort();
keys.dedup();
for key in keys {
match key {
Some(k) => ix.by_prefix.entry(k).or_default().push(pos),
None => ix.anywhere.push(pos),
}
}
ix.docs.push((i, claims));
}
ix
}
pub fn governing(&self, c: &Corpus, path: &str) -> Vec<Governing> {
let path = path.trim_start_matches("./").trim_end_matches('/');
let inside =
(!path.is_empty() && c.cfg.spec_home.join(path).is_dir()).then(|| files_under(&c.cfg.spec_home, path));
let candidates = self.candidates(path, inside.is_some());
let mut out = vec![];
for (i, claims) in candidates.into_iter().map(|p| &self.docs[p]) {
match claims {
Claims::Owner(owner) => {
let under = inside.is_some() && crate::paths::is_below(owner, path);
if crate::paths::is_under(path, owner) || under {
out.push(Governing { idx: *i, pattern: format!("{owner}/**"), specificity: owner.len() });
}
}
Claims::Globs(list) => {
let hit = list.iter().find(|g| {
g.matcher.as_ref().is_some_and(|m| m.is_match(path))
|| inside.as_ref().is_some_and(|files| below_dir(g, path, files))
});
if let Some(g) = hit {
out.push(Governing { idx: *i, pattern: g.pattern.clone(), specificity: g.specificity });
}
}
}
}
out.sort_by(|a, b| b.specificity.cmp(&a.specificity).then(a.idx.cmp(&b.idx)));
out
}
}
impl GoverningIndex {
fn candidates(&self, path: &str, folder: bool) -> Vec<usize> {
if path.is_empty() {
return (0..self.docs.len()).collect();
}
let mut out = self.anywhere.clone();
let above = path.match_indices('/').map(|(i, _)| &path[..i]).filter(|p| !p.is_empty());
for key in above.chain(std::iter::once(path)) {
out.extend(self.by_prefix.get(key).into_iter().flatten());
}
if folder {
let below = format!("{path}/");
let keyed = self.by_prefix.range(below.clone()..).take_while(|(k, _)| k.starts_with(&below));
out.extend(keyed.flat_map(|(_, v)| v));
}
out.sort_unstable();
out.dedup();
out
}
}
fn compile(owners: &crate::config::types::OwnersSection, pattern: String) -> Claim {
let local = owners.unmount(&pattern).to_string();
let specificity = literal(&local).len();
Claim { matcher: matcher(&local), pattern, local, specificity }
}
fn literal(g: &str) -> &str {
g.split(['*', '?', '[', '{']).next().unwrap_or("")
}
pub(crate) fn prefix_key(pattern: &str) -> Option<String> {
let p = pattern.trim_end_matches('/');
let key = if p.contains(['*', '?', '[', '{']) {
let lit = literal(p);
if !p.contains('/') || lit.contains('\\') || matcher(pattern).is_none() {
return None;
}
lit.rsplit_once('/')?.0
} else {
p
};
(!key.is_empty()).then(|| key.to_string())
}
fn below_dir(g: &Claim, dir: &str, files: &[String]) -> bool {
if crate::paths::is_below(literal(&g.local), dir) {
return true;
}
g.matcher.as_ref().is_some_and(|m| files.iter().any(|f| m.is_match(f)))
}
fn files_under(root: &std::path::Path, dir: &str) -> Vec<String> {
const MAX: usize = 5000;
let mut out = vec![];
let mut stack = vec![dir.to_string()];
while let Some(d) = stack.pop() {
let Ok(rd) = std::fs::read_dir(root.join(&d)) else { continue };
for e in rd.flatten() {
let name = e.file_name().to_string_lossy().to_string();
if name.starts_with('.') {
continue;
}
let rel = format!("{d}/{name}");
if e.file_type().is_ok_and(|t| t.is_dir()) {
stack.push(rel);
} else if out.len() < MAX {
out.push(rel);
}
}
if out.len() >= MAX {
break;
}
}
out
}