Skip to main content

omni_index/
types.rs

1//! Core types for the Omniscient Code Index.
2//!
3//! This module defines the fundamental data structures used across all layers:
4//! - Module Topology (Layer 1)
5//! - Symbol Resolution (Layer 2)
6//! - Semantic Embeddings (Layer 3)
7
8use lasso::Spur;
9use std::path::PathBuf;
10
11/// Interned string handle for memory-efficient symbol storage.
12pub type InternedString = Spur;
13
14/// Unique identifier for symbols in the index.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16pub struct SymbolId(pub u32);
17
18/// Unique identifier for files in the index.
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
20pub struct FileId(pub u32);
21
22// ============================================================================
23// Layer 1: Module Topology Types
24// ============================================================================
25
26/// Node types for the Module Topology Graph.
27#[derive(Debug, Clone)]
28pub enum TopologyNode {
29    /// Workspace or crate root
30    Crate {
31        name: String,
32        path: PathBuf,
33        is_workspace: bool,
34    },
35    /// A module (from mod declaration or directory)
36    Module {
37        name: String,
38        path: PathBuf,
39        is_inline: bool,
40    },
41    /// A source file
42    File { path: PathBuf, file_id: FileId },
43}
44
45/// Edge types for the Module Topology Graph.
46#[derive(Debug, Clone)]
47pub enum TopologyEdge {
48    /// Parent contains child (crate -> module, module -> file)
49    Contains,
50    /// Import relationship from `use` statement
51    Imports { use_path: String, is_glob: bool },
52    /// Re-export via `pub use`
53    ReExports { original_path: String },
54}
55
56/// Metrics for a topology node.
57#[derive(Debug, Clone, Default)]
58pub struct TopologyMetrics {
59    /// PageRank score for relevance ranking
60    pub relevance_score: f64,
61    /// Number of modifications in recent history
62    pub churn_count: u32,
63    /// Test coverage percentage (0.0 - 1.0)
64    pub coverage: Option<f32>,
65}
66
67// ============================================================================
68// Layer 2: Symbol Resolution Types
69// ============================================================================
70
71/// Location of a syntax element in a file.
72#[derive(Debug, Clone, PartialEq, Eq, Hash)]
73pub struct Location {
74    pub file: PathBuf,
75    pub start_byte: usize,
76    pub end_byte: usize,
77    pub start_line: usize,
78    pub start_col: usize,
79    pub end_line: usize,
80    pub end_col: usize,
81}
82
83impl Location {
84    pub fn new(file: PathBuf, start_byte: usize, end_byte: usize) -> Self {
85        Self {
86            file,
87            start_byte,
88            end_byte,
89            start_line: 0,
90            start_col: 0,
91            end_line: 0,
92            end_col: 0,
93        }
94    }
95
96    pub fn with_positions(
97        mut self,
98        start_line: usize,
99        start_col: usize,
100        end_line: usize,
101        end_col: usize,
102    ) -> Self {
103        self.start_line = start_line;
104        self.start_col = start_col;
105        self.end_line = end_line;
106        self.end_col = end_col;
107        self
108    }
109}
110
111/// Kind of symbol in the codebase.
112#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
113pub enum SymbolKind {
114    Function,
115    Method,
116    Struct,
117    Enum,
118    Trait,
119    Impl,
120    Const,
121    Static,
122    Module,
123    TypeAlias,
124    Macro,
125    Field,
126    Variant,
127}
128
129impl SymbolKind {
130    pub fn as_str(&self) -> &'static str {
131        match self {
132            Self::Function => "function",
133            Self::Method => "method",
134            Self::Struct => "struct",
135            Self::Enum => "enum",
136            Self::Trait => "trait",
137            Self::Impl => "impl",
138            Self::Const => "const",
139            Self::Static => "static",
140            Self::Module => "module",
141            Self::TypeAlias => "type",
142            Self::Macro => "macro",
143            Self::Field => "field",
144            Self::Variant => "variant",
145        }
146    }
147}
148
149/// Visibility of a symbol.
150#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
151pub enum Visibility {
152    #[default]
153    Private,
154    /// pub(crate)
155    Crate,
156    /// pub(super)
157    Super,
158    /// pub(in path)
159    Restricted,
160    /// pub
161    Public,
162}
163
164/// Function/method signature information.
165#[derive(Debug, Clone, Default)]
166pub struct Signature {
167    pub params: Vec<String>,
168    pub return_type: Option<String>,
169    pub is_async: bool,
170    pub is_unsafe: bool,
171    pub is_const: bool,
172    pub generics: Option<String>,
173    pub where_clause: Option<String>,
174}
175
176/// A symbol definition in the codebase.
177#[derive(Debug, Clone)]
178pub struct SymbolDef {
179    /// Simple name (e.g., "foo")
180    pub name: InternedString,
181    /// Fully qualified scoped name (e.g., "crate::module::Struct::foo")
182    pub scoped_name: InternedString,
183    /// Kind of symbol
184    pub kind: SymbolKind,
185    /// Source location
186    pub location: Location,
187    /// Signature (for functions/methods)
188    pub signature: Option<Signature>,
189    /// Visibility
190    pub visibility: Visibility,
191    /// Attributes (e.g., #[test], #[derive(...)])
192    pub attributes: Vec<String>,
193    /// Documentation comments
194    pub doc_comment: Option<String>,
195    /// Parent symbol (for methods -> impl, fields -> struct)
196    pub parent: Option<InternedString>,
197}
198
199/// A call edge in the call graph.
200#[derive(Debug, Clone)]
201pub struct CallEdge {
202    /// Scoped name of the caller
203    pub caller: InternedString,
204    /// Simple name of the callee (unscoped for dynamic resolution)
205    pub callee_name: String,
206    /// Location of the call site
207    pub location: Location,
208    /// Whether this is a method call (has receiver)
209    pub is_method_call: bool,
210}
211
212/// Import information from `use` statements.
213#[derive(Debug, Clone)]
214pub struct ImportInfo {
215    /// The full use path (e.g., "std::collections::HashMap")
216    pub path: String,
217    /// Imported name or alias
218    pub name: String,
219    /// Whether it's a glob import (use foo::*)
220    pub is_glob: bool,
221    /// Location of the use statement
222    pub location: Location,
223}
224
225// ============================================================================
226// Layer 3: Semantic Types
227// ============================================================================
228
229/// Entry in the semantic embedding index.
230#[derive(Debug, Clone)]
231pub struct SemanticEntry {
232    pub symbol_id: SymbolId,
233    pub scoped_name: InternedString,
234    /// Embedding vector (may be quantized)
235    pub embedding: EmbeddingData,
236}
237
238/// Embedding storage format.
239#[derive(Debug, Clone)]
240pub enum EmbeddingData {
241    /// Full float32 embedding
242    Float32(Vec<f32>),
243    /// Binary quantized for memory efficiency
244    Binary(Vec<u8>),
245}
246
247impl EmbeddingData {
248    pub fn dimension(&self) -> usize {
249        match self {
250            Self::Float32(v) => v.len(),
251            Self::Binary(v) => v.len() * 8,
252        }
253    }
254}
255
256// ============================================================================
257// Analysis Types
258// ============================================================================
259
260/// Result of dead code analysis.
261#[derive(Debug, Clone)]
262pub struct DeadCodeReport {
263    /// Symbols that are potentially dead (unreachable)
264    pub dead_symbols: Vec<InternedString>,
265    /// Entry points used for reachability analysis
266    pub entry_points: Vec<InternedString>,
267    /// Symbols marked as potentially live (conservative)
268    pub potentially_live: Vec<InternedString>,
269}
270
271/// Coverage data for a symbol.
272#[derive(Debug, Clone)]
273pub struct SymbolCoverage {
274    pub symbol: InternedString,
275    pub lines_covered: u32,
276    pub lines_total: u32,
277    pub branches_covered: u32,
278    pub branches_total: u32,
279}
280
281// ============================================================================
282// Intervention Types
283// ============================================================================
284
285/// Severity of an intervention.
286#[derive(Debug, Clone, Copy, PartialEq, Eq)]
287pub enum InterventionSeverity {
288    /// Informational - similar code exists
289    Info,
290    /// Warning - likely duplicate
291    Warning,
292    /// Block - high confidence duplicate, should use existing
293    Block,
294}
295
296/// An intervention triggered by duplicate detection.
297#[derive(Debug, Clone)]
298pub struct Intervention {
299    pub severity: InterventionSeverity,
300    pub message: String,
301    pub existing_symbol: InternedString,
302    pub existing_location: Location,
303    pub similarity_score: f32,
304    pub recommendation: String,
305}
306
307/// Result of similarity detection.
308#[derive(Debug, Clone)]
309pub struct SimilarityMatch {
310    pub symbol: InternedString,
311    pub location: Location,
312    pub score: f32,
313    pub kind: SymbolKind,
314}
315
316// ============================================================================
317// Query Types
318// ============================================================================
319
320/// Options for search queries.
321#[derive(Debug, Clone, Default)]
322pub struct SearchOptions {
323    /// Maximum results to return
324    pub limit: Option<usize>,
325    /// Filter by symbol kind
326    pub kind_filter: Option<Vec<SymbolKind>>,
327    /// Filter by file pattern (glob)
328    pub file_pattern: Option<String>,
329    /// Include private symbols
330    pub include_private: bool,
331    /// Lines of context before match
332    pub context_before: usize,
333    /// Lines of context after match
334    pub context_after: usize,
335}
336
337/// A search result with snippet.
338#[derive(Debug, Clone)]
339pub struct SearchResult {
340    pub symbol: InternedString,
341    pub kind: SymbolKind,
342    pub location: Location,
343    pub score: f32,
344    pub snippet: Option<String>,
345}