use std::collections::{BTreeSet, HashMap};
use std::path::Path;
use crate::docref::{doc_files, extract_doc_sections, Mention};
use crate::errors::Result;
use crate::extract::{extract_structure, source_files};
const ONTO: &str = "http://aegis.gastown.local/ontology/";
pub fn to_turtle(root: &Path, repo: &str) -> Result<String> {
let sources: Vec<(String, String, &'static str)> = source_files(root)
.into_iter()
.filter_map(|(file, language)| {
let source = std::fs::read_to_string(&file).ok()?;
Some((rel_path(&file, root), source, language))
})
.collect();
let docs: Vec<(String, String)> = doc_files(root)
.into_iter()
.filter_map(|file| {
let source = std::fs::read_to_string(&file).ok()?;
Some((rel_path(&file, root), source))
})
.collect();
to_turtle_from(repo, &sources, &docs)
}
pub fn to_turtle_at(root: &Path, repo: &str, reference: &str) -> Result<String> {
let files = crate::git::list_files_at(root, reference);
let sources: Vec<(String, String, &'static str)> = files
.iter()
.filter_map(|path| {
let ext = path.extension().and_then(std::ffi::OsStr::to_str)?;
let language = crate::extract::language_for_extension(ext)?;
let source = crate::git::read_blob_at(root, reference, path)?;
Some((path.display().to_string(), source, language))
})
.collect();
let docs: Vec<(String, String)> = files
.iter()
.filter(|p| p.extension().and_then(std::ffi::OsStr::to_str) == Some("md"))
.filter_map(|path| {
let source = crate::git::read_blob_at(root, reference, path)?;
Some((path.display().to_string(), source))
})
.collect();
to_turtle_from(repo, &sources, &docs)
}
fn to_turtle_from(
repo: &str,
sources: &[(String, String, &'static str)],
doc_sources: &[(String, String)],
) -> Result<String> {
let mut modules: Vec<(String, String, &'static str)> = Vec::new();
let mut symbols: Vec<SymbolTriple> = Vec::new();
let mut by_name: HashMap<String, Vec<String>> = HashMap::new();
let mut raw_calls: Vec<(String, String)> = Vec::new();
let mut raw_imports: Vec<(String, Vec<String>)> = Vec::new();
let mut by_stem: HashMap<String, Vec<String>> = HashMap::new();
let mut stem_by_sym: HashMap<String, String> = HashMap::new();
for (rel, source, language) in sources {
let (rel, language) = (rel.as_str(), *language);
let Ok(structure) = extract_structure(source, language) else {
continue;
};
let module = module_iri(repo, rel);
let stem = format!("{language}\u{0}{}", module_stem(rel));
modules.push((module.clone(), rel.to_string(), language));
by_stem
.entry(stem.clone())
.or_default()
.push(module.clone());
for symbol in &structure.symbols {
let iri = symbol_iri(&module, &symbol.scope, &symbol.name);
symbols.push(SymbolTriple {
iri: iri.clone(),
name: symbol.name.clone(),
kind: symbol.kind.as_str().to_string(),
module: module.clone(),
});
stem_by_sym.insert(iri.clone(), stem.clone());
by_name
.entry(format!("{language}\u{0}{}", symbol.name))
.or_default()
.push(iri);
}
for call in &structure.calls {
raw_calls.push((
format!("{language}\u{0}{}", call.caller),
format!("{language}\u{0}{}", call.callee),
));
}
raw_imports.push((
module.clone(),
structure
.import_refs
.iter()
.map(|r| format!("{language}\u{0}{r}"))
.collect(),
));
}
let mut call_edges: BTreeSet<(String, String)> = BTreeSet::new();
for (caller, callee) in raw_calls {
if let (Some(from), Some(to)) = (by_name.get(&caller), by_name.get(&callee)) {
for a in from {
for b in to {
if a != b {
call_edges.insert((a.clone(), b.clone()));
}
}
}
}
}
let mut import_edges: BTreeSet<(String, String)> = BTreeSet::new();
for (from, refs) in raw_imports {
for name in refs {
if let Some(targets) = by_stem.get(&name) {
for to in targets {
if *to != from {
import_edges.insert((from.clone(), to.clone()));
}
}
}
}
}
let mut docs: Vec<DocTriple> = Vec::new();
let mut sections: Vec<SectionTriple> = Vec::new();
let mut ref_edges: BTreeSet<(String, String)> = BTreeSet::new();
for (rel, source) in doc_sources {
let rel = rel.clone();
let doc = document_iri(repo, &rel);
let source = source.as_str();
let mut doc_used = false;
for section in extract_doc_sections(source) {
let sec_iri = format!("{doc}#{}", section.slug);
let mut sec_used = false;
for mention in §ion.mentions {
for target in resolve(mention, &by_name, &stem_by_sym) {
if ref_edges.insert((sec_iri.clone(), target)) {
sec_used = true;
}
}
}
if sec_used {
sections.push(SectionTriple {
iri: sec_iri,
heading: section.heading,
depth: section.depth,
document: doc.clone(),
});
doc_used = true;
}
}
if doc_used {
docs.push(DocTriple {
iri: doc,
path: rel,
});
}
}
Ok(render(
repo,
&modules,
&symbols,
&call_edges,
&import_edges,
&docs,
§ions,
&ref_edges,
))
}
fn resolve(
mention: &Mention,
by_name: &HashMap<String, Vec<String>>,
stem_by_sym: &HashMap<String, String>,
) -> Vec<String> {
let candidates: Vec<String> = by_name
.iter()
.filter(|(key, _)| {
key.rsplit_once('\u{0}')
.map_or(key.as_str(), |(_, name)| name)
== mention.symbol
})
.flat_map(|(_, iris)| iris.iter().cloned())
.collect();
if candidates.is_empty() {
return Vec::new();
}
if let Some(qualifier) = &mention.qualifier {
let narrowed: Vec<String> = candidates
.iter()
.filter(|iri| {
stem_by_sym.get(*iri).is_some_and(|stem| {
stem.rsplit_once('\u{0}')
.map_or(stem.as_str(), |(_, bare)| bare)
.eq_ignore_ascii_case(qualifier)
})
})
.cloned()
.collect();
if !narrowed.is_empty() {
return narrowed;
}
}
candidates
}
struct SymbolTriple {
iri: String,
name: String,
kind: String,
module: String,
}
struct DocTriple {
iri: String,
path: String,
}
struct SectionTriple {
iri: String,
heading: String,
depth: usize,
document: String,
}
#[allow(clippy::too_many_arguments)]
fn render(
repo: &str,
modules: &[(String, String, &'static str)],
symbols: &[SymbolTriple],
call_edges: &BTreeSet<(String, String)>,
import_edges: &BTreeSet<(String, String)>,
docs: &[DocTriple],
sections: &[SectionTriple],
ref_edges: &BTreeSet<(String, String)>,
) -> String {
let mut out = String::new();
out.push_str("@prefix bobbin: <http://aegis.gastown.local/ontology/> .\n");
out.push_str("@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n\n");
for (iri, rel, language) in modules {
out.push_str(&format!(
"<{iri}> a bobbin:CodeModule ;\n bobbin:filePath \"{}\" ;\n \
bobbin:repo \"{}\" ;\n bobbin:language \"{}\" .\n\n",
esc(rel),
esc(repo),
esc(language),
));
}
for symbol in symbols {
out.push_str(&format!(
"<{}> a bobbin:CodeSymbol ;\n bobbin:name \"{}\" ;\n \
bobbin:symbolKind \"{}\" ;\n bobbin:definedIn <{}> .\n",
symbol.iri,
esc(&symbol.name),
esc(&symbol.kind),
symbol.module,
));
}
if !call_edges.is_empty() {
out.push('\n');
for (from, to) in call_edges {
out.push_str(&format!("<{from}> bobbin:calls <{to}> .\n"));
}
}
if !import_edges.is_empty() {
out.push('\n');
for (from, to) in import_edges {
out.push_str(&format!("<{from}> bobbin:imports <{to}> .\n"));
}
}
if !docs.is_empty() {
out.push('\n');
for doc in docs {
out.push_str(&format!(
"<{}> a bobbin:Document ;\n bobbin:filePath \"{}\" ;\n \
bobbin:repo \"{}\" .\n\n",
doc.iri,
esc(&doc.path),
esc(repo),
));
}
}
for section in sections {
out.push_str(&format!(
"<{}> a bobbin:Section ;\n bobbin:heading \"{}\" ;\n \
bobbin:headingDepth {} ;\n bobbin:inDocument <{}> .\n",
section.iri,
esc(§ion.heading),
section.depth,
section.document,
));
}
if !ref_edges.is_empty() {
out.push('\n');
for (from, to) in ref_edges {
out.push_str(&format!("<{from}> bobbin:references <{to}> .\n"));
}
}
out
}
fn module_stem(rel: &str) -> String {
let path = Path::new(rel);
let stem = path
.file_stem()
.map_or_else(String::new, |s| s.to_string_lossy().into_owned());
if stem == "mod" || stem == "__init__" || stem == "index" {
return path
.parent()
.and_then(Path::file_name)
.map_or(stem, |n| n.to_string_lossy().into_owned());
}
stem
}
fn module_iri(repo: &str, rel: &str) -> String {
format!("{ONTO}code/{repo}/{}", rel.replace('/', "%2F"))
}
fn symbol_iri(module: &str, scope: &[String], name: &str) -> String {
let mut iri = String::from(module);
for seg in scope {
iri.push_str("::");
iri.push_str(&iri_segment(seg));
}
iri.push_str("::");
iri.push_str(&iri_segment(name));
iri
}
fn iri_segment(raw: &str) -> String {
let mut out = String::with_capacity(raw.len());
for c in raw.chars() {
match c {
'%' => out.push_str("%25"),
' ' => out.push_str("%20"),
'<' => out.push_str("%3C"),
'>' => out.push_str("%3E"),
'"' => out.push_str("%22"),
'{' => out.push_str("%7B"),
'}' => out.push_str("%7D"),
'|' => out.push_str("%7C"),
'\\' => out.push_str("%5C"),
'^' => out.push_str("%5E"),
'`' => out.push_str("%60"),
'\n' | '\t' => {}
_ => out.push(c),
}
}
out
}
fn document_iri(repo: &str, rel: &str) -> String {
format!("{ONTO}doc/{repo}/{}", rel.replace('/', "%2F"))
}
fn rel_path(file: &Path, root: &Path) -> String {
match file.strip_prefix(root) {
Ok(p) if !p.as_os_str().is_empty() => p.display().to_string(),
_ => file.file_name().map_or_else(
|| file.display().to_string(),
|n| n.to_string_lossy().into_owned(),
),
}
}
fn esc(value: &str) -> String {
value.replace('\\', "\\\\").replace('"', "\\\"")
}
#[cfg(test)]
#[path = "export_test.rs"]
mod export_test;