mod builtins;
mod file_table;
mod graph;
mod imports;
mod state;
mod trigram;
mod walker;
mod words;
use std::sync::{Arc, Mutex};
use harn_vm::VmValue;
use crate::error::HostlibError;
use crate::registry::{BuiltinRegistry, HostlibCapability, RegisteredBuiltin, SyncHandler};
pub use builtins::SharedIndex;
pub use file_table::{FileId, IndexedFile, IndexedSymbol};
pub use graph::DepGraph;
pub use state::{BuildOutcome, IndexState};
pub use trigram::TrigramIndex;
pub use words::{WordHit, WordIndex};
#[derive(Clone, Default)]
pub struct CodeIndexCapability {
index: SharedIndex,
}
impl CodeIndexCapability {
pub fn new() -> Self {
Self {
index: Arc::new(Mutex::new(None)),
}
}
pub fn shared(&self) -> SharedIndex {
self.index.clone()
}
}
impl HostlibCapability for CodeIndexCapability {
fn module_name(&self) -> &'static str {
"code_index"
}
fn register_builtins(&self, registry: &mut BuiltinRegistry) {
register(
registry,
self.index.clone(),
builtins::BUILTIN_QUERY,
"query",
builtins::run_query,
);
register(
registry,
self.index.clone(),
builtins::BUILTIN_REBUILD,
"rebuild",
builtins::run_rebuild,
);
register(
registry,
self.index.clone(),
builtins::BUILTIN_STATS,
"stats",
builtins::run_stats,
);
register(
registry,
self.index.clone(),
builtins::BUILTIN_IMPORTS_FOR,
"imports_for",
builtins::run_imports_for,
);
register(
registry,
self.index.clone(),
builtins::BUILTIN_IMPORTERS_OF,
"importers_of",
builtins::run_importers_of,
);
}
}
fn register(
registry: &mut BuiltinRegistry,
index: SharedIndex,
name: &'static str,
method: &'static str,
runner: fn(&SharedIndex, &[VmValue]) -> Result<VmValue, HostlibError>,
) {
let captured = index;
let handler: SyncHandler = Arc::new(move |args| runner(&captured, args));
registry.register(RegisteredBuiltin {
name,
module: "code_index",
method,
handler,
});
}