1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//! Component analysis logic
//!
//! This module implements the analysis phase of component macro processing,
//! determining the component type and extracting relevant metadata for code generation.
use syn::{FnArg, ItemFn, Pat, PatType, ReturnType, Type};
use crate::component::error::{ComponentError, ComponentResult, IntoComponentError};
use crate::component::types::{ComponentInfo, ComponentParameter, ComponentType};
/// Analyzer for component function definitions
///
/// This struct implements the Single Responsibility Principle by focusing
/// solely on analyzing component functions and extracting metadata.
/// It follows the Open/Closed Principle by being easily extensible for
/// new component patterns.
pub struct ComponentAnalyzer;
impl ComponentAnalyzer {
/// Create a new component analyzer
pub fn new() -> Self {
Self
}
/// Analyze a component function and extract metadata
///
/// This method determines the component type and extracts all necessary
/// information for code generation. It delegates to specialized analyzers
/// for more focused analysis based on the component pattern.
///
/// # Arguments
///
/// * `input` - The validated function definition to analyze
///
/// # Returns
///
/// A `ComponentInfo` struct containing all extracted metadata, or a
/// `ComponentError` if analysis fails.
pub fn analyze(&self, input: &ItemFn) -> ComponentResult<ComponentInfo> {
// Use specialized analyzers based on parameter count and type
let param_count = utils::count_parameters(input);
match param_count {
0 => {
// Use specialized no-params analyzer
specialized::NoParamsAnalyzer::analyze(input)
}
1 => {
// Check if it's props-based or single direct parameter
if let Some(FnArg::Typed(PatType { ty, .. })) = input.sig.inputs.first() {
if utils::is_reference_type(ty.as_ref()) {
specialized::PropBasedAnalyzer::analyze(input)
} else {
specialized::DirectParamsAnalyzer::analyze(input)
}
} else {
Err(ComponentError::invalid_parameters(
&input.sig,
"Invalid parameter type",
Some("Component parameters must be typed parameters"),
))
}
}
_ => {
// Multiple parameters - must be direct parameters
specialized::DirectParamsAnalyzer::analyze(input)
}
}
}
/// Determine the type of component based on function signature
fn determine_component_type(&self, input: &ItemFn) -> ComponentResult<ComponentType> {
let param_count = utils::count_parameters(input);
match param_count {
0 => Ok(ComponentType::NoParams),
1 => {
// Could be props-based or single direct parameter
let param = input.sig.inputs.first().unwrap();
if self.is_props_based_parameter(param)? {
self.extract_props_based_info(param)
} else {
self.extract_single_direct_param(param)
}
}
_ => {
// Multiple parameters - must be direct parameters
self.extract_direct_parameters(input)
}
}
}
/// Check if a parameter indicates a props-based component
fn is_props_based_parameter(&self, param: &FnArg) -> ComponentResult<bool> {
match param {
FnArg::Typed(PatType { ty, .. }) => {
// Props-based components have reference parameters
Ok(utils::is_reference_type(ty.as_ref()))
}
FnArg::Receiver(_) => Err(ComponentError::invalid_parameters(
param,
"Component functions cannot have self parameters",
Some("Use standalone functions for components"),
)),
}
}
/// Extract props-based component information
fn extract_props_based_info(&self, param: &FnArg) -> ComponentResult<ComponentType> {
match param {
FnArg::Typed(PatType { pat, ty, .. }) => {
// Extract parameter name using utility
let param_name = utils::extract_identifier(pat.as_ref())
.ok_or_else(|| {
ComponentError::invalid_parameters(
pat,
"Props parameter must be a simple identifier",
Some("Use a simple name like 'props: &MyProps'"),
)
})?
.clone();
// Extract inner type from reference using utility
let props_type = utils::extract_reference_inner(ty.as_ref())
.ok_or_else(|| {
ComponentError::invalid_parameters(
ty,
"Props parameter must be a reference type",
Some("Use &PropsStruct instead of PropsStruct"),
)
})?
.clone();
Ok(ComponentType::PropsBased {
props_type: Box::new(props_type),
props_param_name: param_name,
})
}
_ => unreachable!("Already validated as typed parameter"),
}
}
/// Extract single direct parameter information
fn extract_single_direct_param(&self, param: &FnArg) -> ComponentResult<ComponentType> {
let component_param = self.extract_component_parameter(param)?;
Ok(ComponentType::DirectParams {
parameters: vec![component_param],
})
}
/// Extract direct parameters information
fn extract_direct_parameters(&self, input: &ItemFn) -> ComponentResult<ComponentType> {
let mut parameters = Vec::new();
for param in &input.sig.inputs {
let component_param = self.extract_component_parameter(param)?;
parameters.push(component_param);
}
Ok(ComponentType::DirectParams { parameters })
}
/// Extract a single component parameter
fn extract_component_parameter(&self, param: &FnArg) -> ComponentResult<ComponentParameter> {
match param {
FnArg::Typed(PatType { pat, ty, .. }) => {
// Use utility function to extract identifier
let param_name = utils::extract_identifier(pat.as_ref())
.ok_or_else(|| {
ComponentError::invalid_parameters(
pat,
"Parameter must be a simple identifier",
Some("Use simple parameter names like 'name: String'"),
)
})?
.clone();
Ok(ComponentParameter::new(param_name, ty.as_ref().clone()))
}
FnArg::Receiver(_) => Err(ComponentError::invalid_parameters(
param,
"Component functions cannot have self parameters",
Some("Use standalone functions for components"),
)),
}
}
/// Extract the return type from the function signature
fn extract_return_type(&self, input: &ItemFn) -> ComponentResult<Type> {
match &input.sig.output {
ReturnType::Type(_, return_type) => {
// Use IntoComponentError trait for potential parsing errors
self.validate_return_type_syntax(return_type.as_ref())
.into_component_error()?;
Ok(return_type.as_ref().clone())
}
ReturnType::Default => Err(ComponentError::invalid_return_type(
&input.sig,
"Component function must have an explicit return type",
"Element, VNode",
)),
}
}
/// Validate return type syntax (demonstrates IntoComponentError usage)
fn validate_return_type_syntax(&self, return_type: &Type) -> syn::Result<()> {
// This is a demonstration of where IntoComponentError would be useful
// In a real scenario, this might involve complex type parsing
match return_type {
Type::Path(_) => Ok(()),
Type::Reference(_) => Ok(()),
Type::Tuple(_) => Ok(()),
_ => {
// Create a syn error that can be converted using IntoComponentError
Err(syn::Error::new_spanned(
return_type,
"Unsupported return type syntax for component function",
))
}
}
}
}
impl Default for ComponentAnalyzer {
fn default() -> Self {
Self::new()
}
}
/// Specialized analyzers for different component patterns
pub mod specialized {
use super::*;
/// Analyzer specifically for props-based components
pub struct PropBasedAnalyzer;
impl PropBasedAnalyzer {
/// Analyze a props-based component
pub fn analyze(input: &ItemFn) -> ComponentResult<ComponentInfo> {
let analyzer = ComponentAnalyzer::new();
// Use the main analyzer's helper methods
let component_type = analyzer.determine_component_type(input)?;
let return_type = analyzer.extract_return_type(input)?;
// Verify it's actually props-based
match &component_type {
ComponentType::PropsBased { .. } => {
// Create component info
let component_info = ComponentInfo::new(
component_type,
input.sig.ident.clone(),
input.vis.clone(),
input.block.as_ref().clone(),
input.sig.generics.clone(),
return_type,
);
Ok(component_info)
}
_ => Err(ComponentError::internal_error(
"Expected props-based component but got different type",
"PropBasedAnalyzer::analyze",
)),
}
}
}
/// Analyzer specifically for direct parameter components
pub struct DirectParamsAnalyzer;
impl DirectParamsAnalyzer {
/// Analyze a direct parameter component
pub fn analyze(input: &ItemFn) -> ComponentResult<ComponentInfo> {
let analyzer = ComponentAnalyzer::new();
// Use the main analyzer's helper methods
let component_type = analyzer.determine_component_type(input)?;
let return_type = analyzer.extract_return_type(input)?;
// Verify it's actually direct parameters
match &component_type {
ComponentType::DirectParams { .. } => {
// Create component info
let component_info = ComponentInfo::new(
component_type,
input.sig.ident.clone(),
input.vis.clone(),
input.block.as_ref().clone(),
input.sig.generics.clone(),
return_type,
);
Ok(component_info)
}
_ => Err(ComponentError::internal_error(
"Expected direct parameters component but got different type",
"DirectParamsAnalyzer::analyze",
)),
}
}
}
/// Analyzer specifically for no-parameter components
pub struct NoParamsAnalyzer;
impl NoParamsAnalyzer {
/// Analyze a no-parameter component
pub fn analyze(input: &ItemFn) -> ComponentResult<ComponentInfo> {
let analyzer = ComponentAnalyzer::new();
// Use the main analyzer's helper methods
let component_type = analyzer.determine_component_type(input)?;
let return_type = analyzer.extract_return_type(input)?;
// Verify it's actually no parameters
match &component_type {
ComponentType::NoParams => {
// Create component info
let component_info = ComponentInfo::new(
component_type,
input.sig.ident.clone(),
input.vis.clone(),
input.block.as_ref().clone(),
input.sig.generics.clone(),
return_type,
);
Ok(component_info)
}
_ => Err(ComponentError::internal_error(
"Expected no-parameter component but got different type",
"NoParamsAnalyzer::analyze",
)),
}
}
}
}
/// Utility functions for component analysis
pub mod utils {
use super::*;
/// Check if a type is a reference type
pub fn is_reference_type(ty: &Type) -> bool {
matches!(ty, Type::Reference(_))
}
/// Extract the inner type from a reference type
pub fn extract_reference_inner(ty: &Type) -> Option<&Type> {
match ty {
Type::Reference(type_ref) => Some(type_ref.elem.as_ref()),
_ => None,
}
}
/// Check if a pattern is a simple identifier
pub fn is_simple_identifier(pat: &Pat) -> bool {
matches!(pat, Pat::Ident(_))
}
/// Extract identifier from a simple pattern
pub fn extract_identifier(pat: &Pat) -> Option<&syn::Ident> {
match pat {
Pat::Ident(pat_ident) => Some(&pat_ident.ident),
_ => None,
}
}
/// Count the number of parameters in a function
pub fn count_parameters(input: &ItemFn) -> usize {
input.sig.inputs.len()
}
/// Check if a function has generics
pub fn has_generics(input: &ItemFn) -> bool {
!input.sig.generics.params.is_empty()
}
}