rinne_types/graph.rs
1//! The `CodeGraph` trait seam and shared graph types.
2//!
3//! `SymbolRef` and `Neighborhood` live here so that `rinne-loop`/`rinne-conductor`
4//! can depend on the trait without depending on `rinne-graph`. The concrete
5//! implementation lives in `rinne-graph` behind a `Mutex<Store>`.
6
7use serde::{Deserialize, Serialize};
8
9/// A lightweight reference to a symbol: name, file path, and 1-based line span.
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct SymbolRef {
12 pub name: String,
13 pub file: String,
14 /// 1-based first line of the definition.
15 pub line: u32,
16 /// 1-based last line of the definition span. Equals `line` for one-liners
17 /// and for rows indexed before this field existed (COALESCE fallback).
18 pub end_line: u32,
19}
20
21/// The structural neighborhood of a symbol: its definition site plus all
22/// callers, callees, and imports known to the index.
23#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct Neighborhood {
25 pub definition: SymbolRef,
26 pub callers: Vec<SymbolRef>,
27 pub callees: Vec<SymbolRef>,
28 pub imports: Vec<SymbolRef>,
29 /// `true` when the underlying file has been modified since the last index run.
30 pub stale: bool,
31}
32
33/// Read-only view over the code graph. Implementations must be `Send + Sync`
34/// so they can be shared across async task boundaries.
35pub trait CodeGraph: Send + Sync {
36 /// Returns the neighborhood of the first symbol matching `symbol`, or `None`
37 /// if the symbol is not in the index.
38 ///
39 /// Prefer [`CodeGraph::neighborhood_all`] when the same name may be defined in
40 /// more than one place: this method collapses those to one arbitrary match.
41 fn neighborhood(&self, symbol: &str) -> Option<Neighborhood>;
42
43 /// Returns the neighborhood of *every* symbol matching `symbol`, one per
44 /// definition site, in a deterministic order (by file, then line).
45 ///
46 /// This is the multi-result form that avoids the single-winner collapse of
47 /// [`CodeGraph::neighborhood`]. The default implementation returns at most the
48 /// single [`CodeGraph::neighborhood`] result so existing implementations keep
49 /// compiling; the concrete `rinne-graph` store overrides it to return all.
50 fn neighborhood_all(&self, symbol: &str) -> Vec<Neighborhood> {
51 self.neighborhood(symbol).into_iter().collect()
52 }
53
54 /// Returns the first symbol named `name` defined in `file`, or `None`.
55 fn resolve_in_file(&self, file: &str, name: &str) -> Option<SymbolRef>;
56
57 /// Returns the names of every symbol currently in the index.
58 fn symbol_names(&self) -> Vec<String>;
59}