codegraph_parser_api/entities/
function.rs

1use serde::{Deserialize, Serialize};
2
3/// Represents a function parameter
4#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
5pub struct Parameter {
6    /// Parameter name
7    pub name: String,
8
9    /// Type annotation (if available)
10    pub type_annotation: Option<String>,
11
12    /// Default value (if any)
13    pub default_value: Option<String>,
14
15    /// Is this a variadic parameter? (e.g., *args, **kwargs)
16    pub is_variadic: bool,
17}
18
19impl Parameter {
20    pub fn new(name: impl Into<String>) -> Self {
21        Self {
22            name: name.into(),
23            type_annotation: None,
24            default_value: None,
25            is_variadic: false,
26        }
27    }
28
29    pub fn with_type(mut self, type_ann: impl Into<String>) -> Self {
30        self.type_annotation = Some(type_ann.into());
31        self
32    }
33
34    pub fn with_default(mut self, default: impl Into<String>) -> Self {
35        self.default_value = Some(default.into());
36        self
37    }
38
39    pub fn variadic(mut self) -> Self {
40        self.is_variadic = true;
41        self
42    }
43}
44
45/// Represents a function/method in any language
46#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
47pub struct FunctionEntity {
48    /// Function name
49    pub name: String,
50
51    /// Full signature (including parameters and return type)
52    pub signature: String,
53
54    /// Visibility: "public", "private", "protected", "internal"
55    pub visibility: String,
56
57    /// Starting line number (1-indexed)
58    pub line_start: usize,
59
60    /// Ending line number (1-indexed)
61    pub line_end: usize,
62
63    /// Is this an async/coroutine function?
64    pub is_async: bool,
65
66    /// Is this a test function?
67    pub is_test: bool,
68
69    /// Is this a static method?
70    pub is_static: bool,
71
72    /// Is this an abstract method?
73    pub is_abstract: bool,
74
75    /// Function parameters
76    pub parameters: Vec<Parameter>,
77
78    /// Return type annotation (if available)
79    pub return_type: Option<String>,
80
81    /// Documentation/docstring
82    pub doc_comment: Option<String>,
83
84    /// Decorators/attributes (e.g., [@property], [@deprecated])
85    pub attributes: Vec<String>,
86
87    /// Parent class (if this is a method)
88    pub parent_class: Option<String>,
89}
90
91impl FunctionEntity {
92    pub fn new(name: impl Into<String>, line_start: usize, line_end: usize) -> Self {
93        let name = name.into();
94        Self {
95            signature: name.clone(),
96            name,
97            visibility: "public".to_string(),
98            line_start,
99            line_end,
100            is_async: false,
101            is_test: false,
102            is_static: false,
103            is_abstract: false,
104            parameters: Vec::new(),
105            return_type: None,
106            doc_comment: None,
107            attributes: Vec::new(),
108            parent_class: None,
109        }
110    }
111
112    // Builder methods
113    pub fn with_signature(mut self, sig: impl Into<String>) -> Self {
114        self.signature = sig.into();
115        self
116    }
117
118    pub fn with_visibility(mut self, vis: impl Into<String>) -> Self {
119        self.visibility = vis.into();
120        self
121    }
122
123    pub fn async_fn(mut self) -> Self {
124        self.is_async = true;
125        self
126    }
127
128    pub fn test_fn(mut self) -> Self {
129        self.is_test = true;
130        self
131    }
132
133    pub fn static_fn(mut self) -> Self {
134        self.is_static = true;
135        self
136    }
137
138    pub fn abstract_fn(mut self) -> Self {
139        self.is_abstract = true;
140        self
141    }
142
143    pub fn with_parameters(mut self, params: Vec<Parameter>) -> Self {
144        self.parameters = params;
145        self
146    }
147
148    pub fn with_return_type(mut self, ret: impl Into<String>) -> Self {
149        self.return_type = Some(ret.into());
150        self
151    }
152
153    pub fn with_doc(mut self, doc: impl Into<String>) -> Self {
154        self.doc_comment = Some(doc.into());
155        self
156    }
157
158    pub fn with_attributes(mut self, attrs: Vec<String>) -> Self {
159        self.attributes = attrs;
160        self
161    }
162
163    pub fn with_parent_class(mut self, parent: impl Into<String>) -> Self {
164        self.parent_class = Some(parent.into());
165        self
166    }
167}