openscenario-rs 0.3.1

Rust library for parsing and manipulating OpenSCENARIO files
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
# OpenSCENARIO-rs Builder System Guide

The OpenSCENARIO-rs builder system provides a type-safe, fluent API for programmatically constructing OpenSCENARIO documents. This guide covers all aspects of using the builder system effectively.

## Current Status

⚠️ **Implementation Status**: The builder system is 99% functional with comprehensive features implemented across 6 development sprints. Currently resolving final 4 lifetime variance errors in fluent API method chaining.

**Latest Updates**:
- ✅ Complete builder API with entities, actions, conditions, storyboard
- ✅ Parameter system with constraint validation
- ✅ Catalog integration with reference resolution
- ✅ 87% of compilation errors resolved (30+ → 4 remaining)
- 🔄 Fixing final lifetime variance issues in storyboard builders

## Table of Contents

1. [Quick Start]#quick-start
2. [Current Implementation]#current-implementation
3. [Core Concepts]#core-concepts
4. [Type States]#type-states
5. [Entity Builders]#entity-builders
6. [Action Builders]#action-builders
7. [Condition Builders]#condition-builders
8. [Storyboard Construction]#storyboard-construction
9. [Parameter Support]#parameter-support
10. [Catalog Integration]#catalog-integration
11. [Error Handling]#error-handling
12. [Known Issues]#known-issues
13. [Best Practices]#best-practices
14. [Examples]#examples

## Quick Start

Enable the builder feature in your `Cargo.toml`:

```toml
[dependencies]
openscenario-rs = { version = "0.1", features = ["builder"] }
```

Create a simple scenario:

```rust
use openscenario_rs::builder::ScenarioBuilder;
use openscenario_rs::types::enums::ParameterType;

let scenario = ScenarioBuilder::new()
    .with_header("My First Scenario", "Your Name")
    .with_entities()
        .add_vehicle("ego")
            .car()
            .finish()
    .build()?;
```

## Current Implementation

The builder system has been developed through 6 comprehensive sprints:

### ✅ Sprint 0: Foundation
- Error handling system with structured error types
- Basic ScenarioBuilder with type-safe state transitions
- Core validation framework

### ✅ Sprint 1: Entity System  
- VehicleBuilder with car/truck presets
- Parameter support for configurable entities
- Catalog vehicle references

### ✅ Sprint 2: Action System
- SpeedActionBuilder for longitudinal control
- TeleportActionBuilder with position integration
- Action wrapper types

### ✅ Sprint 3: Storyboard System
- Story/Act/Maneuver hierarchical builders
- Action integration with proper nesting
- Event sequencing

### ✅ Sprint 4: Conditions & Triggers
- TimeCondition, SpeedCondition, DistanceCondition
- Complex trigger logic with AND/OR combinations
- Edge and delay handling

### ✅ Sprint 5: Advanced Features
- Full catalog integration with parameter resolution
- Comprehensive parameter system with constraints
- Validation framework integration

### ✅ Sprint 6: Documentation & Polish
- Complete API documentation
- Working examples and demos
- Test automation scripts

### 🔄 Current: Final Compilation Fixes
- **87% complete**: 30+ errors → 4 remaining
- **4 lifetime variance errors** in storyboard method chaining
- All other categories resolved: trait imports, type mismatches, API compatibility

## Core Concepts

### Type-Safe Construction

The builder system uses Rust's type system to enforce correct construction order:

- **Compile-time validation**: Invalid operations are caught at compile time
- **State transitions**: Each step unlocks new methods while preventing invalid ones
- **Zero-cost abstractions**: No runtime overhead for type safety

### Fluent API

The builder provides a natural, readable syntax:

```rust
ScenarioBuilder::new()
    .with_header("Highway Test", "Author")
    .add_parameter("speed", ParameterType::Double, "25.0")
    .with_entities()
        .add_vehicle("ego")
            .car()
            .with_dimensions(4.5, 1.8, 1.4)
            .finish()
    .with_storyboard()
        .add_story("main")
            .add_act("acceleration")
                .add_maneuver("speed_up", "ego")
                    .add_speed_action()
                        .to_speed(30.0)
                        .finish()?
                    .finish()
                .finish()
            .finish()
        .finish()
    .build()?
```

## Type States

The builder progresses through several type states that enforce correct construction order:

### State Diagram

```
Empty → HasHeader → HasEntities → Complete
  ↓         ↓            ↓           ↓
 new()  add_param()  add_vehicle()  build()
       with_catalog() add_pedestrian()
       with_road()   with_storyboard()
```

### State Descriptions

- **Empty**: Initial state, must call `with_header()` first
- **HasHeader**: Can add parameters, catalogs, road networks
- **HasEntities**: Can add entities and build storyboard
- **Complete**: Ready to build final OpenSCENARIO document

## Entity Builders

### Vehicle Builder

Create vehicles with different configurations:

```rust
// Basic car
.add_vehicle("ego")
    .car()
    .finish()

// Custom truck
.add_vehicle("truck1")
    .truck()
    .with_dimensions(8.0, 2.5, 3.0)
    .finish()

// Car with custom properties
.add_vehicle("sports_car")
    .car()
    .with_dimensions(4.2, 1.9, 1.3)
    .finish()
```

### Catalog Vehicle Builder

Reference vehicles from catalogs:

```rust
.add_catalog_vehicle("ego")
    .from_catalog("VehicleCatalog")
    .entry_name("PassengerCar")
    .finish()
```

### Pedestrian Builder

Add pedestrian entities:

```rust
.add_pedestrian("pedestrian1")
    .with_mass(75.0)
    .finish()
```

## Action Builders

### Speed Actions

Control vehicle speed:

```rust
.add_speed_action()
    .named("accelerate")
    .to_speed(30.0)  // Absolute speed
    .finish()?

.add_speed_action()
    .named("slow_down")
    .change_by(-5.0)  // Relative speed change
    .finish()?
```

### Teleport Actions

Instantly move entities:

```rust
.add_teleport_action()
    .named("lane_change")
    .to()
        .world_position(100.0, -3.5, 0.0)
    .finish()?

.add_teleport_action()
    .named("highway_entry")
    .to()
        .lane_position("highway", "1", 50.0)
    .finish()?
```

### Lane Change Actions

Perform lane changes:

```rust
.add_lane_change_action()
    .named("overtake")
    .target_lane_offset(-1)  // Move to left lane
    .dynamics()
        .max_lateral_acc(2.0)
        .finish()
    .finish()?
```

## Condition Builders

### Time Conditions

Trigger events based on simulation time:

```rust
.triggered_by()
    .time_condition(5.0)  // After 5 seconds
    .finish()
```

### Speed Conditions

Trigger based on entity speed:

```rust
.triggered_by()
    .speed_condition("ego", 25.0)  // When ego exceeds 25 m/s
    .finish()
```

### Distance Conditions

Trigger based on distance to positions:

```rust
.triggered_by()
    .distance_condition("ego")
        .to_position(world_position(100.0, 0.0, 0.0))
        .closer_than(10.0)
    .finish()
```

### Complex Triggers

Combine multiple conditions:

```rust
.triggered_by()
    .add_condition_group()
        .time_condition()
            .at_time(3.0)
            .finish()?
        .speed_condition()
            .for_entity("ego")
            .speed_above(20.0)
            .finish()?
        .finish_group()
    .build()?
```

## Storyboard Construction

### Stories, Acts, and Maneuvers

Build complex scenario behavior:

```rust
.with_storyboard()
    .add_story("highway_scenario")
        .add_act("initial_phase")
            .add_maneuver("ego_acceleration", "ego")
                .add_speed_action()
                    .to_speed(25.0)
                    .triggered_by()
                        .time_condition(1.0)
                        .finish()
                    .finish()?
                .finish()
            .finish()
        
        .add_act("overtaking_phase")
            .add_maneuver("ego_overtake", "ego")
                .add_lane_change_action()
                    .target_lane_offset(-1)
                    .triggered_by()
                        .speed_condition("target", 20.0)
                        .finish()
                    .finish()?
                .finish()
            .finish()
        .finish()
    .finish()
```

### Event Sequencing

Control when events occur:

```rust
.add_maneuver("complex_behavior", "ego")
    // First action: accelerate after 2 seconds
    .add_speed_action()
        .named("initial_acceleration")
        .to_speed(30.0)
        .triggered_by()
            .time_condition(2.0)
            .finish()
        .finish()?
    
    // Second action: lane change when close to target
    .add_lane_change_action()
        .named("overtake_maneuver")
        .target_lane_offset(-1)
        .triggered_by()
            .distance_condition("ego")
                .to_entity("target")
                .closer_than(20.0)
            .finish()
        .finish()?
    .finish()
```

## Parameter Support

### Adding Parameters

Make scenarios configurable:

```rust
.with_header("Configurable Scenario", "Author")
.add_parameter("initial_speed", ParameterType::Double, "25.0")
.add_parameter("target_lane", ParameterType::String, "1")
.add_parameter("weather_condition", ParameterType::String, "sunny")
```

### Using Parameters

Reference parameters in values:

```rust
.add_speed_action()
    .to_speed_parameter("initial_speed")  // Use ${initial_speed}
    .finish()?
```

### Parameter Validation

Validate parameter constraints:

```rust
.add_parameter_with_constraints("speed", ParameterType::Double, "25.0")
    .range(0.0, 50.0)
    .finish()
```

## Catalog Integration

### Catalog Locations

Specify where to find catalogs:

```rust
.with_catalog_locations()
    .vehicle_catalog("./catalogs/VehicleCatalog.xosc")
    .pedestrian_catalog("./catalogs/PedestrianCatalog.xosc")
    .finish()
```

### Using Catalog Entities

Reference entities from catalogs:

```rust
.add_catalog_vehicle("ego")
    .from_catalog("VehicleCatalog")
    .entry_name("BMW_X5")
    .parameter_assignments()
        .assign("color", "blue")
        .assign("license_plate", "ABC-123")
        .finish()
    .finish()
```

## Error Handling

### Builder Errors

The builder system provides detailed error messages:

```rust
use openscenario_rs::builder::error::BuilderError;

match scenario_result {
    Ok(scenario) => println!("Scenario built successfully"),
    Err(BuilderError::MissingField { field, suggestion }) => {
        println!("Missing {}: {}", field, suggestion);
    }
    Err(BuilderError::ValidationError { message, suggestion }) => {
        println!("Validation failed: {} ({})", message, suggestion);
    }
    Err(BuilderError::TypeMismatch { expected, found }) => {
        println!("Type error: expected {}, found {}", expected, found);
    }
    Err(e) => println!("Other error: {}", e),
}
```

### Validation

The builder includes comprehensive validation:

```rust
let builder = ScenarioBuilder::new()
    .with_header("Test", "Author")
    .with_entities();

// Validate current state
builder.validate()?;

// Continue building...
let scenario = builder.build()?;
```

### Error Categories

1. **Compilation Errors**: Type safety enforced at compile time
2. **Runtime Validation**: Logical consistency checks during build
3. **Parameter Errors**: Invalid parameter references or constraints
4. **Catalog Errors**: Missing catalog entries or files

## Known Issues

### Lifetime Variance in Storyboard Builders

**Status**: 4 remaining compilation errors in fluent API method chaining

**Affected Files**:
- `src/builder/storyboard/story.rs` (lines 81, 131)
- `src/builder/storyboard/maneuver.rs` (lines 34, 39)

**Error Pattern**:
```rust
error: lifetime may not live long enough
method was supposed to return data with lifetime `'parent` but it is returning data with lifetime `'1`
```

**Workaround**: Use explicit lifetime annotations or alternative API patterns until resolved.

**Example Affected Code**:
```rust
// Currently problematic - lifetime variance issue
.add_story("main")
    .add_act("phase1")  // <-- Lifetime error here
        .add_maneuver("action", "ego")
            .finish()
        .finish()
    .finish()
```

**Alternative Pattern** (works around issue):
```rust
// Use separate builder variables to avoid chaining
let story_builder = scenario.add_story("main");
let act_builder = story_builder.add_act("phase1");
let maneuver_builder = act_builder.add_maneuver("action", "ego");
let completed_story = maneuver_builder.finish().finish().finish();
```

### Compilation Status

Run `cargo check --features builder` to see current compilation status:
- **Resolved**: Trait imports, type mismatches, struct fields, API compatibility
- 🔄 **In Progress**: 4 lifetime variance errors in method chaining
- ℹ️ **Warnings**: 54 unused import warnings (non-blocking)

## Best Practices

### 1. Use Type States Effectively

Let the type system guide you:

```rust
// Good: Type system prevents invalid operations
let builder = ScenarioBuilder::new();
// builder.add_vehicle("ego");  // Compile error - no header yet

let builder = builder.with_header("Test", "Author");
// builder.build();  // Compile error - no entities yet

let builder = builder.with_entities();
// Now can add vehicles and build
```

### 2. Handle Errors Appropriately

Use `?` operator for clean error propagation:

```rust
fn build_scenario() -> Result<OpenScenario, BuilderError> {
    let scenario = ScenarioBuilder::new()
        .with_header("Test", "Author")
        .with_entities()
            .add_vehicle("ego")
                .car()
                .finish()
        .with_storyboard()
            .add_story("main")
                .add_act("test")
                    .add_maneuver("action", "ego")
                        .add_speed_action()
                            .to_speed(30.0)
                            .finish()?  // Propagate action builder errors
                        .finish()
                    .finish()
                .finish()
            .finish()
        .build()?;  // Propagate scenario builder errors
    
    Ok(scenario)
}
```

### 3. Use Meaningful Names

Choose descriptive names for entities and events:

```rust
.add_vehicle("ego_vehicle")  // Not just "ego"
.add_vehicle("target_vehicle")
.add_vehicle("oncoming_truck")

.add_speed_action()
    .named("highway_acceleration")  // Descriptive event names
    .to_speed(30.0)
    .finish()?
```

### 4. Structure Complex Scenarios

Break complex scenarios into logical phases:

```rust
.with_storyboard()
    .add_story("highway_overtaking_scenario")
        .add_act("setup_phase")
            // Initial positioning and speeds
            .finish()
        
        .add_act("approach_phase")
            // Approach target vehicle
            .finish()
        
        .add_act("overtaking_phase")
            // Execute overtaking maneuver
            .finish()
        
        .add_act("completion_phase")
            // Return to original lane
            .finish()
        .finish()
```

### 5. Use Parameters for Reusability

Make scenarios configurable:

```rust
.add_parameter("ego_initial_speed", ParameterType::Double, "25.0")
.add_parameter("target_speed", ParameterType::Double, "20.0")
.add_parameter("overtaking_speed", ParameterType::Double, "35.0")
.add_parameter("safety_distance", ParameterType::Double, "10.0")
```

## Examples

### Complete Highway Scenario

```rust
use openscenario_rs::{ScenarioBuilder, types::enums::ParameterType};

fn highway_overtaking_scenario() -> Result<OpenScenario, BuilderError> {
    ScenarioBuilder::new()
        .with_header("Highway Overtaking Scenario", "OpenSCENARIO-rs")
        
        // Parameters for configurability
        .add_parameter("ego_speed", ParameterType::Double, "25.0")
        .add_parameter("target_speed", ParameterType::Double, "20.0")
        .add_parameter("overtake_speed", ParameterType::Double, "35.0")
        
        // Road network
        .with_road_file("highway_3_lanes.xodr")
        
        // Entities
        .with_entities()
            .add_vehicle("ego")
                .car()
                .with_dimensions(4.5, 1.8, 1.4)
                .finish()
            
            .add_vehicle("target")
                .car()
                .finish()
            
            .add_vehicle("oncoming")
                .truck()
                .finish()
        
        // Scenario behavior
        .with_storyboard()
            .add_story("overtaking_story")
                .add_act("initial_driving")
                    .add_maneuver("ego_cruise", "ego")
                        .add_speed_action()
                            .named("initial_cruise")
                            .to_speed_parameter("ego_speed")
                            .triggered_by()
                                .time_condition(1.0)
                                .finish()
                            .finish()?
                        .finish()
                    
                    .add_maneuver("target_cruise", "target")
                        .add_speed_action()
                            .named("target_cruise")
                            .to_speed_parameter("target_speed")
                            .triggered_by()
                                .time_condition(0.5)
                                .finish()
                            .finish()?
                        .finish()
                    .finish()
                
                .add_act("overtaking")
                    .add_maneuver("ego_overtake", "ego")
                        .add_speed_action()
                            .named("accelerate_for_overtake")
                            .to_speed_parameter("overtake_speed")
                            .triggered_by()
                                .distance_condition("ego")
                                    .to_entity("target")
                                    .closer_than(30.0)
                                .finish()
                            .finish()?
                        
                        .add_lane_change_action()
                            .named("move_to_left_lane")
                            .target_lane_offset(-1)
                            .triggered_by()
                                .speed_condition("ego", 30.0)
                                .finish()
                            .finish()?
                        
                        .add_lane_change_action()
                            .named("return_to_right_lane")
                            .target_lane_offset(1)
                            .triggered_by()
                                .distance_condition("ego")
                                    .to_entity("target")
                                    .farther_than(50.0)
                                .finish()
                            .finish()?
                        .finish()
                    .finish()
                .finish()
            .finish()
        .build()
}
```

### Urban Intersection Scenario

```rust
fn urban_intersection_scenario() -> Result<OpenScenario, BuilderError> {
    ScenarioBuilder::new()
        .with_header("Urban Intersection Scenario", "Traffic Engineer")
        
        .add_parameter("approach_speed", ParameterType::Double, "15.0")
        .add_parameter("stop_distance", ParameterType::Double, "5.0")
        
        .with_road_file("urban_intersection.xodr")
        
        .with_entities()
            .add_vehicle("ego")
                .car()
                .finish()
            
            .add_pedestrian("pedestrian1")
                .with_mass(75.0)
                .finish()
            
            .add_vehicle("cross_traffic")
                .car()
                .finish()
        
        .with_storyboard()
            .add_story("intersection_approach")
                .add_act("approach_phase")
                    .add_maneuver("ego_approach", "ego")
                        .add_speed_action()
                            .named("approach_intersection")
                            .to_speed_parameter("approach_speed")
                            .triggered_by()
                                .time_condition(0.5)
                                .finish()
                            .finish()?
                        
                        .add_speed_action()
                            .named("stop_for_pedestrian")
                            .to_speed(0.0)
                            .triggered_by()
                                .distance_condition("ego")
                                    .to_entity("pedestrian1")
                                    .closer_than_parameter("stop_distance")
                                .finish()
                            .finish()?
                        .finish()
                    .finish()
                
                .add_act("crossing_phase")
                    .add_maneuver("pedestrian_cross", "pedestrian1")
                        .add_teleport_action()
                            .named("cross_street")
                            .to()
                                .world_position(50.0, 10.0, 0.0)
                            .triggered_by()
                                .speed_condition("ego", 0.1)  // When ego stops
                                .finish()
                            .finish()?
                        .finish()
                    
                    .add_maneuver("ego_proceed", "ego")
                        .add_speed_action()
                            .named("proceed_through_intersection")
                            .to_speed_parameter("approach_speed")
                            .triggered_by()
                                .distance_condition("pedestrian1")
                                    .to_position(world_position(50.0, 10.0, 0.0))
                                    .closer_than(2.0)  // When pedestrian reaches other side
                                .finish()
                            .finish()?
                        .finish()
                    .finish()
                .finish()
            .finish()
        .build()
}
```

## Advanced Features

### Custom Validation

Implement custom validation logic:

```rust
impl ScenarioBuilder<HasEntities> {
    fn validate_entity_count(&self) -> Result<(), BuilderError> {
        if let Some(entities) = &self.data.entities {
            if entities.scenario_objects.is_empty() {
                return Err(BuilderError::validation_error(
                    "At least one entity is required"
                ));
            }
        }
        Ok(())
    }
}
```

### Performance Optimization

For large scenarios, consider:

```rust
// Pre-allocate collections
let mut builder = ScenarioBuilder::new()
    .with_header("Large Scenario", "Author")
    .with_entities();

// Add many entities efficiently
for i in 0..100 {
    builder = builder
        .add_vehicle(&format!("vehicle_{}", i))
            .car()
            .finish();
}

let scenario = builder.build()?;
```

### Integration with External Tools

Export scenarios for external validation:

```rust
let scenario = build_my_scenario()?;

// Serialize to XML
let xml = openscenario_rs::serialize_to_string(&scenario)?;

// Save to file
std::fs::write("scenario.xosc", xml)?;

// Validate with external tool
std::process::Command::new("openscenario-validator")
    .arg("scenario.xosc")
    .status()?;
```

## Troubleshooting

### Common Issues

1. **Compilation Errors**: Usually indicate incorrect state transitions
   ```rust
   // Error: Cannot add vehicle before header
   ScenarioBuilder::new().add_vehicle("ego");  //   
   // Correct: Set header first
   ScenarioBuilder::new()
       .with_header("Test", "Author")
       .with_entities()
       .add_vehicle("ego");  //   ```

2. **Runtime Errors**: Check for missing required fields
   ```rust
   // Error: Missing speed value
   .add_speed_action()
       .finish()?;  // ❌ No speed set
   
   // Correct: Set speed value
   .add_speed_action()
       .to_speed(30.0)
       .finish()?;  //   ```

3. **Validation Errors**: Ensure logical consistency
   ```rust
   // Error: Invalid entity reference
   .add_maneuver("test", "nonexistent_entity");  //   
   // Correct: Use existing entity
   .add_maneuver("test", "ego");  //   ```

### Debug Tips

1. **Enable Debug Logging**:
   ```rust
   env_logger::init();
   log::debug!("Building scenario...");
   ```

2. **Validate Incrementally**:
   ```rust
   let builder = ScenarioBuilder::new()
       .with_header("Test", "Author");
   builder.validate()?;  // Check each step
   
   let builder = builder.with_entities();
   builder.validate()?;
   ```

3. **Use Type Annotations**:
   ```rust
   let builder: ScenarioBuilder<HasHeader> = ScenarioBuilder::new()
       .with_header("Test", "Author");
   ```

This guide provides comprehensive coverage of the OpenSCENARIO-rs builder system. For more examples and advanced usage patterns, see the `examples/` directory in the repository.