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
380
381
//! Validation logic for component functions
//!
//! This module implements comprehensive validation for component function definitions,
//! ensuring they meet the requirements for code generation and providing helpful
//! error messages when they don't.
use syn::{FnArg, ItemFn, Pat, PatType, ReturnType};
use crate::component::error::{ComponentError, ComponentResult, messages};
use crate::component::types::ValidationConfig;
/// Validator for component function definitions
///
/// This struct implements the Single Responsibility Principle by focusing
/// solely on validation logic. It can be easily extended with new validation
/// rules without modifying existing code.
pub struct ComponentValidator {
config: ValidationConfig,
}
impl ComponentValidator {
/// Create a new validator with default configuration
pub fn new() -> Self {
Self {
config: ValidationConfig::default(),
}
}
/// Create a new validator with custom configuration
pub fn with_config(config: ValidationConfig) -> Self {
Self { config }
}
/// Validate a component function definition
///
/// This method performs comprehensive validation including:
/// - Function signature validation
/// - Return type validation
/// - Parameter validation
/// - Generics validation
///
/// # Arguments
///
/// * `input` - The function definition to validate
///
/// # Returns
///
/// `Ok(())` if validation passes, or a `ComponentError` with detailed
/// information about what went wrong and how to fix it.
pub fn validate(&self, input: &ItemFn) -> ComponentResult<()> {
// Validate function signature
self.validate_signature(input)?;
// Validate return type
self.validate_return_type(input)?;
// Validate parameters
self.validate_parameters(input)?;
// Validate generics (if present)
self.validate_generics(input)?;
// Perform specialized validation based on component type
self.validate_component_type_specific(input)?;
Ok(())
}
/// Perform component-type-specific validation
fn validate_component_type_specific(&self, input: &ItemFn) -> ComponentResult<()> {
// Use utility function to count parameters
let param_count = crate::component::analysis::utils::count_parameters(input);
match param_count {
0 => {
// Use specialized no-params validator
specialized::NoParamsValidator::validate(input)
}
1 => {
// Check if it's props-based or single direct parameter
if let Some(FnArg::Typed(PatType { ty, .. })) = input.sig.inputs.first() {
// Use utility function to check reference type
if crate::component::analysis::utils::is_reference_type(ty.as_ref()) {
specialized::PropBasedValidator::validate(input)
} else {
specialized::DirectParamsValidator::validate(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::DirectParamsValidator::validate(input)
}
}
}
/// Validate the function signature
fn validate_signature(&self, input: &ItemFn) -> ComponentResult<()> {
// Check for async functions
if input.sig.asyncness.is_some() && !self.config.allow_async {
return Err(ComponentError::invalid_signature(
&input.sig,
messages::signature::ASYNC_NOT_SUPPORTED,
Some("Remove the 'async' keyword from the component function"),
));
}
// Check for unsafe functions
if input.sig.unsafety.is_some() && !self.config.allow_unsafe {
return Err(ComponentError::invalid_signature(
&input.sig,
messages::signature::UNSAFE_NOT_SUPPORTED,
Some("Remove the 'unsafe' keyword from the component function"),
));
}
// Check for explicit return type if required
if self.config.require_explicit_return && matches!(input.sig.output, ReturnType::Default) {
return Err(ComponentError::invalid_return_type(
&input.sig,
messages::signature::MISSING_RETURN_TYPE,
"Element, VNode",
));
}
Ok(())
}
/// Validate the return type
fn validate_return_type(&self, input: &ItemFn) -> ComponentResult<()> {
match &input.sig.output {
ReturnType::Type(_, return_type) => {
// For now, we accept any explicit return type
// In the future, we could add more specific validation
// to ensure it's Element, VNode, etc.
self.validate_return_type_compatibility(return_type)?;
}
ReturnType::Default => {
if self.config.require_explicit_return {
return Err(ComponentError::invalid_return_type(
&input.sig,
messages::signature::MISSING_RETURN_TYPE,
messages::signature::INVALID_RETURN_TYPE,
));
}
}
}
Ok(())
}
/// Validate return type compatibility
fn validate_return_type_compatibility(&self, _return_type: &syn::Type) -> ComponentResult<()> {
// TODO: Add specific validation for known return types
// For now, we accept any type and let the compiler catch issues
Ok(())
}
/// Validate function parameters
fn validate_parameters(&self, input: &ItemFn) -> ComponentResult<()> {
// Use utility function to count parameters
let param_count = crate::component::analysis::utils::count_parameters(input);
// Check parameter count for direct parameter components
if param_count > 1 && param_count > self.config.max_direct_params {
return Err(ComponentError::invalid_parameters(
&input.sig,
format!(
"Too many parameters ({}). Maximum allowed: {}",
param_count, self.config.max_direct_params
),
Some("Consider using a props struct for components with many parameters"),
));
}
// Validate each parameter
for param in &input.sig.inputs {
self.validate_parameter(param)?;
}
Ok(())
}
/// Validate a single parameter
fn validate_parameter(&self, param: &FnArg) -> ComponentResult<()> {
match param {
FnArg::Receiver(_) => Err(ComponentError::invalid_parameters(
param,
messages::parameters::SELF_PARAMETER,
Some("Component functions should be standalone functions, not methods"),
)),
FnArg::Typed(typed_param) => {
// Validate parameter pattern
self.validate_parameter_pattern(&typed_param.pat)?;
// Validate parameter type
self.validate_parameter_type(&typed_param.ty)?;
Ok(())
}
}
}
/// Validate parameter pattern (must be simple identifier)
fn validate_parameter_pattern(&self, pattern: &Pat) -> ComponentResult<()> {
match pattern {
Pat::Ident(_) => Ok(()),
_ => Err(ComponentError::invalid_parameters(
pattern,
messages::parameters::INVALID_PATTERN,
Some("Use simple parameter names like 'name: String' instead of destructuring"),
)),
}
}
/// Validate parameter type
fn validate_parameter_type(&self, _param_type: &syn::Type) -> ComponentResult<()> {
// TODO: Add specific validation for parameter types
// For now, we accept any type and let the compiler catch issues
Ok(())
}
/// Validate generics usage
fn validate_generics(&self, input: &ItemFn) -> ComponentResult<()> {
// Use utility function to check if component has generics
if !crate::component::analysis::utils::has_generics(input) {
return Ok(()); // No generics to validate
}
let generics = &input.sig.generics;
// Check for complex generic bounds
for param in &generics.params {
match param {
syn::GenericParam::Type(type_param) => {
if !type_param.bounds.is_empty() {
// For now, we allow bounded type parameters but warn about complexity
// In the future, we might want to be more restrictive
}
}
syn::GenericParam::Lifetime(lifetime_param) => {
// Lifetime parameters are allowed but have limited support
// Warn about potential limitations
if lifetime_param.bounds.len() > 2 {
return Err(ComponentError::unsupported_generics(
lifetime_param,
"Complex lifetime bounds may not be fully supported",
messages::generics::LIFETIME_PARAMETERS,
));
}
}
syn::GenericParam::Const(_) => {
// Const generics are allowed
}
}
}
// Check where clause complexity
if let Some(where_clause) = &generics.where_clause
&& where_clause.predicates.len() > 5
{
return Err(ComponentError::unsupported_generics(
where_clause,
"Complex where clauses may not be fully supported",
messages::generics::COMPLEX_BOUNDS,
));
}
Ok(())
}
}
impl Default for ComponentValidator {
fn default() -> Self {
Self::new()
}
}
/// Specialized validators for different component types
pub mod specialized {
use super::*;
/// Validator specifically for props-based components
pub struct PropBasedValidator;
impl PropBasedValidator {
/// Validate a props-based component
pub fn validate(input: &ItemFn) -> ComponentResult<()> {
// Additional validation specific to props-based components
if input.sig.inputs.len() != 1 {
return Err(ComponentError::invalid_parameters(
&input.sig,
"Props-based components must have exactly one parameter",
Some(messages::parameters::REFERENCE_PARAMETER_SUGGESTION),
));
}
// Validate that the parameter is a reference using utility function
if let Some(FnArg::Typed(PatType { ty, .. })) = input.sig.inputs.first()
&& !crate::component::analysis::utils::is_reference_type(ty.as_ref())
{
return Err(ComponentError::invalid_parameters(
ty,
"Props parameter must be a reference type",
Some("Use &PropsStruct instead of PropsStruct"),
));
}
Ok(())
}
}
/// Validator specifically for direct parameter components
pub struct DirectParamsValidator;
impl DirectParamsValidator {
/// Validate a direct parameter component
pub fn validate(input: &ItemFn) -> ComponentResult<()> {
// Additional validation specific to direct parameter components
if input.sig.inputs.is_empty() {
return Err(ComponentError::invalid_parameters(
&input.sig,
"Direct parameter components must have at least one parameter",
Some(messages::parameters::DIRECT_PARAMETER_SUGGESTION),
));
}
// Validate that all parameters are simple typed parameters
for param in &input.sig.inputs {
match param {
FnArg::Receiver(_) => {
return Err(ComponentError::invalid_parameters(
param,
messages::parameters::SELF_PARAMETER,
Some("Component functions should be standalone functions, not methods"),
));
}
FnArg::Typed(PatType { pat, .. }) => {
// Use utility function to check if pattern is simple identifier
if !crate::component::analysis::utils::is_simple_identifier(pat.as_ref()) {
return Err(ComponentError::invalid_parameters(
pat,
messages::parameters::INVALID_PATTERN,
Some(
"Use simple parameter names like 'name: String' instead of destructuring",
),
));
}
}
}
}
Ok(())
}
}
/// Validator specifically for no-parameter components
pub struct NoParamsValidator;
impl NoParamsValidator {
/// Validate a no-parameter component
pub fn validate(input: &ItemFn) -> ComponentResult<()> {
// Additional validation specific to no-parameter components
if !input.sig.inputs.is_empty() {
return Err(ComponentError::invalid_parameters(
&input.sig,
"No-parameter components must not have any parameters",
Some("Remove all parameters or use a different component pattern"),
));
}
Ok(())
}
}
}