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
//! Longitudinal action builders for distance keeping and speed profiles
//!
//! This module provides builders for actions that control longitudinal movement,
//! including maintaining distance to other entities and executing time-based speed profiles.
//!
//! # Available Builders
//!
//! - [`LongitudinalDistanceActionBuilder`] - Maintain longitudinal distance to a leading entity
//! - [`SpeedProfileActionBuilder`] - Define multi-point speed profile over time
//!
//! # Usage Examples
//!
//! ```rust
//! use openscenario_rs::builder::actions::longitudinal::{
//!     LongitudinalDistanceActionBuilder, SpeedProfileActionBuilder
//! };
//!
//! // Create a longitudinal distance action to maintain 10m from lead vehicle
//! let distance_action = LongitudinalDistanceActionBuilder::new()
//!     .for_entity("ego_vehicle")
//!     .from_entity("lead_vehicle")
//!     .at_distance(10.0)
//!     .with_freespace(true);
//!
//! // Create a speed profile action with multiple time/speed points
//! let profile_action = SpeedProfileActionBuilder::new()
//!     .for_entity("ego_vehicle")
//!     .add_entry_direct(0.0, 0.0)
//!     .add_entry_direct(5.0, 30.0)
//!     .add_entry_direct(10.0, 50.0);
//! ```

use crate::builder::actions::base::{ActionBuilder, ManeuverAction};
use crate::builder::{BuilderError, BuilderResult};
use crate::types::{
    actions::movement::{
        LongitudinalAction, LongitudinalActionChoice, LongitudinalDistanceAction,
        SpeedProfileAction, SpeedProfileEntry,
    },
    actions::wrappers::PrivateAction,
    basic::{Boolean, Double, OSString},
};

/// Builder for longitudinal distance actions
#[derive(Debug, Default)]
pub struct LongitudinalDistanceActionBuilder {
    entity_ref: Option<String>,
    target_entity: Option<String>,
    distance: Option<f64>,
    time_gap: Option<f64>,
    freespace: bool,
    continuous: bool,
}

impl LongitudinalDistanceActionBuilder {
    /// Create new longitudinal distance action builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Set target entity for this action
    pub fn for_entity(mut self, entity_ref: &str) -> Self {
        self.entity_ref = Some(entity_ref.to_string());
        self
    }

    /// Set the target entity to maintain distance from
    pub fn from_entity(mut self, target_entity: &str) -> Self {
        self.target_entity = Some(target_entity.to_string());
        self
    }

    /// Set fixed distance value (mutually exclusive with time_gap)
    pub fn at_distance(mut self, distance: f64) -> Self {
        self.distance = Some(distance);
        self.time_gap = None; // Mutually exclusive
        self
    }

    /// Set time gap value (mutually exclusive with distance)
    pub fn with_time_gap(mut self, time_gap: f64) -> Self {
        self.time_gap = Some(time_gap);
        self.distance = None; // Mutually exclusive
        self
    }

    /// Set whether to use freespace measurement
    pub fn with_freespace(mut self, freespace: bool) -> Self {
        self.freespace = freespace;
        self
    }

    /// Set whether the action is continuous
    pub fn continuous(mut self, continuous: bool) -> Self {
        self.continuous = continuous;
        self
    }
}

impl ActionBuilder for LongitudinalDistanceActionBuilder {
    fn build_action(self) -> BuilderResult<PrivateAction> {
        self.validate()?;

        let action = LongitudinalDistanceAction {
            entity_ref: OSString::literal(self.target_entity.unwrap()),
            distance: self.distance.map(Double::literal),
            time_gap: self.time_gap.map(Double::literal),
            coordinate_system: None,
            displacement: None,
            freespace: Some(Boolean::literal(self.freespace)),
            continuous: Boolean::literal(self.continuous),
            dynamic_constraints: None,
        };

        Ok(PrivateAction::LongitudinalAction(LongitudinalAction {
            longitudinal_action_choice: LongitudinalActionChoice::LongitudinalDistanceAction(
                action,
            ),
        }))
    }

    fn validate(&self) -> BuilderResult<()> {
        if self.target_entity.is_none() {
            return Err(BuilderError::validation_error(
                "Target entity is required for longitudinal distance action",
            ));
        }

        if self.distance.is_none() && self.time_gap.is_none() {
            return Err(BuilderError::validation_error(
                "Either distance or time_gap must be specified",
            ));
        }

        Ok(())
    }
}

impl ManeuverAction for LongitudinalDistanceActionBuilder {
    fn entity_ref(&self) -> Option<&str> {
        self.entity_ref.as_deref()
    }
}

/// Builder for speed profile actions
#[derive(Debug, Default)]
pub struct SpeedProfileActionBuilder {
    entity_ref: Option<String>,
    entries: Vec<SpeedProfileEntry>,
}

impl SpeedProfileActionBuilder {
    /// Create new speed profile action builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Set target entity for this action
    pub fn for_entity(mut self, entity_ref: &str) -> Self {
        self.entity_ref = Some(entity_ref.to_string());
        self
    }

    /// Add a speed profile entry using builder pattern
    pub fn add_entry(self) -> SpeedProfileEntryBuilder {
        SpeedProfileEntryBuilder::new(self)
    }

    /// Add a speed profile entry directly (convenience method)
    pub fn add_entry_direct(mut self, time: f64, speed: f64) -> Self {
        self.entries.push(SpeedProfileEntry {
            time: Double::literal(time),
            speed: Double::literal(speed),
        });
        self
    }
}

impl ActionBuilder for SpeedProfileActionBuilder {
    fn build_action(self) -> BuilderResult<PrivateAction> {
        self.validate()?;

        let action = SpeedProfileAction {
            entity_ref: self
                .entity_ref
                .as_ref()
                .map(|s| OSString::literal(s.clone())),
            entries: self.entries,
            dynamic_constraints: None,
        };

        Ok(PrivateAction::LongitudinalAction(LongitudinalAction {
            longitudinal_action_choice: LongitudinalActionChoice::SpeedProfileAction(action),
        }))
    }

    fn validate(&self) -> BuilderResult<()> {
        if self.entries.len() < 2 {
            return Err(BuilderError::validation_error(
                "Speed profile requires at least 2 entries",
            ));
        }

        // Verify chronological order
        for i in 1..self.entries.len() {
            let prev_time = self.entries[i - 1].time.as_literal().unwrap();
            let curr_time = self.entries[i].time.as_literal().unwrap();
            if curr_time <= prev_time {
                return Err(BuilderError::validation_error(
                    "Speed profile entries must be in chronological order",
                ));
            }
        }

        Ok(())
    }
}

impl ManeuverAction for SpeedProfileActionBuilder {
    fn entity_ref(&self) -> Option<&str> {
        self.entity_ref.as_deref()
    }
}

/// Builder for individual speed profile entries
pub struct SpeedProfileEntryBuilder {
    parent: SpeedProfileActionBuilder,
    time: Option<f64>,
    speed: Option<f64>,
}

impl SpeedProfileEntryBuilder {
    fn new(parent: SpeedProfileActionBuilder) -> Self {
        Self {
            parent,
            time: None,
            speed: None,
        }
    }

    /// Set the time for this entry
    pub fn at_time(mut self, time: f64) -> Self {
        self.time = Some(time);
        self
    }

    /// Set the speed for this entry
    pub fn with_speed(mut self, speed: f64) -> Self {
        self.speed = Some(speed);
        self
    }

    /// Finish building this entry and return to parent builder
    pub fn finish(mut self) -> BuilderResult<SpeedProfileActionBuilder> {
        if self.time.is_none() {
            return Err(BuilderError::validation_error("Entry time is required"));
        }
        if self.speed.is_none() {
            return Err(BuilderError::validation_error("Entry speed is required"));
        }

        let entry = SpeedProfileEntry {
            time: Double::literal(self.time.unwrap()),
            speed: Double::literal(self.speed.unwrap()),
        };

        self.parent.entries.push(entry);
        Ok(self.parent)
    }
}

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

    #[test]
    fn test_longitudinal_distance_basic() {
        let builder = LongitudinalDistanceActionBuilder::new()
            .for_entity("ego")
            .from_entity("lead")
            .at_distance(10.0)
            .build_action()
            .unwrap();

        // Verify structure
        match builder {
            PrivateAction::LongitudinalAction(ref action) => {
                match &action.longitudinal_action_choice {
                    LongitudinalActionChoice::LongitudinalDistanceAction(ref dist) => {
                        assert_eq!(dist.distance.as_ref().unwrap().as_literal(), Some(&10.0));
                        assert_eq!(dist.entity_ref.as_literal(), Some(&"lead".to_string()));
                    }
                    _ => panic!("Expected LongitudinalDistanceAction"),
                }
            }
            _ => panic!("Expected LongitudinalAction"),
        }
    }

    #[test]
    fn test_longitudinal_distance_time_gap() {
        let builder = LongitudinalDistanceActionBuilder::new()
            .for_entity("ego")
            .from_entity("lead")
            .with_time_gap(2.5)
            .build_action()
            .unwrap();

        match builder {
            PrivateAction::LongitudinalAction(ref action) => {
                match &action.longitudinal_action_choice {
                    LongitudinalActionChoice::LongitudinalDistanceAction(ref dist) => {
                        assert_eq!(dist.time_gap.as_ref().unwrap().as_literal(), Some(&2.5));
                        assert!(dist.distance.is_none());
                    }
                    _ => panic!("Expected LongitudinalDistanceAction"),
                }
            }
            _ => panic!("Expected LongitudinalAction"),
        }
    }

    #[test]
    fn test_validation_requires_target() {
        let result = LongitudinalDistanceActionBuilder::new()
            .at_distance(10.0)
            .build_action();

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Target entity"));
    }

    #[test]
    fn test_validation_requires_distance_or_time_gap() {
        let result = LongitudinalDistanceActionBuilder::new()
            .from_entity("lead")
            .build_action();

        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("distance or time_gap"));
    }

    #[test]
    fn test_speed_profile_basic() {
        let builder = SpeedProfileActionBuilder::new()
            .for_entity("ego")
            .add_entry_direct(0.0, 0.0)
            .add_entry_direct(5.0, 30.0)
            .add_entry_direct(10.0, 50.0)
            .build_action()
            .unwrap();

        match builder {
            PrivateAction::LongitudinalAction(ref action) => {
                match &action.longitudinal_action_choice {
                    LongitudinalActionChoice::SpeedProfileAction(ref profile) => {
                        assert_eq!(profile.entries.len(), 3);
                        assert_eq!(profile.entries[0].time.as_literal(), Some(&0.0));
                        assert_eq!(profile.entries[0].speed.as_literal(), Some(&0.0));
                        assert_eq!(profile.entries[2].time.as_literal(), Some(&10.0));
                        assert_eq!(profile.entries[2].speed.as_literal(), Some(&50.0));
                    }
                    _ => panic!("Expected SpeedProfileAction"),
                }
            }
            _ => panic!("Expected LongitudinalAction"),
        }
    }

    #[test]
    fn test_speed_profile_entry_builder() {
        let builder = SpeedProfileActionBuilder::new()
            .for_entity("ego")
            .add_entry()
            .at_time(0.0)
            .with_speed(0.0)
            .finish()
            .unwrap()
            .add_entry()
            .at_time(5.0)
            .with_speed(25.0)
            .finish()
            .unwrap()
            .build_action()
            .unwrap();

        match builder {
            PrivateAction::LongitudinalAction(ref action) => {
                match &action.longitudinal_action_choice {
                    LongitudinalActionChoice::SpeedProfileAction(ref profile) => {
                        assert_eq!(profile.entries.len(), 2);
                    }
                    _ => panic!("Expected SpeedProfileAction"),
                }
            }
            _ => panic!("Expected LongitudinalAction"),
        }
    }

    #[test]
    fn test_speed_profile_validation_min_entries() {
        let result = SpeedProfileActionBuilder::new()
            .for_entity("ego")
            .add_entry_direct(0.0, 0.0)
            .build_action();

        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("at least 2 entries"));
    }

    #[test]
    fn test_speed_profile_validation_chronological() {
        let result = SpeedProfileActionBuilder::new()
            .for_entity("ego")
            .add_entry_direct(5.0, 30.0)
            .add_entry_direct(3.0, 20.0) // Out of order
            .build_action();

        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("chronological order"));
    }

    #[test]
    fn test_maneuver_action_trait() {
        let builder = LongitudinalDistanceActionBuilder::new().for_entity("ego");
        assert_eq!(builder.entity_ref(), Some("ego"));

        let profile_builder = SpeedProfileActionBuilder::new().for_entity("test_entity");
        assert_eq!(profile_builder.entity_ref(), Some("test_entity"));
    }
}