maliput_sys/api/rules/
mod.rs

1// BSD 3-Clause License
2//
3// Copyright (c) 2024, Woven by Toyota.
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are met:
8//
9// * Redistributions of source code must retain the above copyright notice, this
10//   list of conditions and the following disclaimer.
11//
12// * Redistributions in binary form must reproduce the above copyright notice,
13//   this list of conditions and the following disclaimer in the documentation
14//   and/or other materials provided with the distribution.
15//
16// * Neither the name of the copyright holder nor the names of its
17//   contributors may be used to endorse or promote products derived from
18//   this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31#[cxx::bridge(namespace = "maliput::api::rules")]
32#[allow(clippy::needless_lifetimes)] // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/5787
33pub mod ffi {
34    /// Shared struct for `TrafficLight` pointers.
35    /// This is needed because `*const` can't be used directly in the CxxVector collection.
36    struct ConstTrafficLightPtr {
37        pub traffic_light: *const TrafficLight,
38    }
39    /// Shared struct for `BulbGroup` pointers.
40    /// This is needed because `*const` can't be used directly in the CxxVector collection.
41    struct ConstBulbGroupPtr {
42        pub bulb_group: *const BulbGroup,
43    }
44    /// Shared struct for `Bulb` pointers.
45    /// This is needed because `*const` can't be used directly in the CxxVector collection.
46    struct ConstBulbPtr {
47        pub bulb: *const Bulb,
48    }
49    /// Shared struct for `BulbState` references.
50    /// This is needed because `&f` can't be used directly in the CxxVector collection.
51    struct ConstBulbStateRef<'a> {
52        pub bulb_state: &'a BulbState,
53    }
54    /// Shared struct for floats types.
55    /// This is needed because `f64` can't be used directly in the UniquePtr type.
56    struct FloatWrapper {
57        pub value: f64,
58    }
59    /// Shared struct for pairs in a RelatedRules collection.
60    ///  - key: Group name of the rules.
61    ///  - value: Rule ids.
62    /// This is needed because maps can't be bound directly.
63    struct RelatedRule {
64        pub group_name: String,
65        pub rule_ids: Vec<String>,
66    }
67    /// Shared struct for pairs in a RelatedRules collection.
68    ///  - key: Group name.
69    ///  - value: Unique Ids.
70    /// This is needed because maps can't be bound directly.
71    struct RelatedUniqueId {
72        pub group_name: String,
73        pub unique_ids: Vec<String>,
74    }
75    /// Shared struct for pairs in a DiscreteValueRules collection.
76    ///  - key: Rule type ids.
77    ///  - value: Discrete Values.
78    /// This is needed because maps can't be bound directly.
79    struct DiscreteValueRuleType {
80        pub type_id: String,
81        pub values: UniquePtr<CxxVector<DiscreteValueRuleDiscreteValue>>,
82    }
83    /// Shared struct for pairs in a DiscreteValues collection.
84    ///  - key: Rule ids.
85    ///  - value: Discrete Value State.
86    /// This is needed because maps can't be bound directly.
87    struct DiscreteValueRuleState {
88        pub rule_id: String,
89        pub state: UniquePtr<DiscreteValueRuleDiscreteValue>,
90    }
91    /// Shared struct for pairs in a RangeValueRules collection.
92    ///  - key: Rule type ids.
93    ///  - value: Range Values.
94    /// This is needed because maps can't be bound directly.
95    struct RangeValueRuleType {
96        pub type_id: String,
97        pub values: UniquePtr<CxxVector<RangeValueRuleRange>>,
98    }
99
100    /// Shared struct for `LaneSRange` constant reference.
101    /// Interestingly this was done at maliput::api module but
102    /// couldn't reference to that so it was necessary to
103    /// redefine it here.
104    struct ConstLaneSRangeRef<'a> {
105        pub lane_s_range: &'a LaneSRange,
106    }
107
108    /// Shared struct for a `NextPhase` in a `PhaseRing`.
109    ///  - phase_id: ID of the `NextPhase`.
110    ///  - duration_until: Optional field to suggest a default duration of the current `Phase`
111    ///    until the `NextPhase`.
112    /// Redefinition is necessary since std::optional<T> isn't supported.
113    struct NextPhase {
114        pub phase_id: String,
115        pub duration_until: UniquePtr<FloatWrapper>,
116    }
117
118    struct DiscreteValueNextState {
119        pub state: UniquePtr<DiscreteValueRuleDiscreteValue>,
120        pub duration_until: UniquePtr<FloatWrapper>,
121    }
122
123    struct RangeValueNextState {
124        pub state: UniquePtr<RangeValueRuleRange>,
125        pub duration_until: UniquePtr<FloatWrapper>,
126    }
127
128    #[repr(i32)]
129    enum BulbColor {
130        kRed = 0,
131        kYellow,
132        kGreen,
133    }
134
135    #[repr(i32)]
136    enum BulbType {
137        kRound = 0,
138        kArrow,
139    }
140
141    #[repr(i32)]
142    enum BulbState {
143        kOff = 0,
144        kOn,
145        kBlinking,
146    }
147
148    unsafe extern "C++" {
149        include!("api/rules/rules.h");
150        include!("api/rules/aliases.h");
151        include!("cxx_utils/error_handling.h");
152
153        // Forward declarations
154        #[namespace = "maliput::api"]
155        type InertialPosition = crate::api::ffi::InertialPosition;
156        #[namespace = "maliput::api"]
157        type Rotation = crate::api::ffi::Rotation;
158        #[namespace = "maliput::api"]
159        type LaneSRange = crate::api::ffi::LaneSRange;
160        #[namespace = "maliput::api"]
161        type LaneSRoute = crate::api::ffi::LaneSRoute;
162        #[namespace = "maliput::api"]
163        type RoadPosition = crate::api::ffi::RoadPosition;
164        #[namespace = "maliput::math"]
165        type Vector3 = crate::math::ffi::Vector3;
166
167        // TrafficLightBook bindings definitions.
168        type TrafficLightBook;
169        fn TrafficLightBook_TrafficLights(book: &TrafficLightBook) -> UniquePtr<CxxVector<ConstTrafficLightPtr>>;
170        fn TrafficLightBook_GetTrafficLight(book: &TrafficLightBook, id: &String) -> *const TrafficLight;
171
172        // TrafficLight bindings definitions.
173        type TrafficLight;
174        fn TrafficLight_id(traffic_light: &TrafficLight) -> String;
175        fn TrafficLight_position_road_network(traffic_light: &TrafficLight) -> UniquePtr<InertialPosition>;
176        fn TrafficLight_orientation_road_network(traffic_light: &TrafficLight) -> UniquePtr<Rotation>;
177        fn TrafficLight_bulb_groups(traffic_light: &TrafficLight) -> UniquePtr<CxxVector<ConstBulbGroupPtr>>;
178        fn TrafficLight_GetBulbGroup(traffic_light: &TrafficLight, id: &String) -> *const BulbGroup;
179
180        type BulbColor;
181        type BulbState;
182        type BulbType;
183        // Bulb bindings definitions.
184        type Bulb;
185        fn Bulb_id(bulb: &Bulb) -> String;
186        fn Bulb_unique_id(bulb: &Bulb) -> UniquePtr<UniqueBulbId>;
187        fn Bulb_position_bulb_group(bulb: &Bulb) -> UniquePtr<InertialPosition>;
188        fn Bulb_orientation_bulb_group(bulb: &Bulb) -> UniquePtr<Rotation>;
189        fn color(self: &Bulb) -> &BulbColor;
190        // We can't automatically use the name `type` as it is a reserved keyword in Rust.
191        fn Bulb_type(bulb: &Bulb) -> &BulbType;
192        fn Bulb_arrow_orientation_rad(bulb: &Bulb) -> UniquePtr<FloatWrapper>;
193        fn Bulb_states(bulb: &Bulb) -> UniquePtr<CxxVector<BulbState>>;
194        fn GetDefaultState(self: &Bulb) -> BulbState;
195        fn IsValidState(self: &Bulb, state: &BulbState) -> bool;
196        fn Bulb_bounding_box_min(bulb: &Bulb) -> UniquePtr<Vector3>;
197        fn Bulb_bounding_box_max(bulb: &Bulb) -> UniquePtr<Vector3>;
198        fn Bulb_bulb_group(bulb: &Bulb) -> *const BulbGroup;
199
200        // BulbGroup bindings definitions.
201        type BulbGroup;
202        fn BulbGroup_id(bulb_group: &BulbGroup) -> String;
203        fn BulbGroup_unique_id(bulb: &BulbGroup) -> UniquePtr<UniqueBulbGroupId>;
204        fn BulbGroup_position_traffic_light(bulb_group: &BulbGroup) -> UniquePtr<InertialPosition>;
205        fn BulbGroup_orientation_traffic_light(bulb_group: &BulbGroup) -> UniquePtr<Rotation>;
206        fn BulbGroup_bulbs(bulb_group: &BulbGroup) -> UniquePtr<CxxVector<ConstBulbPtr>>;
207        fn BulbGroup_GetBulb(bulb_group: &BulbGroup, id: &String) -> *const Bulb;
208        fn BulbGroup_traffic_light(bulb_group: &BulbGroup) -> *const TrafficLight;
209
210        // UniqueBulbId bindings definitions.
211        type UniqueBulbId;
212        fn string(self: &UniqueBulbId) -> &CxxString;
213        fn UniqueBulbId_traffic_light_id(id: &UniqueBulbId) -> String;
214        fn UniqueBulbId_bulb_group_id(id: &UniqueBulbId) -> String;
215        fn UniqueBulbId_bulb_id(id: &UniqueBulbId) -> String;
216        fn UniqueBulbId_create_unique_ptr(id: &UniqueBulbId) -> UniquePtr<UniqueBulbId>;
217
218        // UniqueBulbGroupId bindings definitions.
219        type UniqueBulbGroupId;
220        fn string(self: &UniqueBulbGroupId) -> &CxxString;
221        fn UniqueBulbGroupId_traffic_light_id(id: &UniqueBulbGroupId) -> String;
222        fn UniqueBulbGroupId_bulb_group_id(id: &UniqueBulbGroupId) -> String;
223
224        // QueryResults bindings definitions.
225        type QueryResults;
226        fn QueryResults_discrete_value_rules(query_results: &QueryResults) -> Vec<String>;
227        fn QueryResults_range_value_rules(query_results: &QueryResults) -> Vec<String>;
228
229        // RoadRulebook bindings definitions.
230        type RoadRulebook;
231        fn RoadRulebook_GetDiscreteValueRule(book: &RoadRulebook, rule_id: &String) -> UniquePtr<DiscreteValueRule>;
232        fn RoadRulebook_GetRangeValueRule(book: &RoadRulebook, rule_id: &String) -> UniquePtr<RangeValueRule>;
233        fn RoadRulebook_Rules(book: &RoadRulebook) -> UniquePtr<QueryResults>;
234        #[allow(clippy::needless_lifetimes)]
235        fn RoadRulebook_FindRules(
236            book: &RoadRulebook,
237            ranges: &Vec<ConstLaneSRangeRef>,
238            tolerance: f64,
239        ) -> Result<UniquePtr<QueryResults>>;
240
241        // DiscreteValueRule::DiscreteValue bindings definitions.
242        type DiscreteValueRuleDiscreteValue;
243        fn DiscreteValueRuleDiscreteValue_value(value: &DiscreteValueRuleDiscreteValue) -> String;
244        fn DiscreteValueRuleDiscreteValue_severity(value: &DiscreteValueRuleDiscreteValue) -> i32;
245        fn DiscreteValueRuleDiscreteValue_related_rules(
246            value: &DiscreteValueRuleDiscreteValue,
247        ) -> UniquePtr<CxxVector<RelatedRule>>;
248        fn DiscreteValueRuleDiscreteValue_related_unique_ids(
249            value: &DiscreteValueRuleDiscreteValue,
250        ) -> UniquePtr<CxxVector<RelatedUniqueId>>;
251
252        // DiscreteValueRule bindings definitions.
253        type DiscreteValueRule;
254        fn states(self: &DiscreteValueRule) -> &CxxVector<DiscreteValueRuleDiscreteValue>;
255        fn DiscreteValueRule_id(rule: &DiscreteValueRule) -> String;
256        fn DiscreteValueRule_type_id(rule: &DiscreteValueRule) -> String;
257        fn DiscreteValueRule_zone(rule: &DiscreteValueRule) -> UniquePtr<LaneSRoute>;
258
259        // RangeValueRule::Range bindings definitions.
260        type RangeValueRuleRange;
261        fn RangeValueRuleRange_description(range: &RangeValueRuleRange) -> String;
262        fn RangeValueRuleRange_min(range: &RangeValueRuleRange) -> f64;
263        fn RangeValueRuleRange_max(range: &RangeValueRuleRange) -> f64;
264        fn RangeValueRuleRange_severity(range: &RangeValueRuleRange) -> i32;
265        fn RangeValueRuleRange_related_rules(range: &RangeValueRuleRange) -> UniquePtr<CxxVector<RelatedRule>>;
266        fn RangeValueRuleRange_related_unique_ids(range: &RangeValueRuleRange)
267            -> UniquePtr<CxxVector<RelatedUniqueId>>;
268        // RangeValueRule::Range bindings definitions.
269        type RangeValueRule;
270        fn RangeValueRule_id(rule: &RangeValueRule) -> String;
271        fn RangeValueRule_type_id(rule: &RangeValueRule) -> String;
272        fn RangeValueRule_zone(rule: &RangeValueRule) -> UniquePtr<LaneSRoute>;
273        fn states(self: &RangeValueRule) -> &CxxVector<RangeValueRuleRange>;
274
275        // Phase bindings definitions.
276        type Phase;
277        fn Phase_id(phase: &Phase) -> String;
278        fn Phase_discrete_value_rule_states(phase: &Phase) -> UniquePtr<CxxVector<DiscreteValueRuleState>>;
279        fn Phase_unique_bulb_ids(phase: &Phase) -> UniquePtr<CxxVector<UniqueBulbId>>;
280        fn Phase_bulb_state(phase: &Phase, bulb_id: &UniqueBulbId) -> UniquePtr<BulbState>;
281        // Helper method to implement [Phase_unique_bulb_ids] API method.
282        fn ptr_from_unique_bulb_id(id: &UniqueBulbId) -> UniquePtr<UniqueBulbId>;
283
284        // PhaseRing bindings definitions.
285        type PhaseRing;
286        fn PhaseRing_id(phase_ring: &PhaseRing) -> String;
287        fn PhaseRing_GetPhase(phase_ring: &PhaseRing, id: &String) -> UniquePtr<Phase>;
288        fn PhaseRing_phases_ids(phase_ring: &PhaseRing) -> Vec<String>;
289        fn PhaseRing_GetNextPhases(phase_ring: &PhaseRing, id: &String) -> Result<UniquePtr<CxxVector<NextPhase>>>;
290
291        // StateProviderResult<Phase::Id> bindings definitions.
292        type PhaseStateProviderQuery;
293        fn PhaseStateProvider_state(phase_state_provider: &PhaseStateProviderQuery) -> String;
294        fn PhaseStateProvider_next(phase_state_provider: &PhaseStateProviderQuery) -> UniquePtr<NextPhase>;
295
296        // PhaseProvider bindings definitions.
297        type PhaseProvider;
298        fn PhaseProvider_GetPhase(
299            phase_provider: &PhaseProvider,
300            phase_ring_id: &String,
301        ) -> UniquePtr<PhaseStateProviderQuery>;
302
303        // PhaseRingBook bindings definitions.
304        type PhaseRingBook;
305        fn PhaseRingBook_GetPhaseRingsId(book: &PhaseRingBook) -> Vec<String>;
306        fn PhaseRingBook_GetPhaseRing(book: &PhaseRingBook, id: &String) -> UniquePtr<PhaseRing>;
307        fn PhaseRingBook_FindPhaseRing(book: &PhaseRingBook, rule_id: &String) -> UniquePtr<PhaseRing>;
308
309        // RuleRegistry bindings definitions.
310        type RuleRegistry;
311        fn RuleRegistry_DiscreteValueRuleTypes(registry: &RuleRegistry) -> UniquePtr<CxxVector<DiscreteValueRuleType>>;
312        fn RuleRegistry_RangeValueRuleTypes(registry: &RuleRegistry) -> UniquePtr<CxxVector<RangeValueRuleType>>;
313
314        // DiscreteValueRuleStateProviderQuery bindings definitions.
315        type DiscreteValueRuleStateProviderQuery;
316        fn DiscreteValueRuleStateProviderQuery_state(
317            state_provider_query: &DiscreteValueRuleStateProviderQuery,
318        ) -> UniquePtr<DiscreteValueRuleDiscreteValue>;
319        fn DiscreteValueRuleStateProviderQuery_next(
320            state_provider_query: &DiscreteValueRuleStateProviderQuery,
321        ) -> UniquePtr<DiscreteValueNextState>;
322
323        // DiscreteValueRuleStateProvider bindings definitions.
324        type DiscreteValueRuleStateProvider;
325        fn DiscreteValueRuleStateProvider_GetStateById(
326            state_provider: &DiscreteValueRuleStateProvider,
327            id: &String,
328        ) -> UniquePtr<DiscreteValueRuleStateProviderQuery>;
329        fn DiscreteValueRuleStateProvider_GetStateByType(
330            state_provider: &DiscreteValueRuleStateProvider,
331            road_position: &RoadPosition,
332            rule_type: &String,
333            tolerance: f64,
334        ) -> UniquePtr<DiscreteValueRuleStateProviderQuery>;
335
336        // RangeValueRuleStateProviderQuery bindings definitions.
337        type RangeValueRuleStateProviderQuery;
338        fn RangeValueRuleStateProviderQuery_state(
339            state_provider_query: &RangeValueRuleStateProviderQuery,
340        ) -> UniquePtr<RangeValueRuleRange>;
341        fn RangeValueRuleStateProviderQuery_next(
342            state_provider_query: &RangeValueRuleStateProviderQuery,
343        ) -> UniquePtr<RangeValueNextState>;
344
345        // RangeValueRuleStateProvider bindings definitions.
346        type RangeValueRuleStateProvider;
347        fn RangeValueRuleStateProvider_GetStateById(
348            state_provider: &RangeValueRuleStateProvider,
349            id: &String,
350        ) -> UniquePtr<RangeValueRuleStateProviderQuery>;
351        fn RangeValueRuleStateProvider_GetStateByType(
352            state_provider: &RangeValueRuleStateProvider,
353            road_position: &RoadPosition,
354            rule_type: &String,
355            tolerance: f64,
356        ) -> UniquePtr<RangeValueRuleStateProviderQuery>;
357    }
358}