component_model 0.17.0

Revolutionary type-safe component assignment for Rust. Build complex objects with zero boilerplate using derive macros and type-driven field setting. Perfect for configuration builders, fluent APIs, and object composition patterns.
Documentation
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# Task 003: Validation Framework

## ๐ŸŽฏ **Objective**

Implement a comprehensive validation framework that allows field-level validation during component assignment, providing clear error messages and validation composition.

## ๐Ÿ“‹ **Current State**

No built-in validation exists - users must implement validation manually:
```rust
impl Config
{
  fn set_port( &mut self, port : u16 )
  {
    if port < 1024
    {
      panic!( "Port must be >= 1024" );
    }
    self.port = port;
  }
}
```

## ๐ŸŽฏ **Target State**

Declarative validation with clear error reporting:
```rust
#[derive(ComponentModel)]
struct Config
{
  #[ component( validate = "is_valid_host" ) ]
  host : String,
  
  #[ component( validate = "is_port_range(1024, 65535)" ) ]
  port : u16,
  
  #[ component( validate = "not_empty" ) ]
  database_name : String,
}

// Usage with validation
let result = Config::default()
  .try_assign( "" ) // Fails validation
  .and_then( | c | c.try_assign( 80u16 ) ) // Fails validation
  .and_then( | c | c.try_assign( "" ) );   // Fails validation

match result
{
  Ok( config ) => println!( "Valid config: {:?}", config ),
  Err( errors ) =>
  {
    for error in errors
    {
      eprintln!( "Validation error: {}", error );
    }
  }
}
```

## ๐Ÿ“ **Detailed Requirements**

### **Core Validation API**

#### **Result-Based Assignment**
```rust
pub trait TryAssign< T, IntoT >
{
  type Error;
  
  fn try_assign( &mut self, component : IntoT ) -> Result< (), Self::Error >;
  fn try_impute( self, component : IntoT ) -> Result< Self, Self::Error >
  where
    Self : Sized;
}
```

#### **Error Types**
```rust
#[ derive( Debug, Clone ) ]
pub struct ValidationError
{
  pub field_name : String,
  pub field_type : String,
  pub provided_value : String,
  pub error_message : String,
  pub suggestion : Option< String >,
}

#[ derive( Debug, Clone ) ]
pub struct ValidationErrors
{
  pub errors : Vec< ValidationError >,
}

impl std::fmt::Display for ValidationErrors
{
  fn fmt( &self, f : &mut std::fmt::Formatter ) -> std::fmt::Result
  {
    for ( i, error ) in self.errors.iter().enumerate()
    {
      if i > 0 { writeln!( f )?; }
      write!( f, "Field '{}': {}", error.field_name, error.error_message )?;
      if let Some( suggestion ) = &error.suggestion
      {
        write!( f, " (try: {})", suggestion )?;
      }
    }
    Ok( () )
  }
}
```

### **Built-in Validators**

#### **String Validators**
```rust
pub fn not_empty( value : &str ) -> Result< (), String >
{
  if value.is_empty()
  {
    Err( "cannot be empty".to_string() )
  }
  else
  {
    Ok( () )
  }
}

pub fn min_length( min : usize ) -> impl Fn( &str ) -> Result< (), String >
{
  move | value |
  {
    if value.len() < min
    {
      Err( format!( "must be at least {} characters", min ) )
    }
    else
    {
      Ok( () )
    }
  }
}

pub fn max_length( max : usize ) -> impl Fn( &str ) -> Result< (), String >
{
  move | value |
  {
    if value.len() > max
    {
      Err( format!( "must be at most {} characters", max ) )
    }
    else
    {
      Ok( () )
    }
  }
}

pub fn matches_regex( pattern : &str ) -> impl Fn( &str ) -> Result< (), String >
{
  let regex = Regex::new( pattern ).expect( "Invalid regex pattern" );
  move | value |
  {
    if regex.is_match( value )
    {
      Ok( () )
    }
    else
    {
      Err( format!( "must match pattern: {}", pattern ) )
    }
  }
}
```

#### **Numeric Validators**
```rust
pub fn min_value< T : PartialOrd + std::fmt::Display >( min : T ) -> impl Fn( &T ) -> Result< (), String >
{
  move | value |
  {
    if value < &min
    {
      Err( format!( "must be at least {}", min ) )
    }
    else
    {
      Ok( () )
    }
  }
}

pub fn max_value< T : PartialOrd + std::fmt::Display >( max : T ) -> impl Fn( &T ) -> Result< (), String >
{
  move | value |
  {
    if value > &max
    {
      Err( format!( "must be at most {}", max ) )
    }
    else
    {
      Ok( () )
    }
  }
}

pub fn range< T : PartialOrd + std::fmt::Display >( min : T, max : T ) -> impl Fn( &T ) -> Result< (), String >
{
  move | value |
  {
    if value < &min || value > &max
    {
      Err( format!( "must be between {} and {}", min, max ) )
    }
    else
    {
      Ok( () )
    }
  }
}
```

### **Attribute Syntax**

#### **Function Reference**
```rust
#[derive(ComponentModel)]
struct Config
{
  #[ component( validate = "not_empty" ) ]
  name : String,
}

fn not_empty( value : &str ) -> Result< (), String >
{
  // validation logic
}
```

#### **Closure Syntax**
```rust
#[derive(ComponentModel)]
struct Config
{
  #[ component( validate = "|v| if v.len() > 0 { Ok(()) } else { Err(\"empty\".to_string()) }" ) ]
  name : String,
}
```

#### **Multiple Validators**
```rust
#[derive(ComponentModel)]
struct Config
{
  #[ component( validate = [ "not_empty", "min_length(3)", "max_length(50)" ] ) ]
  username : String,
}
```

### **Generated Implementation**

The derive macro generates:
```rust
impl TryAssign< String, &str > for Config
{
  type Error = ValidationErrors;
  
  fn try_assign( &mut self, component : &str ) -> Result< (), Self::Error >
  {
    let mut errors = Vec::new();
    
    // Run validation
    if let Err( msg ) = not_empty( component )
    {
      errors.push
      (
        ValidationError
        {
          field_name : "name".to_string(),
          field_type : "String".to_string(),
          provided_value : component.to_string(),
          error_message : msg,
          suggestion : Some( "provide a non-empty string".to_string() ),
        }
      );
    }
    
    if !errors.is_empty()
    {
      return Err( ValidationErrors { errors } );
    }
    
    // If validation passes, assign
    self.name = component.to_string();
    Ok( () )
  }
}
```

## ๐Ÿ—‚๏ธ **File Changes**

### **New Files**
- `component_model_types/src/validation/mod.rs` - Core validation types
- `component_model_types/src/validation/validators.rs` - Built-in validators
- `component_model_types/src/validation/error.rs` - Error types
- `component_model_meta/src/validation.rs` - Validation macro logic
- `examples/validation_example.rs` - Comprehensive example

### **Modified Files**
- `component_model_types/src/lib.rs` - Export validation module
- `component_model_meta/src/lib.rs` - Add validation to derives
- `component_model/src/lib.rs` - Re-export validation types

## โšก **Implementation Steps**

### **Phase 1: Core Framework (Week 1)**
1. Define `TryAssign` trait and error types
2. Implement basic string validators (`not_empty`, `min_length`, etc.)
3. Create validation attribute parsing in derive macro
4. Generate basic validation code

### **Phase 2: Advanced Validators (Week 2)**
1. Add numeric validators (`min_value`, `max_value`, `range`)
2. Implement custom validator support
3. Add validator composition (multiple validators per field)
4. Error message improvement and suggestions

### **Phase 3: Integration & Polish (Week 2-3)**
1. Integration with existing `Assign` trait (fallback behavior)
2. Performance optimization for validation chains  
3. Comprehensive documentation and examples
4. Error message localization support

## ๐Ÿงช **Testing Strategy**

### **Unit Tests**
```rust
#[ cfg( test ) ]
mod tests
{
  use super::*;
  
  #[ test ]
  fn test_validation_success()
  {
    #[ derive( ComponentModel ) ]
    struct Config
    {
      #[ component( validate = "not_empty" ) ]
      name : String,
    }
    
    let mut config = Config::default();
    assert!( config.try_assign( "test" ).is_ok() );
    assert_eq!( config.name, "test" );
  }
  
  #[ test ]
  fn test_validation_failure()
  {
    #[ derive( ComponentModel ) ]
    struct Config
    {
      #[ component( validate = "not_empty" ) ]
      name : String,
    }
    
    let mut config = Config::default();
    let result = config.try_assign( "" );
    
    assert!( result.is_err() );
    let errors = result.unwrap_err();
    assert_eq!( errors.errors.len(), 1 );
    assert_eq!( errors.errors[ 0 ].field_name, "name" );
  }
  
  #[ test ]
  fn test_multiple_validators()
  {
    #[ derive( ComponentModel ) ]
    struct Config
    {
      #[ component( validate = [ "not_empty", "min_length(3)" ] ) ]
      username : String,
    }
    
    let mut config = Config::default();
    
    // Should fail both validations
    let result = config.try_assign( "" );
    assert!( result.is_err() );
    
    // Should fail min_length
    let result = config.try_assign( "ab" );
    assert!( result.is_err() );
    
    // Should succeed
    let result = config.try_assign( "abc" );
    assert!( result.is_ok() );
  }
}
```

### **Integration Tests**
```rust
#[ test ]
fn test_real_world_validation()
{
  #[ derive( ComponentModel ) ]
  struct ServerConfig
  {
    #[ component( validate = "not_empty" ) ]
    host : String,
    
    #[ component( validate = "range(1024, 65535)" ) ]
    port : u16,
    
    #[ component( validate = "min_value(1)" ) ]
    worker_count : usize,
  }
  
  // Test valid configuration
  let config = ServerConfig::default()
    .try_impute( "localhost" )
    .and_then( | c | c.try_impute( 8080u16 ) )
    .and_then( | c | c.try_impute( 4usize ) );
    
  assert!( config.is_ok() );
  
  // Test invalid configuration
  let result = ServerConfig::default()
    .try_impute( "" )  // Empty host
    .and_then( | c | c.try_impute( 80u16 ) )  // Invalid port
    .and_then( | c | c.try_impute( 0usize ) ); // Invalid worker count
    
  assert!( result.is_err() );
  let errors = result.unwrap_err();
  assert_eq!( errors.errors.len(), 3 );
}
```

## ๐Ÿ“Š **Success Metrics**

- [ ] Support for 10+ built-in validators
- [ ] Clear, actionable error messages
- [ ] Zero performance overhead when validation disabled
- [ ] Composable validation (multiple validators per field)
- [ ] Integration with existing assignment patterns

## ๐Ÿšง **Potential Challenges**

1. **Performance Impact**: Validation adds overhead
   - **Solution**: Compile-time optimization and benchmarking

2. **Error Message Quality**: Generic errors aren't helpful
   - **Solution**: Context-aware error generation with suggestions

3. **Validator Composition**: Complex attribute parsing
   - **Solution**: Robust parser with clear error messages

## ๐Ÿ”„ **Dependencies**

- **Requires**: Task 001 (Single Derive Macro) for attribute parsing
- **Blocks**: None
- **Related**: Task 002 benefits from validation for type conversion

## ๐Ÿ“… **Timeline**

- **Week 1**: Core validation framework and basic validators
- **Week 2**: Advanced validators and composition
- **Week 3**: Integration, optimization, and documentation

## ๐Ÿ’ก **Future Enhancements**

- **Async Validation**: For database uniqueness checks, etc.
- **Custom Error Types**: Allow users to define their own error types
- **Conditional Validation**: Validators that depend on other field values
- **Validation Groups**: Different validation rules for different contexts