pub mod api;
pub mod cache;
pub mod index;
pub mod parser;
pub use cache::{MmapCache, ModuleCache};
pub use index::{IndexBuilder, InvertedIndex, QueryEngine, Symbol, SymbolKind};
pub use parser::{GoModuleParser, ParsedModule};
use anyhow::Result;
use std::path::Path;
use std::sync::Arc;
pub struct Woofind {
index: Arc<InvertedIndex>,
engine: QueryEngine,
}
impl Woofind {
pub fn new() -> Self {
let index = Arc::new(InvertedIndex::new());
let engine = QueryEngine::new(Arc::clone(&index));
Self { index, engine }
}
pub fn load_or_build(dir: &Path) -> Result<Self> {
let cache_dir = dirs::cache_dir()
.map(|d| d.join("woofind"))
.unwrap_or_else(|| std::path::PathBuf::from(".woofind_cache"));
let cache = MmapCache::new(&cache_dir)?;
let index = if cache.is_cache_valid(24) {
cache.load_index()?.unwrap_or_else(InvertedIndex::new)
} else {
let index = InvertedIndex::new();
let builder = IndexBuilder::with_index(Arc::new(index.clone()))?;
builder.build_from_directory(dir)?;
builder.save_to_cache()?;
index
};
let index = Arc::new(index);
let engine = QueryEngine::new(Arc::clone(&index));
Ok(Self { index, engine })
}
pub fn lookup(&self, name: &str) -> Vec<Symbol> {
self.engine.exact_lookup(name)
}
pub fn fuzzy_search(&self, query: &str, limit: usize) -> Vec<Symbol> {
self.engine
.fuzzy_search(query, limit)
.into_iter()
.map(|(s, _)| s)
.collect()
}
pub fn search(&self, query: &str, limit: usize) -> Vec<Symbol> {
self.engine.smart_search(query, limit)
}
pub fn autocomplete(&self, prefix: &str, limit: usize) -> Vec<String> {
self.engine.autocomplete(prefix, limit)
}
pub fn stats(&self) -> index::IndexStats {
self.index.stats()
}
}
impl Default for Woofind {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_client_creation() {
let client = Woofind::new();
let stats = client.stats();
assert_eq!(stats.total_symbols, 0);
}
}