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 binded 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 binded directly.
71    struct RelatedUniqueId {
72        pub group_name: String,
73        pub unique_ids: Vec<String>,
74    }
75
76    /// Shared struct for `LaneSRange` constant reference.
77    /// Interestingly this was done at maliput::api module but
78    /// couldn't reference to that so it was necessary to
79    /// redefine it here.
80    struct ConstLaneSRangeRef<'a> {
81        pub lane_s_range: &'a LaneSRange,
82    }
83
84    #[repr(i32)]
85    enum BulbColor {
86        kRed = 0,
87        kYellow,
88        kGreen,
89    }
90
91    #[repr(i32)]
92    enum BulbType {
93        kRound = 0,
94        kArrow,
95    }
96
97    #[repr(i32)]
98    enum BulbState {
99        kOff = 0,
100        kOn,
101        kBlinking,
102    }
103
104    unsafe extern "C++" {
105        include!("api/rules/rules.h");
106        include!("api/rules/aliases.h");
107
108        // Forward declarations
109        #[namespace = "maliput::api"]
110        type InertialPosition = crate::api::ffi::InertialPosition;
111        #[namespace = "maliput::api"]
112        type Rotation = crate::api::ffi::Rotation;
113        #[namespace = "maliput::api"]
114        type LaneSRange = crate::api::ffi::LaneSRange;
115        #[namespace = "maliput::api"]
116        type LaneSRoute = crate::api::ffi::LaneSRoute;
117        #[namespace = "maliput::math"]
118        type Vector3 = crate::math::ffi::Vector3;
119
120        // TrafficLightBook bindings definitions.
121        type TrafficLightBook;
122        fn TrafficLightBook_TrafficLights(book: &TrafficLightBook) -> UniquePtr<CxxVector<ConstTrafficLightPtr>>;
123        fn TrafficLightBook_GetTrafficLight(book: &TrafficLightBook, id: &String) -> *const TrafficLight;
124
125        // TrafficLight bindings definitions.
126        type TrafficLight;
127        fn TrafficLight_id(traffic_light: &TrafficLight) -> String;
128        fn TrafficLight_position_road_network(traffic_light: &TrafficLight) -> UniquePtr<InertialPosition>;
129        fn TrafficLight_orientation_road_network(traffic_light: &TrafficLight) -> UniquePtr<Rotation>;
130        fn TrafficLight_bulb_groups(traffic_light: &TrafficLight) -> UniquePtr<CxxVector<ConstBulbGroupPtr>>;
131        fn TrafficLight_GetBulbGroup(traffic_light: &TrafficLight, id: &String) -> *const BulbGroup;
132
133        type BulbColor;
134        type BulbState;
135        type BulbType;
136        // Bulb bindings definitions.
137        type Bulb;
138        fn Bulb_id(bulb: &Bulb) -> String;
139        fn Bulb_unique_id(bulb: &Bulb) -> UniquePtr<UniqueBulbId>;
140        fn Bulb_position_bulb_group(bulb: &Bulb) -> UniquePtr<InertialPosition>;
141        fn Bulb_orientation_bulb_group(bulb: &Bulb) -> UniquePtr<Rotation>;
142        fn color(self: &Bulb) -> &BulbColor;
143        // We can't automatically use the name `type` as it is a reserved keyword in Rust.
144        fn Bulb_type(bulb: &Bulb) -> &BulbType;
145        fn Bulb_arrow_orientation_rad(bulb: &Bulb) -> UniquePtr<FloatWrapper>;
146        fn Bulb_states(bulb: &Bulb) -> UniquePtr<CxxVector<BulbState>>;
147        fn GetDefaultState(self: &Bulb) -> BulbState;
148        fn IsValidState(self: &Bulb, state: &BulbState) -> bool;
149        fn Bulb_bounding_box_min(bulb: &Bulb) -> UniquePtr<Vector3>;
150        fn Bulb_bounding_box_max(bulb: &Bulb) -> UniquePtr<Vector3>;
151        fn Bulb_bulb_group(bulb: &Bulb) -> *const BulbGroup;
152
153        // BulbGroup bindings definitions.
154        type BulbGroup;
155        fn BulbGroup_id(bulb_group: &BulbGroup) -> String;
156        fn BulbGroup_unique_id(bulb: &BulbGroup) -> UniquePtr<UniqueBulbGroupId>;
157        fn BulbGroup_position_traffic_light(bulb_group: &BulbGroup) -> UniquePtr<InertialPosition>;
158        fn BulbGroup_orientation_traffic_light(bulb_group: &BulbGroup) -> UniquePtr<Rotation>;
159        fn BulbGroup_bulbs(bulb_group: &BulbGroup) -> UniquePtr<CxxVector<ConstBulbPtr>>;
160        fn BulbGroup_GetBulb(bulb_group: &BulbGroup, id: &String) -> *const Bulb;
161        fn BulbGroup_traffic_light(bulb_group: &BulbGroup) -> *const TrafficLight;
162
163        // UniqueBulbId bindings definitions.
164        type UniqueBulbId;
165        fn string(self: &UniqueBulbId) -> &CxxString;
166        fn UniqueBulbId_traffic_light_id(id: &UniqueBulbId) -> String;
167        fn UniqueBulbId_bulb_group_id(id: &UniqueBulbId) -> String;
168        fn UniqueBulbId_bulb_id(id: &UniqueBulbId) -> String;
169
170        // UniqueBulbGroupId bindings definitions.
171        type UniqueBulbGroupId;
172        fn string(self: &UniqueBulbGroupId) -> &CxxString;
173        fn UniqueBulbGroupId_traffic_light_id(id: &UniqueBulbGroupId) -> String;
174        fn UniqueBulbGroupId_bulb_group_id(id: &UniqueBulbGroupId) -> String;
175
176        // QueryResults bindings definitions.
177        type QueryResults;
178        fn QueryResults_discrete_value_rules(query_results: &QueryResults) -> Vec<String>;
179        fn QueryResults_range_value_rules(query_results: &QueryResults) -> Vec<String>;
180
181        // RoadRulebook bindings definitions.
182        type RoadRulebook;
183        fn RoadRulebook_GetDiscreteValueRule(book: &RoadRulebook, rule_id: &String) -> UniquePtr<DiscreteValueRule>;
184        fn RoadRulebook_GetRangeValueRule(book: &RoadRulebook, rule_id: &String) -> UniquePtr<RangeValueRule>;
185        fn RoadRulebook_Rules(book: &RoadRulebook) -> UniquePtr<QueryResults>;
186        #[allow(clippy::needless_lifetimes)]
187        fn RoadRulebook_FindRules(
188            book: &RoadRulebook,
189            ranges: &Vec<ConstLaneSRangeRef>,
190            tolerance: f64,
191        ) -> UniquePtr<QueryResults>;
192
193        // DiscreteValueRule::DiscreteValue bindings definitions.
194        type DiscreteValueRuleDiscreteValue;
195        fn DiscreteValueRuleDiscreteValue_value(value: &DiscreteValueRuleDiscreteValue) -> String;
196        fn DiscreteValueRuleDiscreteValue_severity(value: &DiscreteValueRuleDiscreteValue) -> i32;
197        fn DiscreteValueRuleDiscreteValue_related_rules(
198            value: &DiscreteValueRuleDiscreteValue,
199        ) -> UniquePtr<CxxVector<RelatedRule>>;
200        fn DiscreteValueRuleDiscreteValue_related_unique_ids(
201            value: &DiscreteValueRuleDiscreteValue,
202        ) -> UniquePtr<CxxVector<RelatedUniqueId>>;
203
204        // DiscreteValueRule bindings definitions.
205        type DiscreteValueRule;
206        fn states(self: &DiscreteValueRule) -> &CxxVector<DiscreteValueRuleDiscreteValue>;
207        fn DiscreteValueRule_id(rule: &DiscreteValueRule) -> String;
208        fn DiscreteValueRule_type_id(rule: &DiscreteValueRule) -> String;
209        fn DiscreteValueRule_zone(rule: &DiscreteValueRule) -> UniquePtr<LaneSRoute>;
210
211        // RangeValueRule::Range bindings definitions.
212        type RangeValueRuleRange;
213        fn RangeValueRuleRange_description(range: &RangeValueRuleRange) -> String;
214        fn RangeValueRuleRange_min(range: &RangeValueRuleRange) -> f64;
215        fn RangeValueRuleRange_max(range: &RangeValueRuleRange) -> f64;
216        fn RangeValueRuleRange_severity(range: &RangeValueRuleRange) -> i32;
217        fn RangeValueRuleRange_related_rules(range: &RangeValueRuleRange) -> UniquePtr<CxxVector<RelatedRule>>;
218        fn RangeValueRuleRange_related_unique_ids(range: &RangeValueRuleRange)
219            -> UniquePtr<CxxVector<RelatedUniqueId>>;
220        // RangeValueRule::Range bindings definitions.
221        type RangeValueRule;
222        fn RangeValueRule_id(rule: &RangeValueRule) -> String;
223        fn RangeValueRule_type_id(rule: &RangeValueRule) -> String;
224        fn RangeValueRule_zone(rule: &RangeValueRule) -> UniquePtr<LaneSRoute>;
225        fn states(self: &RangeValueRule) -> &CxxVector<RangeValueRuleRange>;
226    }
227}