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
//! Controller actions implementation
//!
//! This file contains:
//! - Controller assignment and activation actions following OpenSCENARIO specification  
//! - Override actions for manual control (throttle, brake, steering, gear)
//! - Controller configuration and parameter setting per OpenSCENARIO XSD schema
//! - Gear control types (manual/automatic) and supporting enumerations
//!
use crate::types::basic::{Boolean, Double, Int};
use crate::types::catalogs::entities::CatalogController;
use crate::types::catalogs::references::CatalogReference;
use crate::types::controllers::Controller;
use serde::{Deserialize, Serialize};


/// Main controller action wrapper containing all controller action types
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[derive(Default)]
pub struct ControllerAction {
    /// Assign controller action
    #[serde(
        rename = "AssignControllerAction",
        skip_serializing_if = "Option::is_none"
    )]
    pub assign_controller_action: Option<AssignControllerAction>,

    /// Override throttle action
    #[serde(
        rename = "OverrideThrottleAction",
        skip_serializing_if = "Option::is_none"
    )]
    pub override_throttle_action: Option<OverrideThrottleAction>,

    /// Override brake action
    #[serde(
        rename = "OverrideBrakeAction",
        skip_serializing_if = "Option::is_none"
    )]
    pub override_brake_action: Option<OverrideBrakeAction>,

    /// Override clutch action
    #[serde(
        rename = "OverrideClutchAction",
        skip_serializing_if = "Option::is_none"
    )]
    pub override_clutch_action: Option<OverrideClutchAction>,

    /// Override parking brake action
    #[serde(
        rename = "OverrideParkingBrakeAction",
        skip_serializing_if = "Option::is_none"
    )]
    pub override_parking_brake_action: Option<OverrideParkingBrakeAction>,

    /// Override steering wheel action
    #[serde(
        rename = "OverrideSteeringWheelAction",
        skip_serializing_if = "Option::is_none"
    )]
    pub override_steering_wheel_action: Option<OverrideSteeringWheelAction>,

    /// Override gear action
    #[serde(rename = "OverrideGearAction", skip_serializing_if = "Option::is_none")]
    pub override_gear_action: Option<OverrideGearAction>,

    /// Activate controller action (deprecated in OpenSCENARIO 1.2)
    #[serde(
        rename = "ActivateControllerAction",
        skip_serializing_if = "Option::is_none"
    )]
    pub activate_controller_action: Option<ActivateControllerAction>,
}

/// Assign controller action for controller assignment with catalog support
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AssignControllerAction {
    #[serde(rename = "Controller")]
    pub controller: Option<Controller>,
    #[serde(rename = "CatalogReference")]
    pub catalog_reference: Option<CatalogReference<CatalogController>>,
}

/// Activate controller action for controller activation control
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ActivateControllerAction {
    #[serde(rename = "@longitudinal", skip_serializing_if = "Option::is_none")]
    pub longitudinal: Option<Boolean>,
    #[serde(rename = "@lateral", skip_serializing_if = "Option::is_none")]
    pub lateral: Option<Boolean>,
    #[serde(rename = "@lighting", skip_serializing_if = "Option::is_none")]
    pub lighting: Option<Boolean>,
    #[serde(rename = "@animation", skip_serializing_if = "Option::is_none")]
    pub animation: Option<Boolean>,
}

// Individual Override Actions matching XSD schema names

/// Override brake action (XSD compliant name)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct OverrideBrakeAction {
    #[serde(rename = "@active")]
    pub active: Boolean,
    #[serde(rename = "@value", skip_serializing_if = "Option::is_none")]
    pub value: Option<Double>, // deprecated
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub brake_input: Option<BrakeInput>,
}

/// Override throttle action (XSD compliant name)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct OverrideThrottleAction {
    #[serde(rename = "@active")]
    pub active: Boolean,
    #[serde(rename = "@value")]
    pub value: Double,
    #[serde(rename = "@maxRate", skip_serializing_if = "Option::is_none")]
    pub max_rate: Option<Double>,
}

/// Override steering wheel action (XSD compliant name)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct OverrideSteeringWheelAction {
    #[serde(rename = "@active")]
    pub active: Boolean,
    #[serde(rename = "@value")]
    pub value: Double,
    #[serde(rename = "@maxRate", skip_serializing_if = "Option::is_none")]
    pub max_rate: Option<Double>,
    #[serde(rename = "@maxTorque", skip_serializing_if = "Option::is_none")]
    pub max_torque: Option<Double>,
}

/// Override gear action (XSD compliant name)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct OverrideGearAction {
    #[serde(rename = "@active")]
    pub active: Boolean,
    #[serde(rename = "@number", skip_serializing_if = "Option::is_none")]
    pub number: Option<Double>, // deprecated
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub gear: Option<Gear>,
}

/// Override parking brake action (XSD compliant name)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct OverrideParkingBrakeAction {
    #[serde(rename = "@active")]
    pub active: Boolean,
    #[serde(rename = "@value", skip_serializing_if = "Option::is_none")]
    pub value: Option<Double>, // deprecated
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub brake_input: Option<BrakeInput>,
}

/// Override clutch action (XSD compliant name)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct OverrideClutchAction {
    #[serde(rename = "@active")]
    pub active: Boolean,
    #[serde(rename = "@value")]
    pub value: Double,
    #[serde(rename = "@maxRate", skip_serializing_if = "Option::is_none")]
    pub max_rate: Option<Double>,
}


/// Manual gear specification
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ManualGear {
    #[serde(rename = "@gear")]
    pub gear: Int,
}

/// Automatic gear specification
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AutomaticGear {
    #[serde(rename = "@gear")]
    pub gear: AutomaticGearType,
}

/// Automatic gear type enumeration
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[derive(Default)]
pub enum AutomaticGearType {
    #[serde(rename = "park")]
    Park,
    #[serde(rename = "reverse")]
    Reverse,
    #[serde(rename = "neutral")]
    Neutral,
    #[serde(rename = "drive")]
    #[default]
    Drive,
}

/// Base brake type for brake input groups
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Brake {
    #[serde(rename = "@value")]
    pub value: Double,
}

/// BrakeInput group - XSD group wrapper for brake percent/force choice
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum BrakeInput {
    #[serde(rename = "BrakePercent")]
    BrakePercent(Brake),
    #[serde(rename = "BrakeForce")]
    BrakeForce(Brake),
}

/// Gear group - XSD group wrapper for manual/automatic gear choice
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Gear {
    #[serde(rename = "ManualGear")]
    ManualGear(ManualGear),
    #[serde(rename = "AutomaticGear")]
    AutomaticGear(AutomaticGear),
}


impl Default for AssignControllerAction {
    fn default() -> Self {
        Self {
            controller: Some(Controller::default()),
            catalog_reference: None,
        }
    }
}


impl Default for ActivateControllerAction {
    fn default() -> Self {
        Self {
            longitudinal: Some(Boolean::literal(true)),
            lateral: Some(Boolean::literal(true)),
            lighting: Some(Boolean::literal(false)),
            animation: Some(Boolean::literal(false)),
        }
    }
}

impl Default for ManualGear {
    fn default() -> Self {
        Self {
            gear: Int::literal(1),
        }
    }
}

impl Default for AutomaticGear {
    fn default() -> Self {
        Self {
            gear: AutomaticGearType::Drive,
        }
    }
}


impl Default for Brake {
    fn default() -> Self {
        Self {
            value: Double::literal(0.0),
        }
    }
}

impl Default for BrakeInput {
    fn default() -> Self {
        Self::BrakePercent(Brake::default())
    }
}

impl Default for Gear {
    fn default() -> Self {
        Self::AutomaticGear(AutomaticGear::default())
    }
}


impl AssignControllerAction {
    /// Create assignment with direct controller
    pub fn with_controller(controller: Controller) -> Self {
        Self {
            controller: Some(controller),
            catalog_reference: None,
        }
    }

    /// Create assignment with catalog reference
    pub fn with_catalog_reference(catalog_reference: CatalogReference<CatalogController>) -> Self {
        Self {
            controller: None,
            catalog_reference: Some(catalog_reference),
        }
    }
}

impl ActivateControllerAction {
    /// Create activation with all control domains
    pub fn all_domains(longitudinal: bool, lateral: bool, lighting: bool, animation: bool) -> Self {
        Self {
            longitudinal: Some(Boolean::literal(longitudinal)),
            lateral: Some(Boolean::literal(lateral)),
            lighting: Some(Boolean::literal(lighting)),
            animation: Some(Boolean::literal(animation)),
        }
    }

    /// Create activation for movement only (longitudinal + lateral)
    pub fn movement_only() -> Self {
        Self {
            longitudinal: Some(Boolean::literal(true)),
            lateral: Some(Boolean::literal(true)),
            lighting: None,
            animation: None,
        }
    }
}

impl ManualGear {
    /// Create manual gear for specific gear number
    pub fn new(gear: i32) -> Self {
        Self {
            gear: Int::literal(gear),
        }
    }

    /// Neutral gear
    pub fn neutral() -> Self {
        Self::new(0)
    }

    /// First gear
    pub fn first() -> Self {
        Self::new(1)
    }

    /// Reverse gear
    pub fn reverse() -> Self {
        Self::new(-1)
    }
}

impl AutomaticGear {
    /// Create automatic gear for park
    pub fn park() -> Self {
        Self {
            gear: AutomaticGearType::Park,
        }
    }

    /// Create automatic gear for reverse
    pub fn reverse() -> Self {
        Self {
            gear: AutomaticGearType::Reverse,
        }
    }

    /// Create automatic gear for neutral
    pub fn neutral() -> Self {
        Self {
            gear: AutomaticGearType::Neutral,
        }
    }

    /// Create automatic gear for drive
    pub fn drive() -> Self {
        Self {
            gear: AutomaticGearType::Drive,
        }
    }
}

impl Gear {
    /// Create manual gear wrapper
    pub fn manual(gear: i32) -> Self {
        Self::ManualGear(ManualGear::new(gear))
    }

    /// Create automatic gear wrapper
    pub fn automatic(gear_type: AutomaticGearType) -> Self {
        Self::AutomaticGear(AutomaticGear { gear: gear_type })
    }

    /// Create manual first gear
    pub fn manual_first() -> Self {
        Self::ManualGear(ManualGear::first())
    }

    /// Create manual neutral gear
    pub fn manual_neutral() -> Self {
        Self::ManualGear(ManualGear::neutral())
    }

    /// Create manual reverse gear
    pub fn manual_reverse() -> Self {
        Self::ManualGear(ManualGear::reverse())
    }

    /// Create automatic park gear
    pub fn automatic_park() -> Self {
        Self::AutomaticGear(AutomaticGear::park())
    }

    /// Create automatic drive gear
    pub fn automatic_drive() -> Self {
        Self::AutomaticGear(AutomaticGear::drive())
    }

    /// Create automatic reverse gear
    pub fn automatic_reverse() -> Self {
        Self::AutomaticGear(AutomaticGear::reverse())
    }

    /// Create automatic neutral gear
    pub fn automatic_neutral() -> Self {
        Self::AutomaticGear(AutomaticGear::neutral())
    }
}

impl Brake {
    /// Create brake with specific value
    pub fn new(value: f64) -> Self {
        Self {
            value: Double::literal(value),
        }
    }

    /// Create zero brake
    pub fn zero() -> Self {
        Self::new(0.0)
    }

    /// Create full brake
    pub fn full() -> Self {
        Self::new(1.0)
    }
}

impl BrakeInput {
    /// Create brake percent input
    pub fn percent(value: f64) -> Self {
        Self::BrakePercent(Brake::new(value))
    }

    /// Create brake force input
    pub fn force(value: f64) -> Self {
        Self::BrakeForce(Brake::new(value))
    }

    /// Get brake value regardless of type
    pub fn value(&self) -> &Double {
        match self {
            Self::BrakePercent(brake) => &brake.value,
            Self::BrakeForce(brake) => &brake.value,
        }
    }

    /// Check if this is a percent-based brake input
    pub fn is_percent(&self) -> bool {
        matches!(self, Self::BrakePercent(_))
    }

    /// Check if this is a force-based brake input
    pub fn is_force(&self) -> bool {
        matches!(self, Self::BrakeForce(_))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::controllers::Controller;

    #[test]
    fn test_assign_controller_action_creation() {
        let controller = Controller::default();
        let action = AssignControllerAction::with_controller(controller);

        assert!(action.controller.is_some());
        assert!(action.catalog_reference.is_none());
    }

    #[test]
    fn test_activate_controller_action_creation() {
        let action = ActivateControllerAction::all_domains(true, true, false, false);

        assert_eq!(action.longitudinal.unwrap().as_literal(), Some(&true));
        assert_eq!(action.lateral.unwrap().as_literal(), Some(&true));
        assert_eq!(action.lighting.unwrap().as_literal(), Some(&false));
        assert_eq!(action.animation.unwrap().as_literal(), Some(&false));
    }

    #[test]
    fn test_activate_controller_default_serialization() {
        let action = ActivateControllerAction::default();
        let xml = quick_xml::se::to_string(&action).expect("Serialization should succeed");

        // Should contain explicit boolean values, not empty strings
        assert!(xml.contains("longitudinal=\"true\""));
        assert!(xml.contains("lateral=\"true\""));
        assert!(xml.contains("lighting=\"false\""));
        assert!(xml.contains("animation=\"false\""));
    }

    #[test]
    fn test_activate_controller_movement_only() {
        let action = ActivateControllerAction::movement_only();

        assert_eq!(action.longitudinal.unwrap().as_literal(), Some(&true));
        assert_eq!(action.lateral.unwrap().as_literal(), Some(&true));
        assert!(action.lighting.is_none());
        assert!(action.animation.is_none());
    }

    #[test]
    fn test_manual_gear_creation() {
        let first_gear = ManualGear::first();
        assert_eq!(first_gear.gear.as_literal().unwrap(), &1);

        let neutral = ManualGear::neutral();
        assert_eq!(neutral.gear.as_literal().unwrap(), &0);

        let reverse = ManualGear::reverse();
        assert_eq!(reverse.gear.as_literal().unwrap(), &(-1));
    }

    #[test]
    fn test_automatic_gear_creation() {
        let park = AutomaticGear::park();
        assert_eq!(park.gear, AutomaticGearType::Park);

        let drive = AutomaticGear::drive();
        assert_eq!(drive.gear, AutomaticGearType::Drive);

        let reverse = AutomaticGear::reverse();
        assert_eq!(reverse.gear, AutomaticGearType::Reverse);

        let neutral = AutomaticGear::neutral();
        assert_eq!(neutral.gear, AutomaticGearType::Neutral);
    }

    #[test]
    fn test_controller_action_defaults() {
        let assign = AssignControllerAction::default();
        assert!(assign.controller.is_some());

        let activate = ActivateControllerAction::default();
        assert_eq!(activate.longitudinal.unwrap().as_literal(), Some(&true));
        assert_eq!(activate.lateral.unwrap().as_literal(), Some(&true));

        let controller_action = ControllerAction::default();
        assert!(controller_action.assign_controller_action.is_none());
        assert!(controller_action.override_brake_action.is_none());
        assert!(controller_action.override_throttle_action.is_none());
    }

    // Tests for new group types
    #[test]
    fn test_brake_creation_and_helpers() {
        let brake = Brake::new(0.5);
        assert_eq!(brake.value.as_literal().unwrap(), &0.5);

        let zero_brake = Brake::zero();
        assert_eq!(zero_brake.value.as_literal().unwrap(), &0.0);

        let full_brake = Brake::full();
        assert_eq!(full_brake.value.as_literal().unwrap(), &1.0);

        let default_brake = Brake::default();
        assert_eq!(default_brake.value.as_literal().unwrap(), &0.0);
    }

    #[test]
    fn test_brake_input_group() {
        let percent_brake = BrakeInput::percent(0.7);
        assert!(percent_brake.is_percent());
        assert!(!percent_brake.is_force());
        assert_eq!(percent_brake.value().as_literal(), Some(&0.7));

        let force_brake = BrakeInput::force(500.0);
        assert!(!force_brake.is_percent());
        assert!(force_brake.is_force());
        assert_eq!(force_brake.value().as_literal(), Some(&500.0));

        let default_brake_input = BrakeInput::default();
        assert!(default_brake_input.is_percent());
        assert_eq!(default_brake_input.value().as_literal(), Some(&0.0));
    }

    #[test]
    fn test_gear_group_creation() {
        let manual_gear = Gear::manual(3);
        if let Gear::ManualGear(gear) = manual_gear {
            assert_eq!(gear.gear.as_literal(), Some(&3));
        } else {
            panic!("Expected ManualGear variant");
        }

        let auto_gear = Gear::automatic(AutomaticGearType::Park);
        if let Gear::AutomaticGear(gear) = auto_gear {
            assert_eq!(gear.gear, AutomaticGearType::Park);
        } else {
            panic!("Expected AutomaticGear variant");
        }
    }

    #[test]
    fn test_gear_group_convenience_methods() {
        let manual_first = Gear::manual_first();
        if let Gear::ManualGear(gear) = manual_first {
            assert_eq!(gear.gear.as_literal(), Some(&1));
        } else {
            panic!("Expected ManualGear variant");
        }

        let manual_neutral = Gear::manual_neutral();
        if let Gear::ManualGear(gear) = manual_neutral {
            assert_eq!(gear.gear.as_literal(), Some(&0));
        } else {
            panic!("Expected ManualGear variant");
        }

        let manual_reverse = Gear::manual_reverse();
        if let Gear::ManualGear(gear) = manual_reverse {
            assert_eq!(gear.gear.as_literal(), Some(&(-1)));
        } else {
            panic!("Expected ManualGear variant");
        }

        let auto_park = Gear::automatic_park();
        if let Gear::AutomaticGear(gear) = auto_park {
            assert_eq!(gear.gear, AutomaticGearType::Park);
        } else {
            panic!("Expected AutomaticGear variant");
        }

        let auto_drive = Gear::automatic_drive();
        if let Gear::AutomaticGear(gear) = auto_drive {
            assert_eq!(gear.gear, AutomaticGearType::Drive);
        } else {
            panic!("Expected AutomaticGear variant");
        }
    }

    #[test]
    fn test_gear_group_default() {
        let default_gear = Gear::default();
        if let Gear::AutomaticGear(gear) = default_gear {
            assert_eq!(gear.gear, AutomaticGearType::Drive);
        } else {
            panic!("Expected AutomaticGear variant as default");
        }
    }
}