1pub mod extract;
8pub mod indexer;
9pub mod lang;
10pub mod model;
11pub mod resolve;
12pub mod schema;
13pub mod store;
14
15use std::path::Path;
16use std::sync::Mutex;
17
18use rinne_types::graph::{CodeGraph, Neighborhood, SymbolRef};
19
20use crate::store::Store;
21
22pub struct Graph {
27 store: Mutex<Store>,
28}
29
30impl Graph {
31 pub fn open(db_path: &Path, root: &Path) -> rusqlite::Result<Graph> {
34 let store = Store::open(db_path, root)?;
35 Ok(Graph {
36 store: Mutex::new(store),
37 })
38 }
39
40 pub fn ensure_current(&self, path: &str, source: &str, mtime: i64) -> rusqlite::Result<()> {
42 let store = self.store.lock().expect("graph store lock poisoned");
43 if !store.is_current(path, source) {
44 store.index_file(path, source, mtime)?;
45 }
46 Ok(())
47 }
48
49 pub fn stats(&self) -> (usize, usize, usize) {
51 self.store
52 .lock()
53 .expect("graph store lock poisoned")
54 .stats()
55 }
56
57 pub fn symbols_in(&self, path: &str) -> Vec<crate::model::Symbol> {
59 self.store
60 .lock()
61 .expect("graph store lock poisoned")
62 .symbols_in(path)
63 }
64}
65
66impl CodeGraph for Graph {
67 fn neighborhood(&self, symbol: &str) -> Option<Neighborhood> {
68 self.store
69 .lock()
70 .expect("graph store lock poisoned")
71 .neighborhood(symbol)
72 }
73
74 fn resolve_in_file(&self, file: &str, name: &str) -> Option<SymbolRef> {
75 self.store
76 .lock()
77 .expect("graph store lock poisoned")
78 .resolve_in_file(file, name)
79 }
80
81 fn symbol_names(&self) -> Vec<String> {
82 self.store
83 .lock()
84 .expect("graph store lock poisoned")
85 .symbol_names()
86 }
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92 use rinne_types::graph::CodeGraph;
93
94 #[test]
95 fn graph_exposes_codegraph_trait() {
96 let dir = std::env::temp_dir().join(format!("rinne-graph-test-{}", std::process::id()));
97 std::fs::create_dir_all(&dir).unwrap();
98 let db = dir.join("state.db");
99 let graph = Graph::open(&db, &dir).unwrap();
100 graph.ensure_current("m.rs", "fn helper() {}\nfn main() { helper(); }\n", 0).unwrap();
101 let nb = CodeGraph::neighborhood(&graph, "helper").unwrap();
102 assert_eq!(nb.definition.name, "helper");
103 let _ = std::fs::remove_dir_all(&dir);
104 }
105}