codegraph_parser_api/
ir.rs

1use crate::{
2    entities::{ClassEntity, FunctionEntity, ModuleEntity, TraitEntity},
3    relationships::{CallRelation, ImplementationRelation, ImportRelation, InheritanceRelation},
4};
5use std::path::PathBuf;
6
7/// Intermediate representation of extracted code
8///
9/// This is the bridge between language-specific AST and the CodeGraph database.
10/// Parsers extract entities and relationships into this IR, then the IR is
11/// inserted into the graph in a batch operation.
12#[derive(Debug, Default, Clone)]
13pub struct CodeIR {
14    /// Source file path
15    pub file_path: PathBuf,
16
17    /// Module/file entity
18    pub module: Option<ModuleEntity>,
19
20    /// Extracted functions
21    pub functions: Vec<FunctionEntity>,
22
23    /// Extracted classes
24    pub classes: Vec<ClassEntity>,
25
26    /// Extracted traits/interfaces
27    pub traits: Vec<TraitEntity>,
28
29    /// Function call relationships
30    pub calls: Vec<CallRelation>,
31
32    /// Import relationships
33    pub imports: Vec<ImportRelation>,
34
35    /// Inheritance relationships
36    pub inheritance: Vec<InheritanceRelation>,
37
38    /// Implementation relationships
39    pub implementations: Vec<ImplementationRelation>,
40}
41
42impl CodeIR {
43    /// Create a new empty IR
44    pub fn new(file_path: PathBuf) -> Self {
45        Self {
46            file_path,
47            ..Default::default()
48        }
49    }
50
51    /// Total number of entities
52    pub fn entity_count(&self) -> usize {
53        self.functions.len()
54            + self.classes.len()
55            + self.traits.len()
56            + if self.module.is_some() { 1 } else { 0 }
57    }
58
59    /// Total number of relationships
60    pub fn relationship_count(&self) -> usize {
61        self.calls.len() + self.imports.len() + self.inheritance.len() + self.implementations.len()
62    }
63
64    /// Add a module entity
65    pub fn set_module(&mut self, module: ModuleEntity) {
66        self.module = Some(module);
67    }
68
69    /// Add a function
70    pub fn add_function(&mut self, func: FunctionEntity) {
71        self.functions.push(func);
72    }
73
74    /// Add a class
75    pub fn add_class(&mut self, class: ClassEntity) {
76        self.classes.push(class);
77    }
78
79    /// Add a trait
80    pub fn add_trait(&mut self, trait_entity: TraitEntity) {
81        self.traits.push(trait_entity);
82    }
83
84    /// Add a call relationship
85    pub fn add_call(&mut self, call: CallRelation) {
86        self.calls.push(call);
87    }
88
89    /// Add an import relationship
90    pub fn add_import(&mut self, import: ImportRelation) {
91        self.imports.push(import);
92    }
93
94    /// Add an inheritance relationship
95    pub fn add_inheritance(&mut self, inheritance: InheritanceRelation) {
96        self.inheritance.push(inheritance);
97    }
98
99    /// Add an implementation relationship
100    pub fn add_implementation(&mut self, implementation: ImplementationRelation) {
101        self.implementations.push(implementation);
102    }
103}