Skip to main content

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 `TrafficSign` pointers.
50    /// This is needed because `*const` can't be used directly in the CxxVector collection.
51    struct ConstTrafficSignPtr {
52        pub traffic_sign: *const TrafficSign,
53    }
54    /// Shared struct for `BulbState` references.
55    /// This is needed because `&f` can't be used directly in the CxxVector collection.
56    struct ConstBulbStateRef<'a> {
57        pub bulb_state: &'a BulbState,
58    }
59    /// Shared struct for floats types.
60    /// This is needed because `f64` can't be used directly in the UniquePtr type.
61    struct FloatWrapper {
62        pub value: f64,
63    }
64    /// Shared struct for optional string types.
65    /// This is needed because `String` can't be used directly in the UniquePtr type.
66    /// A null `UniquePtr<StringWrapper>` represents `std::nullopt`.
67    struct StringWrapper {
68        pub value: String,
69    }
70    /// Shared struct for pairs in a RelatedRules collection.
71    ///  - key: Group name of the rules.
72    ///  - value: Rule ids.
73    /// This is needed because maps can't be bound directly.
74    struct RelatedRule {
75        pub group_name: String,
76        pub rule_ids: Vec<String>,
77    }
78    /// Shared struct for pairs in a RelatedRules collection.
79    ///  - key: Group name.
80    ///  - value: Unique Ids.
81    /// This is needed because maps can't be bound directly.
82    struct RelatedUniqueId {
83        pub group_name: String,
84        pub unique_ids: Vec<String>,
85    }
86    /// Shared struct for pairs in a DiscreteValueRules collection.
87    ///  - key: Rule type ids.
88    ///  - value: Discrete Values.
89    /// This is needed because maps can't be bound directly.
90    struct DiscreteValueRuleType {
91        pub type_id: String,
92        pub values: UniquePtr<CxxVector<DiscreteValueRuleDiscreteValue>>,
93    }
94    /// Shared struct for pairs in a DiscreteValues collection.
95    ///  - key: Rule ids.
96    ///  - value: Discrete Value State.
97    /// This is needed because maps can't be bound directly.
98    struct DiscreteValueRuleState {
99        pub rule_id: String,
100        pub state: UniquePtr<DiscreteValueRuleDiscreteValue>,
101    }
102    /// Shared struct for pairs in a RangeValueRules collection.
103    ///  - key: Rule type ids.
104    ///  - value: Range Values.
105    /// This is needed because maps can't be bound directly.
106    struct RangeValueRuleType {
107        pub type_id: String,
108        pub values: UniquePtr<CxxVector<RangeValueRuleRange>>,
109    }
110
111    /// Shared struct for `LaneSRange` constant reference.
112    /// Interestingly this was done at maliput::api module but
113    /// couldn't reference to that so it was necessary to
114    /// redefine it here.
115    struct ConstLaneSRangeRef<'a> {
116        pub lane_s_range: &'a LaneSRange,
117    }
118
119    /// Shared struct for a `NextPhase` in a `PhaseRing`.
120    ///  - phase_id: ID of the `NextPhase`.
121    ///  - duration_until: Optional field to suggest a default duration of the current `Phase`
122    ///    until the `NextPhase`.
123    /// Redefinition is necessary since std::optional<T> isn't supported.
124    struct NextPhase {
125        pub phase_id: String,
126        pub duration_until: UniquePtr<FloatWrapper>,
127    }
128
129    struct DiscreteValueNextState {
130        pub state: UniquePtr<DiscreteValueRuleDiscreteValue>,
131        pub duration_until: UniquePtr<FloatWrapper>,
132    }
133
134    struct RangeValueNextState {
135        pub state: UniquePtr<RangeValueRuleRange>,
136        pub duration_until: UniquePtr<FloatWrapper>,
137    }
138
139    #[repr(i32)]
140    enum BulbColor {
141        kRed = 0,
142        kYellow,
143        kGreen,
144    }
145
146    #[repr(i32)]
147    enum BulbType {
148        kRound = 0,
149        kArrow,
150        kArrowLeft,
151        kArrowRight,
152        kArrowUp,
153        kArrowUpperLeft,
154        kArrowUpperRight,
155        kUTurnLeft,
156        kUTurnRight,
157        kWalk,
158        kDontWalk,
159    }
160
161    #[repr(i32)]
162    enum BulbState {
163        kOff = 0,
164        kOn,
165        kBlinking,
166    }
167
168    #[repr(i32)]
169    enum TrafficSignType {
170        kStop = 0,
171        kYield,
172        kSpeedLimit,
173        kNoEntry,
174        kOneWay,
175        kPedestrianCrossing,
176        kNoLeftTurn,
177        kNoRightTurn,
178        kNoUTurn,
179        kSchoolZone,
180        kConstruction,
181        kRailroadCrossing,
182        kNoOvertaking,
183        kUnknown,
184    }
185
186    unsafe extern "C++" {
187        include!("api/rules/rules.h");
188        include!("api/rules/aliases.h");
189        include!("cxx_utils/error_handling.h");
190
191        // Forward declarations
192        #[namespace = "maliput::api"]
193        type InertialPosition = crate::api::ffi::InertialPosition;
194        #[namespace = "maliput::api"]
195        type Rotation = crate::api::ffi::Rotation;
196        #[namespace = "maliput::api"]
197        type LaneSRange = crate::api::ffi::LaneSRange;
198        #[namespace = "maliput::api"]
199        type LaneSRoute = crate::api::ffi::LaneSRoute;
200        #[namespace = "maliput::api"]
201        type RoadPosition = crate::api::ffi::RoadPosition;
202        #[namespace = "maliput::math"]
203        type Vector3 = crate::math::ffi::Vector3;
204        #[namespace = "maliput::math"]
205        type RollPitchYaw = crate::math::ffi::RollPitchYaw;
206        #[namespace = "maliput::math"]
207        type BoundingBox = crate::math::ffi::BoundingBox;
208
209        // TrafficLightBook bindings definitions.
210        type TrafficLightBook;
211        fn TrafficLightBook_TrafficLights(book: &TrafficLightBook) -> UniquePtr<CxxVector<ConstTrafficLightPtr>>;
212        fn TrafficLightBook_GetTrafficLight(book: &TrafficLightBook, id: &String) -> *const TrafficLight;
213        fn TrafficLightBook_FindByLane(
214            book: &TrafficLightBook,
215            lane_id: &String,
216        ) -> UniquePtr<CxxVector<ConstTrafficLightPtr>>;
217
218        // TrafficLight bindings definitions.
219        type TrafficLight;
220        fn TrafficLight_id(traffic_light: &TrafficLight) -> String;
221        fn TrafficLight_position_road_network(traffic_light: &TrafficLight) -> UniquePtr<InertialPosition>;
222        fn TrafficLight_orientation_road_network(traffic_light: &TrafficLight) -> UniquePtr<Rotation>;
223        fn TrafficLight_bulb_groups(traffic_light: &TrafficLight) -> UniquePtr<CxxVector<ConstBulbGroupPtr>>;
224        fn TrafficLight_GetBulbGroup(traffic_light: &TrafficLight, id: &String) -> *const BulbGroup;
225        fn TrafficLight_related_lanes(traffic_light: &TrafficLight) -> Vec<String>;
226
227        // TrafficSignBook bindings definitions.
228        type TrafficSignBook;
229        type TrafficSignType;
230        fn TrafficSignBook_TrafficSigns(book: &TrafficSignBook) -> UniquePtr<CxxVector<ConstTrafficSignPtr>>;
231        fn TrafficSignBook_GetTrafficSign(book: &TrafficSignBook, id: &String) -> *const TrafficSign;
232        fn TrafficSignBook_FindByLane(
233            book: &TrafficSignBook,
234            lane_id: &String,
235        ) -> UniquePtr<CxxVector<ConstTrafficSignPtr>>;
236        fn TrafficSignBook_FindByType(
237            book: &TrafficSignBook,
238            sign_type: TrafficSignType,
239        ) -> UniquePtr<CxxVector<ConstTrafficSignPtr>>;
240
241        // TrafficSign bindings definitions.
242        type TrafficSign;
243        fn TrafficSign_id(sign: &TrafficSign) -> String;
244        // This method could be bound as `fn type(self: &TrafficSign) -> TrafficSignType` but it causes a conflict with the `type` keyword in Rust.
245        fn TrafficSign_type(sign: &TrafficSign) -> &TrafficSignType;
246        fn TrafficSign_position_road_network(sign: &TrafficSign) -> UniquePtr<InertialPosition>;
247        fn TrafficSign_orientation_road_network(sign: &TrafficSign) -> UniquePtr<Rotation>;
248        fn TrafficSign_message(sign: &TrafficSign) -> UniquePtr<StringWrapper>;
249        fn TrafficSign_related_lanes(sign: &TrafficSign) -> Vec<String>;
250        fn TrafficSign_bounding_box(sign: &TrafficSign) -> UniquePtr<BoundingBox>;
251
252        type BulbColor;
253        type BulbState;
254        type BulbType;
255        // Bulb bindings definitions.
256        type Bulb;
257        fn Bulb_id(bulb: &Bulb) -> String;
258        fn Bulb_unique_id(bulb: &Bulb) -> UniquePtr<UniqueBulbId>;
259        fn Bulb_position_bulb_group(bulb: &Bulb) -> UniquePtr<InertialPosition>;
260        fn Bulb_orientation_bulb_group(bulb: &Bulb) -> UniquePtr<Rotation>;
261        fn color(self: &Bulb) -> &BulbColor;
262        // We can't automatically use the name `type` as it is a reserved keyword in Rust.
263        fn Bulb_type(bulb: &Bulb) -> &BulbType;
264        fn Bulb_arrow_orientation_rad(bulb: &Bulb) -> UniquePtr<FloatWrapper>;
265        fn Bulb_states(bulb: &Bulb) -> UniquePtr<CxxVector<BulbState>>;
266        fn GetDefaultState(self: &Bulb) -> BulbState;
267        fn IsValidState(self: &Bulb, state: &BulbState) -> bool;
268        fn Bulb_bounding_box_min(bulb: &Bulb) -> UniquePtr<Vector3>;
269        fn Bulb_bounding_box_max(bulb: &Bulb) -> UniquePtr<Vector3>;
270        fn Bulb_bulb_group(bulb: &Bulb) -> *const BulbGroup;
271
272        // BulbGroup bindings definitions.
273        type BulbGroup;
274        fn BulbGroup_id(bulb_group: &BulbGroup) -> String;
275        fn BulbGroup_unique_id(bulb: &BulbGroup) -> UniquePtr<UniqueBulbGroupId>;
276        fn BulbGroup_position_traffic_light(bulb_group: &BulbGroup) -> UniquePtr<InertialPosition>;
277        fn BulbGroup_orientation_traffic_light(bulb_group: &BulbGroup) -> UniquePtr<Rotation>;
278        fn BulbGroup_bulbs(bulb_group: &BulbGroup) -> UniquePtr<CxxVector<ConstBulbPtr>>;
279        fn BulbGroup_GetBulb(bulb_group: &BulbGroup, id: &String) -> *const Bulb;
280        fn BulbGroup_traffic_light(bulb_group: &BulbGroup) -> *const TrafficLight;
281
282        // UniqueBulbId bindings definitions.
283        type UniqueBulbId;
284        fn string(self: &UniqueBulbId) -> &CxxString;
285        fn UniqueBulbId_traffic_light_id(id: &UniqueBulbId) -> String;
286        fn UniqueBulbId_bulb_group_id(id: &UniqueBulbId) -> String;
287        fn UniqueBulbId_bulb_id(id: &UniqueBulbId) -> String;
288        fn UniqueBulbId_create_unique_ptr(id: &UniqueBulbId) -> UniquePtr<UniqueBulbId>;
289
290        // UniqueBulbGroupId bindings definitions.
291        type UniqueBulbGroupId;
292        fn string(self: &UniqueBulbGroupId) -> &CxxString;
293        fn UniqueBulbGroupId_traffic_light_id(id: &UniqueBulbGroupId) -> String;
294        fn UniqueBulbGroupId_bulb_group_id(id: &UniqueBulbGroupId) -> String;
295
296        // QueryResults bindings definitions.
297        type QueryResults;
298        fn QueryResults_discrete_value_rules(query_results: &QueryResults) -> Vec<String>;
299        fn QueryResults_range_value_rules(query_results: &QueryResults) -> Vec<String>;
300
301        // RoadRulebook bindings definitions.
302        type RoadRulebook;
303        fn RoadRulebook_GetDiscreteValueRule(book: &RoadRulebook, rule_id: &String) -> UniquePtr<DiscreteValueRule>;
304        fn RoadRulebook_GetRangeValueRule(book: &RoadRulebook, rule_id: &String) -> UniquePtr<RangeValueRule>;
305        fn RoadRulebook_Rules(book: &RoadRulebook) -> UniquePtr<QueryResults>;
306        #[allow(clippy::needless_lifetimes)]
307        fn RoadRulebook_FindRules(
308            book: &RoadRulebook,
309            ranges: &Vec<ConstLaneSRangeRef>,
310            tolerance: f64,
311        ) -> Result<UniquePtr<QueryResults>>;
312
313        // DiscreteValueRule::DiscreteValue bindings definitions.
314        type DiscreteValueRuleDiscreteValue;
315        fn DiscreteValueRuleDiscreteValue_value(value: &DiscreteValueRuleDiscreteValue) -> String;
316        fn DiscreteValueRuleDiscreteValue_severity(value: &DiscreteValueRuleDiscreteValue) -> i32;
317        fn DiscreteValueRuleDiscreteValue_related_rules(
318            value: &DiscreteValueRuleDiscreteValue,
319        ) -> UniquePtr<CxxVector<RelatedRule>>;
320        fn DiscreteValueRuleDiscreteValue_related_unique_ids(
321            value: &DiscreteValueRuleDiscreteValue,
322        ) -> UniquePtr<CxxVector<RelatedUniqueId>>;
323
324        // DiscreteValueRule bindings definitions.
325        type DiscreteValueRule;
326        fn states(self: &DiscreteValueRule) -> &CxxVector<DiscreteValueRuleDiscreteValue>;
327        fn DiscreteValueRule_id(rule: &DiscreteValueRule) -> String;
328        fn DiscreteValueRule_type_id(rule: &DiscreteValueRule) -> String;
329        fn DiscreteValueRule_zone(rule: &DiscreteValueRule) -> UniquePtr<LaneSRoute>;
330
331        // RangeValueRule::Range bindings definitions.
332        type RangeValueRuleRange;
333        fn RangeValueRuleRange_description(range: &RangeValueRuleRange) -> String;
334        fn RangeValueRuleRange_min(range: &RangeValueRuleRange) -> f64;
335        fn RangeValueRuleRange_max(range: &RangeValueRuleRange) -> f64;
336        fn RangeValueRuleRange_severity(range: &RangeValueRuleRange) -> i32;
337        fn RangeValueRuleRange_related_rules(range: &RangeValueRuleRange) -> UniquePtr<CxxVector<RelatedRule>>;
338        fn RangeValueRuleRange_related_unique_ids(range: &RangeValueRuleRange)
339            -> UniquePtr<CxxVector<RelatedUniqueId>>;
340        // RangeValueRule::Range bindings definitions.
341        type RangeValueRule;
342        fn RangeValueRule_id(rule: &RangeValueRule) -> String;
343        fn RangeValueRule_type_id(rule: &RangeValueRule) -> String;
344        fn RangeValueRule_zone(rule: &RangeValueRule) -> UniquePtr<LaneSRoute>;
345        fn states(self: &RangeValueRule) -> &CxxVector<RangeValueRuleRange>;
346
347        // Phase bindings definitions.
348        type Phase;
349        fn Phase_id(phase: &Phase) -> String;
350        fn Phase_discrete_value_rule_states(phase: &Phase) -> UniquePtr<CxxVector<DiscreteValueRuleState>>;
351        fn Phase_unique_bulb_ids(phase: &Phase) -> UniquePtr<CxxVector<UniqueBulbId>>;
352        fn Phase_bulb_state(phase: &Phase, bulb_id: &UniqueBulbId) -> UniquePtr<BulbState>;
353        // Helper method to implement [Phase_unique_bulb_ids] API method.
354        fn ptr_from_unique_bulb_id(id: &UniqueBulbId) -> UniquePtr<UniqueBulbId>;
355
356        // PhaseRing bindings definitions.
357        type PhaseRing;
358        fn PhaseRing_id(phase_ring: &PhaseRing) -> String;
359        fn PhaseRing_GetPhase(phase_ring: &PhaseRing, id: &String) -> UniquePtr<Phase>;
360        fn PhaseRing_phases_ids(phase_ring: &PhaseRing) -> Vec<String>;
361        fn PhaseRing_GetNextPhases(phase_ring: &PhaseRing, id: &String) -> Result<UniquePtr<CxxVector<NextPhase>>>;
362
363        // StateProviderResult<Phase::Id> bindings definitions.
364        type PhaseStateProviderQuery;
365        fn PhaseStateProvider_state(phase_state_provider: &PhaseStateProviderQuery) -> String;
366        fn PhaseStateProvider_next(phase_state_provider: &PhaseStateProviderQuery) -> UniquePtr<NextPhase>;
367
368        // PhaseProvider bindings definitions.
369        type PhaseProvider;
370        fn PhaseProvider_GetPhase(
371            phase_provider: &PhaseProvider,
372            phase_ring_id: &String,
373        ) -> UniquePtr<PhaseStateProviderQuery>;
374
375        // PhaseRingBook bindings definitions.
376        type PhaseRingBook;
377        fn PhaseRingBook_GetPhaseRingsId(book: &PhaseRingBook) -> Vec<String>;
378        fn PhaseRingBook_GetPhaseRing(book: &PhaseRingBook, id: &String) -> UniquePtr<PhaseRing>;
379        fn PhaseRingBook_FindPhaseRing(book: &PhaseRingBook, rule_id: &String) -> UniquePtr<PhaseRing>;
380
381        // RuleRegistry bindings definitions.
382        type RuleRegistry;
383        fn RuleRegistry_DiscreteValueRuleTypes(registry: &RuleRegistry) -> UniquePtr<CxxVector<DiscreteValueRuleType>>;
384        fn RuleRegistry_RangeValueRuleTypes(registry: &RuleRegistry) -> UniquePtr<CxxVector<RangeValueRuleType>>;
385
386        // DiscreteValueRuleStateProviderQuery bindings definitions.
387        type DiscreteValueRuleStateProviderQuery;
388        fn DiscreteValueRuleStateProviderQuery_state(
389            state_provider_query: &DiscreteValueRuleStateProviderQuery,
390        ) -> UniquePtr<DiscreteValueRuleDiscreteValue>;
391        fn DiscreteValueRuleStateProviderQuery_next(
392            state_provider_query: &DiscreteValueRuleStateProviderQuery,
393        ) -> UniquePtr<DiscreteValueNextState>;
394
395        // DiscreteValueRuleStateProvider bindings definitions.
396        type DiscreteValueRuleStateProvider;
397        fn DiscreteValueRuleStateProvider_GetStateById(
398            state_provider: &DiscreteValueRuleStateProvider,
399            id: &String,
400        ) -> UniquePtr<DiscreteValueRuleStateProviderQuery>;
401        fn DiscreteValueRuleStateProvider_GetStateByType(
402            state_provider: &DiscreteValueRuleStateProvider,
403            road_position: &RoadPosition,
404            rule_type: &String,
405            tolerance: f64,
406        ) -> UniquePtr<DiscreteValueRuleStateProviderQuery>;
407
408        // RangeValueRuleStateProviderQuery bindings definitions.
409        type RangeValueRuleStateProviderQuery;
410        fn RangeValueRuleStateProviderQuery_state(
411            state_provider_query: &RangeValueRuleStateProviderQuery,
412        ) -> UniquePtr<RangeValueRuleRange>;
413        fn RangeValueRuleStateProviderQuery_next(
414            state_provider_query: &RangeValueRuleStateProviderQuery,
415        ) -> UniquePtr<RangeValueNextState>;
416
417        // RangeValueRuleStateProvider bindings definitions.
418        type RangeValueRuleStateProvider;
419        fn RangeValueRuleStateProvider_GetStateById(
420            state_provider: &RangeValueRuleStateProvider,
421            id: &String,
422        ) -> UniquePtr<RangeValueRuleStateProviderQuery>;
423        fn RangeValueRuleStateProvider_GetStateByType(
424            state_provider: &RangeValueRuleStateProvider,
425            road_position: &RoadPosition,
426            rule_type: &String,
427            tolerance: f64,
428        ) -> UniquePtr<RangeValueRuleStateProviderQuery>;
429    }
430}