codegraph_parser_api/
ir.rs1use crate::{
2 entities::{ClassEntity, FunctionEntity, ModuleEntity, TraitEntity},
3 relationships::{CallRelation, ImplementationRelation, ImportRelation, InheritanceRelation},
4};
5use std::path::PathBuf;
6
7#[derive(Debug, Default, Clone)]
13pub struct CodeIR {
14 pub file_path: PathBuf,
16
17 pub module: Option<ModuleEntity>,
19
20 pub functions: Vec<FunctionEntity>,
22
23 pub classes: Vec<ClassEntity>,
25
26 pub traits: Vec<TraitEntity>,
28
29 pub calls: Vec<CallRelation>,
31
32 pub imports: Vec<ImportRelation>,
34
35 pub inheritance: Vec<InheritanceRelation>,
37
38 pub implementations: Vec<ImplementationRelation>,
40}
41
42impl CodeIR {
43 pub fn new(file_path: PathBuf) -> Self {
45 Self {
46 file_path,
47 ..Default::default()
48 }
49 }
50
51 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 pub fn relationship_count(&self) -> usize {
61 self.calls.len() + self.imports.len() + self.inheritance.len() + self.implementations.len()
62 }
63
64 pub fn set_module(&mut self, module: ModuleEntity) {
66 self.module = Some(module);
67 }
68
69 pub fn add_function(&mut self, func: FunctionEntity) {
71 self.functions.push(func);
72 }
73
74 pub fn add_class(&mut self, class: ClassEntity) {
76 self.classes.push(class);
77 }
78
79 pub fn add_trait(&mut self, trait_entity: TraitEntity) {
81 self.traits.push(trait_entity);
82 }
83
84 pub fn add_call(&mut self, call: CallRelation) {
86 self.calls.push(call);
87 }
88
89 pub fn add_import(&mut self, import: ImportRelation) {
91 self.imports.push(import);
92 }
93
94 pub fn add_inheritance(&mut self, inheritance: InheritanceRelation) {
96 self.inheritance.push(inheritance);
97 }
98
99 pub fn add_implementation(&mut self, implementation: ImplementationRelation) {
101 self.implementations.push(implementation);
102 }
103}