component_model_meta 0.17.0

A flexible implementation of the Builder pattern supporting nested builders and collection-specific subcomponent_models. Implementation of its derive macro. Should not be used independently, instead use module::component_model which relies on the module.
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//! Manual testing file for all documented examples.
//!
//! This file tests each example from src/lib.rs documentation to verify they work correctly.
//! It includes comprehensive corner case testing for all derive macros.

#![ allow( missing_docs ) ]

use component_model_meta::*;
use component_model_types::*;

// ============================================================================
// Test 1: ComponentFrom - Person Example (from line 65)
// ============================================================================

#[cfg(test)]
mod test_component_from_person
{
  use super::*;

  #[test]
  fn documented_example_works()
  {
    #[ derive( ComponentFrom ) ]
    struct Person
    {
      pub age: i32,
      pub name: String,
    }

    let my_struct = Person { age: 10, name: "Hello".into() };
    let age: i32 = From::from( &my_struct );
    let name: String = From::from( &my_struct );

    assert_eq!( age, 10 );
    assert_eq!( name, "Hello" );
  }

  #[test]
  fn single_field_struct()
  {
    #[ derive( ComponentFrom ) ]
    struct OnlyAge
    {
      pub age: i32,
    }

    let obj = OnlyAge { age: 42 };
    let age: i32 = From::from( &obj );
    assert_eq!( age, 42 );
  }

  #[test]
  fn three_fields_different_types()
  {
    #[ derive( ComponentFrom ) ]
    struct TripleData
    {
      pub count: i32,
      pub label: String,
      pub ratio: f64,
    }

    let obj = TripleData
    {
      count: 100,
      label: "test".into(),
      ratio: 3.14,
    };

    let count: i32 = From::from( &obj );
    let label: String = From::from( &obj );
    let ratio: f64 = From::from( &obj );

    assert_eq!( count, 100 );
    assert_eq!( label, "test" );
    assert_eq!( ratio, 3.14 );
  }

  #[test]
  fn tuple_struct_single_field()
  {
    #[ derive( ComponentFrom ) ]
    struct Wrapper( pub i32 );

    let obj = Wrapper( 777 );
    let val: i32 = From::from( &obj );
    assert_eq!( val, 777 );
  }

  #[test]
  fn tuple_struct_multiple_fields()
  {
    #[ derive( ComponentFrom ) ]
    struct Pair( pub i32, pub String );

    let obj = Pair( 123, "data".into() );
    let num: i32 = From::from( &obj );
    let text: String = From::from( &obj );

    assert_eq!( num, 123 );
    assert_eq!( text, "data" );
  }
}

// ============================================================================
// Test 2: ComponentAssign - Person Example (from line 120)
// ============================================================================

#[cfg(test)]
mod test_component_assign_person
{
  use super::*;

  #[test]
  fn documented_example_works()
  {
    #[ derive( Default, PartialEq, Debug, Assign ) ]
    struct Person
    {
      age: i32,
      name: String,
    }

    let mut person: Person = Default::default();
    person.assign( 13 );
    person.assign( "John" );
    assert_eq!( person, Person { age: 13, name: "John".to_string() } );
  }

  #[test]
  fn single_field_assignment()
  {
    #[ derive( Default, PartialEq, Debug, Assign ) ]
    struct Counter
    {
      count: i32,
    }

    let mut counter = Counter::default();
    counter.assign( 999 );
    assert_eq!( counter.count, 999 );
  }

  // ISSUE FOUND: Type ambiguity with Assign when assigning literal numbers
  // The compiler cannot determine which field to assign when multiple numeric fields exist
  // ERROR: type annotations needed at line "config.assign( 30 );"
  // This appears to be a limitation of the Assign trait inference
  // TODO: Add bug reproducer test for this issue
  /*
  #[test]
  fn three_field_assignment()
  {
    #[ derive( Default, PartialEq, Debug, Assign ) ]
    struct Config
    {
      timeout: i32,
      host: String,
      ratio: f64,
    }

    let mut config = Config::default();
    config.assign( 30 ); // ERROR: type annotations needed
    config.assign( "localhost" );
    config.assign( 0.5 );

    assert_eq!( config.timeout, 30 );
    assert_eq!( config.host, "localhost" );
    assert_eq!( config.ratio, 0.5 );
  }
  */

  #[test]
  fn into_conversion_on_assign()
  {
    #[ derive( Default, PartialEq, Debug, Assign ) ]
    struct Data
    {
      label: String,
    }

    let mut data = Data::default();
    data.assign( "test" ); // &str converts Into<String>
    assert_eq!( data.label, "test" );
  }

  #[test]
  fn tuple_struct_assignment()
  {
    #[ derive( Default, PartialEq, Debug, Assign ) ]
    struct Pair( i32, String );

    let mut pair = Pair::default();
    pair.assign( 42 );
    pair.assign( "answer" );

    assert_eq!( pair.0, 42 );
    assert_eq!( pair.1, "answer" );
  }
}

// ============================================================================
// Test 3: FromComponents - Options1/Options2 Example (from line 466)
// ============================================================================

#[cfg(test)]
mod test_from_components_options
{
  use super::*;

  #[test]
  fn documented_example_works()
  {
    #[ derive( Debug, Default, PartialEq ) ]
    pub struct Options1
    {
      field1: i32,
      field2: String,
      field3: f32,
    }

    impl From< &Options1 > for i32
    {
      #[ inline( always ) ]
      fn from( src: &Options1 ) -> Self
      {
        src.field1.clone()
      }
    }

    impl From< &Options1 > for String
    {
      #[ inline( always ) ]
      fn from( src: &Options1 ) -> Self
      {
        src.field2.clone()
      }
    }

    impl From< &Options1 > for f32
    {
      #[ inline( always ) ]
      fn from( src: &Options1 ) -> Self
      {
        src.field3.clone()
      }
    }

    #[ derive( Debug, Default, PartialEq, FromComponents ) ]
    pub struct Options2
    {
      field1: i32,
      field2: String,
    }

    let o1 = Options1 { field1: 42, field2: "Hello, world!".to_string(), field3: 13.01 };

    // Test Into::into()
    let o2: Options2 = Into::< Options2 >::into( &o1 );
    let expected = Options2 { field1: 42, field2: "Hello, world!".to_string() };
    assert_eq!( o2, expected );

    // Test .into()
    let o2: Options2 = ( &o1 ).into();
    assert_eq!( o2, expected );

    // Test Type::from()
    let o2 = Options2::from( &o1 );
    assert_eq!( o2, expected );
  }

  #[test]
  fn single_field_conversion()
  {
    #[ derive( Debug, Default, PartialEq ) ]
    struct Source
    {
      value: i32,
      extra: String,
    }

    impl From< &Source > for i32
    {
      fn from( src: &Source ) -> Self
      {
        src.value
      }
    }

    #[ derive( Debug, Default, PartialEq, FromComponents ) ]
    struct Target
    {
      value: i32,
    }

    let source = Source { value: 100, extra: "ignored".into() };
    let target: Target = ( &source ).into();
    assert_eq!( target.value, 100 );
  }

  #[test]
  fn equal_field_count_conversion()
  {
    #[ derive( Debug, Default, PartialEq ) ]
    struct SourceData
    {
      id: i32,
      name: String,
    }

    impl From< &SourceData > for i32
    {
      fn from( src: &SourceData ) -> Self
      {
        src.id
      }
    }

    impl From< &SourceData > for String
    {
      fn from( src: &SourceData ) -> Self
      {
        src.name.clone()
      }
    }

    #[ derive( Debug, Default, PartialEq, FromComponents ) ]
    struct TargetData
    {
      id: i32,
      name: String,
    }

    let source = SourceData { id: 999, name: "test".into() };
    let target: TargetData = ( &source ).into();

    assert_eq!( target.id, 999 );
    assert_eq!( target.name, "test" );
  }
}

// ============================================================================
// Test 4: ComponentModel - Config Example (from line 567)
// ============================================================================

#[cfg(test)]
mod test_component_model_config
{
  use super::*;

  #[test]
  fn documented_example_works()
  {
    #[ derive( Default, ComponentModel ) ]
    struct Config
    {
      host: String,
      port: i32,
      enabled: bool,
    }

    let mut config = Config::default();

    // Use Assign trait (auto-generated)
    config.assign( "localhost".to_string() );
    config.assign( 8080i32 );
    config.enabled_set( true ); // Use field-specific method

    assert_eq!( config.host, "localhost" );
    assert_eq!( config.port, 8080 );
    assert_eq!( config.enabled, true );

    // Use fluent builder pattern (auto-generated)
    let config2 = Config::default()
      .impute( "api.example.com".to_string() )
      .impute( 3000i32 )
      .enabled_with( false ); // Use field-specific method

    assert_eq!( config2.host, "api.example.com" );
    assert_eq!( config2.port, 3000 );
    assert_eq!( config2.enabled, false );
  }

  #[test]
  fn single_field_component_model()
  {
    #[ derive( Default, ComponentModel ) ]
    struct SimpleConfig
    {
      value: i32,
    }

    let mut config = SimpleConfig::default();
    config.value_set( 123 );
    assert_eq!( config.value, 123 );

    let config2 = SimpleConfig::default().value_with( 456 );
    assert_eq!( config2.value, 456 );
  }

  #[test]
  fn many_fields_component_model()
  {
    #[ derive( Default, ComponentModel ) ]
    struct LargeConfig
    {
      field1: i32,
      field2: String,
      field3: f64,
      field4: bool,
      field5: u64,
    }

    let config = LargeConfig::default()
      .field1_with( 1 )
      .field2_with( "two".to_string() )
      .field3_with( 3.0 )
      .field4_with( true )
      .field5_with( 5u64 ); // Fix: explicit u64 type

    assert_eq!( config.field1, 1 );
    assert_eq!( config.field2, "two" );
    assert_eq!( config.field3, 3.0 );
    assert_eq!( config.field4, true );
    assert_eq!( config.field5, 5 );
  }

  #[test]
  fn duplicate_types_handled()
  {
    #[ derive( Default, ComponentModel ) ]
    struct DuplicateTypes
    {
      x: i32,
      y: i32,
      flag1: bool,
      flag2: bool,
    }

    let obj = DuplicateTypes::default()
      .x_with( 10 )
      .y_with( 20 )
      .flag1_with( true )
      .flag2_with( false );

    assert_eq!( obj.x, 10 );
    assert_eq!( obj.y, 20 );
    assert_eq!( obj.flag1, true );
    assert_eq!( obj.flag2, false );
  }

  // ISSUE FOUND: ComponentModel doesn't support tuple structs
  // Error: "ComponentModel requires named fields" (component_model.rs:37)
  // This limitation is NOT documented in src/lib.rs public API documentation
  // TODO: Add documentation to ComponentModel explaining named-fields-only requirement
  /*
  #[test]
  fn tuple_struct_component_model()
  {
    #[ derive( Default, ComponentModel ) ]
    struct Point( i32, i32 );

    let point = Point::default()
      .field_0_with( 100 )
      .field_1_with( 200 );

    assert_eq!( point.0, 100 );
    assert_eq!( point.1, 200 );
  }
  */
}

// ============================================================================
// Test 5: Cross-Macro Combinations
// ============================================================================

#[cfg(test)]
mod test_macro_combinations
{
  use super::*;

  #[test]
  fn component_from_plus_assign()
  {
    #[ derive( Default, ComponentFrom, Assign ) ]
    struct Data
    {
      value: i32,
      label: String,
    }

    let mut obj = Data::default();
    obj.assign( 99 );
    obj.assign( "test" );

    let extracted_value: i32 = From::from( &obj );
    let extracted_label: String = From::from( &obj );

    assert_eq!( extracted_value, 99 );
    assert_eq!( extracted_label, "test" );
  }

  #[test]
  fn round_trip_conversion()
  {
    #[ derive( Debug, PartialEq, Default, ComponentFrom, Assign ) ]
    struct Original
    {
      id: i32,
      name: String,
    }

    let obj1 = Original { id: 123, name: "original".into() };

    // Extract
    let id: i32 = From::from( &obj1 );
    let name: String = From::from( &obj1 );

    // Rebuild
    let mut obj2 = Original::default();
    obj2.assign( id );
    obj2.assign( name );

    assert_eq!( obj1, obj2 );
  }
}