Skip to main content

openscenario_rs/types/
mod.rs

1//! OpenSCENARIO type system
2//!
3//! This module organizes all OpenSCENARIO data types into logical categories,
4//! provides common traits and utilities, and establishes the foundation for
5//! the complete type system covering 347+ specification types.
6
7use std::collections::HashMap;
8
9// Core type modules
10pub mod basic;
11pub mod enums;
12
13// Entity and object types
14pub mod entities;
15pub mod geometry;
16
17// Scenario structure
18pub mod scenario;
19
20// Actions and conditions (to be expanded)
21pub mod actions;
22pub mod conditions;
23pub mod positions;
24
25// Environment and world modeling (to be expanded)
26pub mod environment;
27
28// Advanced features (to be expanded)
29pub mod catalogs;
30pub mod controllers;
31pub mod distributions;
32
33// Road network types
34pub mod road;
35
36// Routing and navigation types
37pub mod routing;
38
39// Re-export commonly used types for convenience
40pub use basic::{
41    Boolean, Directory, Double, Int, OSString, ParameterDeclaration, ParameterDeclarations, Range,
42    UnsignedInt, UnsignedShort, Value, ValueConstraint, ValueConstraintGroup,
43};
44pub use enums::{
45    AngleType, AutomaticGearType, ColorType, ConditionEdge, ControllerType, DirectionalDimension,
46    DynamicsDimension, DynamicsShape, FractionalCloudCover, LightMode, MiscObjectCategory,
47    ObjectType, ParameterType, PedestrianCategory, PedestrianGestureType, PedestrianMotionType,
48    PrecipitationType, Priority, Role, RouteStrategy, RoutingAlgorithm, Rule,
49    TriggeringEntitiesRule, VehicleCategory, VehicleComponentType, VehicleLightType, Wetness,
50};
51pub use environment::{Environment, Fog, Precipitation, RoadCondition, Sun, TimeOfDay, Weather};
52pub use scenario::init::{
53    Actions, EnvironmentAction, GlobalAction, Init, LongitudinalAction, Private, PrivateAction,
54};
55pub use scenario::storyboard::{
56    CatalogDefinition, FileHeader, OpenScenario, OpenScenarioDocumentType, ScenarioDefinition,
57    Storyboard,
58};
59
60// Re-export distribution types
61pub use distributions::{
62    DeterministicMultiParameterDistribution,
63    // Deterministic types
64    DeterministicParameterDistribution,
65    DeterministicSingleParameterDistribution,
66    DeterministicSingleParameterDistributionType,
67    DistributionDefinition,
68    DistributionRange,
69    DistributionSampler,
70    DistributionSet,
71    DistributionSetElement,
72    Histogram,
73    HistogramBin,
74    LogNormalDistribution,
75    NormalDistribution,
76    ParameterAssignment,
77    ParameterValueDistribution,
78    ParameterValueSet,
79    PoissonDistribution,
80    ProbabilityDistributionSet,
81    ProbabilityDistributionSetElement,
82    // Stochastic types
83    StochasticDistribution,
84    StochasticDistributionType,
85    UniformDistribution,
86    UserDefinedDistribution,
87    ValidateDistribution,
88    ValueSetDistribution,
89};
90
91// Re-export controller types
92pub use controllers::{
93    ActivateControllerAction, Controller, ControllerAssignment, ControllerDistribution,
94    ControllerProperties, ObjectController, OverrideControllerValueAction,
95};
96
97// Re-export catalog location types
98pub use catalogs::locations::{
99    CatalogLocations, ControllerCatalogLocation, EnvironmentCatalogLocation,
100    ManeuverCatalogLocation, MiscObjectCatalogLocation, PedestrianCatalogLocation,
101    RouteCatalogLocation, TrajectoryCatalogLocation, VehicleCatalogLocation,
102};
103
104// Re-export condition types
105pub use conditions::{
106    AccelerationCondition, ByEntityCondition, ByValueCondition, DistanceCondition,
107    ReachPositionCondition, RelativeDistanceCondition, SimulationTimeCondition, SpeedCondition,
108    StandStillCondition,
109};
110
111// Re-export routing types
112pub use routing::{Route, RouteRef, Waypoint};
113
114// Re-export entity types
115pub use entities::{Axle, Axles, Entities, Pedestrian, ScenarioObject, Vehicle};
116
117/// Common trait for types that support validation
118///
119/// This trait will be implemented by all major OpenSCENARIO types to enable
120/// comprehensive validation of scenario documents.
121pub trait Validate {
122    /// Validate this object using the provided validation context
123    fn validate(&self, ctx: &ValidationContext) -> crate::Result<()>;
124}
125
126/// Common trait for types that support parameter resolution
127///
128/// This trait enables resolution of ${parameter} references to their actual values
129/// using a parameter context containing the parameter definitions.
130pub trait Resolve<T> {
131    /// Resolve any parameters in this object using the provided context
132    fn resolve(&self, ctx: &ParameterContext) -> crate::Result<T>;
133}
134
135/// Context for validation operations
136///
137/// Contains references to entities, catalogs, and other global state needed
138/// for validating cross-references and constraints.
139#[derive(Debug, Default)]
140pub struct ValidationContext {
141    /// Registry of all entities in the scenario for reference validation
142    pub entities: HashMap<String, EntityRef>,
143    /// Available catalog entries for catalog reference validation  
144    pub catalogs: HashMap<String, CatalogRef>,
145    /// Validation settings and options
146    pub strict_mode: bool,
147}
148
149impl ValidationContext {
150    /// Create a new validation context
151    pub fn new() -> Self {
152        Self::default()
153    }
154
155    /// Enable strict validation mode
156    pub fn with_strict_mode(mut self) -> Self {
157        self.strict_mode = true;
158        self
159    }
160
161    /// Add an entity to the validation registry
162    pub fn add_entity(&mut self, name: String, entity_ref: EntityRef) {
163        self.entities.insert(name, entity_ref);
164    }
165}
166
167/// Context for parameter resolution
168///
169/// Contains the parameter values and scope information needed to resolve
170/// ${parameter} references to their actual values.
171#[derive(Debug, Default)]
172pub struct ParameterContext {
173    /// Current parameter values by name
174    pub parameters: HashMap<String, String>,
175    /// Parameter declaration scope (for nested parameter sets)
176    pub scope: Vec<String>,
177}
178
179impl ParameterContext {
180    /// Create a new parameter context
181    pub fn new() -> Self {
182        Self::default()
183    }
184
185    /// Add a parameter value
186    pub fn with_parameter(mut self, name: String, value: String) -> Self {
187        self.parameters.insert(name, value);
188        self
189    }
190
191    /// Get a parameter value by name
192    pub fn get(&self, name: &str) -> Option<&str> {
193        self.parameters.get(name).map(|s| s.as_str())
194    }
195}
196
197/// Reference to an entity in the scenario
198#[derive(Debug, Clone)]
199pub struct EntityRef {
200    /// Entity name
201    pub name: String,
202    /// Entity type
203    pub object_type: ObjectType,
204}
205
206/// Reference to a catalog entry  
207#[derive(Debug, Clone)]
208pub struct CatalogRef {
209    /// Catalog name
210    pub catalog: String,
211    /// Entry name within the catalog
212    pub entry: String,
213}
214
215// Default implementations for basic types
216impl<T> Validate for Value<T> {
217    fn validate(&self, _ctx: &ValidationContext) -> crate::Result<()> {
218        // Basic Value<T> types don't need validation beyond their own constraints
219        Ok(())
220    }
221}
222
223impl<T: Clone> Resolve<T> for Value<T>
224where
225    T: std::str::FromStr,
226    T::Err: std::fmt::Display,
227{
228    fn resolve(&self, ctx: &ParameterContext) -> crate::Result<T> {
229        self.resolve(&ctx.parameters)
230    }
231}