codegraph_parser_api/entities/
class.rs

1use super::function::FunctionEntity;
2use serde::{Deserialize, Serialize};
3
4/// Represents a class field/attribute
5#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
6pub struct Field {
7    /// Field name
8    pub name: String,
9
10    /// Type annotation (if available)
11    pub type_annotation: Option<String>,
12
13    /// Visibility: "public", "private", "protected"
14    pub visibility: String,
15
16    /// Is this a static/class field?
17    pub is_static: bool,
18
19    /// Is this a constant?
20    pub is_constant: bool,
21
22    /// Default value
23    pub default_value: Option<String>,
24}
25
26impl Field {
27    pub fn new(name: impl Into<String>) -> Self {
28        Self {
29            name: name.into(),
30            type_annotation: None,
31            visibility: "public".to_string(),
32            is_static: false,
33            is_constant: false,
34            default_value: None,
35        }
36    }
37
38    pub fn with_type(mut self, type_ann: impl Into<String>) -> Self {
39        self.type_annotation = Some(type_ann.into());
40        self
41    }
42
43    pub fn with_visibility(mut self, vis: impl Into<String>) -> Self {
44        self.visibility = vis.into();
45        self
46    }
47
48    pub fn static_field(mut self) -> Self {
49        self.is_static = true;
50        self
51    }
52
53    pub fn constant(mut self) -> Self {
54        self.is_constant = true;
55        self
56    }
57
58    pub fn with_default(mut self, default: impl Into<String>) -> Self {
59        self.default_value = Some(default.into());
60        self
61    }
62}
63
64/// Represents a class/struct in any language
65#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
66pub struct ClassEntity {
67    /// Class name
68    pub name: String,
69
70    /// Visibility: "public", "private", "internal"
71    pub visibility: String,
72
73    /// Starting line number (1-indexed)
74    pub line_start: usize,
75
76    /// Ending line number (1-indexed)
77    pub line_end: usize,
78
79    /// Is this an abstract class?
80    pub is_abstract: bool,
81
82    /// Is this an interface/trait definition?
83    pub is_interface: bool,
84
85    /// Base classes (inheritance)
86    pub base_classes: Vec<String>,
87
88    /// Interfaces/traits implemented
89    pub implemented_traits: Vec<String>,
90
91    /// Methods in this class
92    pub methods: Vec<FunctionEntity>,
93
94    /// Fields/attributes
95    pub fields: Vec<Field>,
96
97    /// Documentation/docstring
98    pub doc_comment: Option<String>,
99
100    /// Decorators/attributes
101    pub attributes: Vec<String>,
102
103    /// Generic type parameters (if any)
104    pub type_parameters: Vec<String>,
105}
106
107impl ClassEntity {
108    pub fn new(name: impl Into<String>, line_start: usize, line_end: usize) -> Self {
109        Self {
110            name: name.into(),
111            visibility: "public".to_string(),
112            line_start,
113            line_end,
114            is_abstract: false,
115            is_interface: false,
116            base_classes: Vec::new(),
117            implemented_traits: Vec::new(),
118            methods: Vec::new(),
119            fields: Vec::new(),
120            doc_comment: None,
121            attributes: Vec::new(),
122            type_parameters: Vec::new(),
123        }
124    }
125
126    pub fn with_visibility(mut self, vis: impl Into<String>) -> Self {
127        self.visibility = vis.into();
128        self
129    }
130
131    pub fn abstract_class(mut self) -> Self {
132        self.is_abstract = true;
133        self
134    }
135
136    pub fn interface(mut self) -> Self {
137        self.is_interface = true;
138        self
139    }
140
141    pub fn with_bases(mut self, bases: Vec<String>) -> Self {
142        self.base_classes = bases;
143        self
144    }
145
146    pub fn with_traits(mut self, traits: Vec<String>) -> Self {
147        self.implemented_traits = traits;
148        self
149    }
150
151    pub fn with_methods(mut self, methods: Vec<FunctionEntity>) -> Self {
152        self.methods = methods;
153        self
154    }
155
156    pub fn with_fields(mut self, fields: Vec<Field>) -> Self {
157        self.fields = fields;
158        self
159    }
160
161    pub fn with_doc(mut self, doc: impl Into<String>) -> Self {
162        self.doc_comment = Some(doc.into());
163        self
164    }
165
166    pub fn with_attributes(mut self, attrs: Vec<String>) -> Self {
167        self.attributes = attrs;
168        self
169    }
170
171    pub fn with_type_parameters(mut self, type_params: Vec<String>) -> Self {
172        self.type_parameters = type_params;
173        self
174    }
175}