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    #[repr(i32)]
119    enum BulbColor {
120        kRed = 0,
121        kYellow,
122        kGreen,
123    }
124
125    #[repr(i32)]
126    enum BulbType {
127        kRound = 0,
128        kArrow,
129    }
130
131    #[repr(i32)]
132    enum BulbState {
133        kOff = 0,
134        kOn,
135        kBlinking,
136    }
137
138    unsafe extern "C++" {
139        include!("api/rules/rules.h");
140        include!("api/rules/aliases.h");
141        include!("cxx_utils/error_handling.h");
142
143        // Forward declarations
144        #[namespace = "maliput::api"]
145        type InertialPosition = crate::api::ffi::InertialPosition;
146        #[namespace = "maliput::api"]
147        type Rotation = crate::api::ffi::Rotation;
148        #[namespace = "maliput::api"]
149        type LaneSRange = crate::api::ffi::LaneSRange;
150        #[namespace = "maliput::api"]
151        type LaneSRoute = crate::api::ffi::LaneSRoute;
152        #[namespace = "maliput::math"]
153        type Vector3 = crate::math::ffi::Vector3;
154
155        // TrafficLightBook bindings definitions.
156        type TrafficLightBook;
157        fn TrafficLightBook_TrafficLights(book: &TrafficLightBook) -> UniquePtr<CxxVector<ConstTrafficLightPtr>>;
158        fn TrafficLightBook_GetTrafficLight(book: &TrafficLightBook, id: &String) -> *const TrafficLight;
159
160        // TrafficLight bindings definitions.
161        type TrafficLight;
162        fn TrafficLight_id(traffic_light: &TrafficLight) -> String;
163        fn TrafficLight_position_road_network(traffic_light: &TrafficLight) -> UniquePtr<InertialPosition>;
164        fn TrafficLight_orientation_road_network(traffic_light: &TrafficLight) -> UniquePtr<Rotation>;
165        fn TrafficLight_bulb_groups(traffic_light: &TrafficLight) -> UniquePtr<CxxVector<ConstBulbGroupPtr>>;
166        fn TrafficLight_GetBulbGroup(traffic_light: &TrafficLight, id: &String) -> *const BulbGroup;
167
168        type BulbColor;
169        type BulbState;
170        type BulbType;
171        // Bulb bindings definitions.
172        type Bulb;
173        fn Bulb_id(bulb: &Bulb) -> String;
174        fn Bulb_unique_id(bulb: &Bulb) -> UniquePtr<UniqueBulbId>;
175        fn Bulb_position_bulb_group(bulb: &Bulb) -> UniquePtr<InertialPosition>;
176        fn Bulb_orientation_bulb_group(bulb: &Bulb) -> UniquePtr<Rotation>;
177        fn color(self: &Bulb) -> &BulbColor;
178        // We can't automatically use the name `type` as it is a reserved keyword in Rust.
179        fn Bulb_type(bulb: &Bulb) -> &BulbType;
180        fn Bulb_arrow_orientation_rad(bulb: &Bulb) -> UniquePtr<FloatWrapper>;
181        fn Bulb_states(bulb: &Bulb) -> UniquePtr<CxxVector<BulbState>>;
182        fn GetDefaultState(self: &Bulb) -> BulbState;
183        fn IsValidState(self: &Bulb, state: &BulbState) -> bool;
184        fn Bulb_bounding_box_min(bulb: &Bulb) -> UniquePtr<Vector3>;
185        fn Bulb_bounding_box_max(bulb: &Bulb) -> UniquePtr<Vector3>;
186        fn Bulb_bulb_group(bulb: &Bulb) -> *const BulbGroup;
187
188        // BulbGroup bindings definitions.
189        type BulbGroup;
190        fn BulbGroup_id(bulb_group: &BulbGroup) -> String;
191        fn BulbGroup_unique_id(bulb: &BulbGroup) -> UniquePtr<UniqueBulbGroupId>;
192        fn BulbGroup_position_traffic_light(bulb_group: &BulbGroup) -> UniquePtr<InertialPosition>;
193        fn BulbGroup_orientation_traffic_light(bulb_group: &BulbGroup) -> UniquePtr<Rotation>;
194        fn BulbGroup_bulbs(bulb_group: &BulbGroup) -> UniquePtr<CxxVector<ConstBulbPtr>>;
195        fn BulbGroup_GetBulb(bulb_group: &BulbGroup, id: &String) -> *const Bulb;
196        fn BulbGroup_traffic_light(bulb_group: &BulbGroup) -> *const TrafficLight;
197
198        // UniqueBulbId bindings definitions.
199        type UniqueBulbId;
200        fn string(self: &UniqueBulbId) -> &CxxString;
201        fn UniqueBulbId_traffic_light_id(id: &UniqueBulbId) -> String;
202        fn UniqueBulbId_bulb_group_id(id: &UniqueBulbId) -> String;
203        fn UniqueBulbId_bulb_id(id: &UniqueBulbId) -> String;
204
205        // UniqueBulbGroupId bindings definitions.
206        type UniqueBulbGroupId;
207        fn string(self: &UniqueBulbGroupId) -> &CxxString;
208        fn UniqueBulbGroupId_traffic_light_id(id: &UniqueBulbGroupId) -> String;
209        fn UniqueBulbGroupId_bulb_group_id(id: &UniqueBulbGroupId) -> String;
210
211        // QueryResults bindings definitions.
212        type QueryResults;
213        fn QueryResults_discrete_value_rules(query_results: &QueryResults) -> Vec<String>;
214        fn QueryResults_range_value_rules(query_results: &QueryResults) -> Vec<String>;
215
216        // RoadRulebook bindings definitions.
217        type RoadRulebook;
218        fn RoadRulebook_GetDiscreteValueRule(book: &RoadRulebook, rule_id: &String) -> UniquePtr<DiscreteValueRule>;
219        fn RoadRulebook_GetRangeValueRule(book: &RoadRulebook, rule_id: &String) -> UniquePtr<RangeValueRule>;
220        fn RoadRulebook_Rules(book: &RoadRulebook) -> UniquePtr<QueryResults>;
221        #[allow(clippy::needless_lifetimes)]
222        fn RoadRulebook_FindRules(
223            book: &RoadRulebook,
224            ranges: &Vec<ConstLaneSRangeRef>,
225            tolerance: f64,
226        ) -> Result<UniquePtr<QueryResults>>;
227
228        // DiscreteValueRule::DiscreteValue bindings definitions.
229        type DiscreteValueRuleDiscreteValue;
230        fn DiscreteValueRuleDiscreteValue_value(value: &DiscreteValueRuleDiscreteValue) -> String;
231        fn DiscreteValueRuleDiscreteValue_severity(value: &DiscreteValueRuleDiscreteValue) -> i32;
232        fn DiscreteValueRuleDiscreteValue_related_rules(
233            value: &DiscreteValueRuleDiscreteValue,
234        ) -> UniquePtr<CxxVector<RelatedRule>>;
235        fn DiscreteValueRuleDiscreteValue_related_unique_ids(
236            value: &DiscreteValueRuleDiscreteValue,
237        ) -> UniquePtr<CxxVector<RelatedUniqueId>>;
238
239        // DiscreteValueRule bindings definitions.
240        type DiscreteValueRule;
241        fn states(self: &DiscreteValueRule) -> &CxxVector<DiscreteValueRuleDiscreteValue>;
242        fn DiscreteValueRule_id(rule: &DiscreteValueRule) -> String;
243        fn DiscreteValueRule_type_id(rule: &DiscreteValueRule) -> String;
244        fn DiscreteValueRule_zone(rule: &DiscreteValueRule) -> UniquePtr<LaneSRoute>;
245
246        // RangeValueRule::Range bindings definitions.
247        type RangeValueRuleRange;
248        fn RangeValueRuleRange_description(range: &RangeValueRuleRange) -> String;
249        fn RangeValueRuleRange_min(range: &RangeValueRuleRange) -> f64;
250        fn RangeValueRuleRange_max(range: &RangeValueRuleRange) -> f64;
251        fn RangeValueRuleRange_severity(range: &RangeValueRuleRange) -> i32;
252        fn RangeValueRuleRange_related_rules(range: &RangeValueRuleRange) -> UniquePtr<CxxVector<RelatedRule>>;
253        fn RangeValueRuleRange_related_unique_ids(range: &RangeValueRuleRange)
254            -> UniquePtr<CxxVector<RelatedUniqueId>>;
255        // RangeValueRule::Range bindings definitions.
256        type RangeValueRule;
257        fn RangeValueRule_id(rule: &RangeValueRule) -> String;
258        fn RangeValueRule_type_id(rule: &RangeValueRule) -> String;
259        fn RangeValueRule_zone(rule: &RangeValueRule) -> UniquePtr<LaneSRoute>;
260        fn states(self: &RangeValueRule) -> &CxxVector<RangeValueRuleRange>;
261
262        // Phase bindings definitions.
263        type Phase;
264        fn Phase_id(phase: &Phase) -> String;
265        fn Phase_discrete_value_rule_states(phase: &Phase) -> UniquePtr<CxxVector<DiscreteValueRuleState>>;
266        fn Phase_unique_bulb_ids(phase: &Phase) -> UniquePtr<CxxVector<UniqueBulbId>>;
267        fn Phase_bulb_state(phase: &Phase, bulb_id: &UniqueBulbId) -> UniquePtr<BulbState>;
268        // Helper method to implement [Phase_unique_bulb_ids] API method.
269        fn ptr_from_unique_bulb_id(id: &UniqueBulbId) -> UniquePtr<UniqueBulbId>;
270
271        // PhaseRing bindings definitions.
272        type PhaseRing;
273        fn PhaseRing_id(phase_ring: &PhaseRing) -> String;
274        fn PhaseRing_GetPhase(phase_ring: &PhaseRing, id: &String) -> UniquePtr<Phase>;
275        fn PhaseRing_phases_ids(phase_ring: &PhaseRing) -> Vec<String>;
276        fn PhaseRing_GetNextPhases(phase_ring: &PhaseRing, id: &String) -> Result<UniquePtr<CxxVector<NextPhase>>>;
277
278        // PhaseRingBook bindings definitions.
279        type PhaseRingBook;
280        fn PhaseRingBook_GetPhaseRingsId(book: &PhaseRingBook) -> Vec<String>;
281        fn PhaseRingBook_GetPhaseRing(book: &PhaseRingBook, id: &String) -> UniquePtr<PhaseRing>;
282        fn PhaseRingBook_FindPhaseRing(book: &PhaseRingBook, rule_id: &String) -> UniquePtr<PhaseRing>;
283
284        // RuleRegistry bindings definitions.
285        type RuleRegistry;
286        fn RuleRegistry_DiscreteValueRuleTypes(registry: &RuleRegistry) -> UniquePtr<CxxVector<DiscreteValueRuleType>>;
287        fn RuleRegistry_RangeValueRuleTypes(registry: &RuleRegistry) -> UniquePtr<CxxVector<RangeValueRuleType>>;
288    }
289}