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        include!("cxx_utils/error_handling.h");
108
109        // Forward declarations
110        #[namespace = "maliput::api"]
111        type InertialPosition = crate::api::ffi::InertialPosition;
112        #[namespace = "maliput::api"]
113        type Rotation = crate::api::ffi::Rotation;
114        #[namespace = "maliput::api"]
115        type LaneSRange = crate::api::ffi::LaneSRange;
116        #[namespace = "maliput::api"]
117        type LaneSRoute = crate::api::ffi::LaneSRoute;
118        #[namespace = "maliput::math"]
119        type Vector3 = crate::math::ffi::Vector3;
120
121        // TrafficLightBook bindings definitions.
122        type TrafficLightBook;
123        fn TrafficLightBook_TrafficLights(book: &TrafficLightBook) -> UniquePtr<CxxVector<ConstTrafficLightPtr>>;
124        fn TrafficLightBook_GetTrafficLight(book: &TrafficLightBook, id: &String) -> *const TrafficLight;
125
126        // TrafficLight bindings definitions.
127        type TrafficLight;
128        fn TrafficLight_id(traffic_light: &TrafficLight) -> String;
129        fn TrafficLight_position_road_network(traffic_light: &TrafficLight) -> UniquePtr<InertialPosition>;
130        fn TrafficLight_orientation_road_network(traffic_light: &TrafficLight) -> UniquePtr<Rotation>;
131        fn TrafficLight_bulb_groups(traffic_light: &TrafficLight) -> UniquePtr<CxxVector<ConstBulbGroupPtr>>;
132        fn TrafficLight_GetBulbGroup(traffic_light: &TrafficLight, id: &String) -> *const BulbGroup;
133
134        type BulbColor;
135        type BulbState;
136        type BulbType;
137        // Bulb bindings definitions.
138        type Bulb;
139        fn Bulb_id(bulb: &Bulb) -> String;
140        fn Bulb_unique_id(bulb: &Bulb) -> UniquePtr<UniqueBulbId>;
141        fn Bulb_position_bulb_group(bulb: &Bulb) -> UniquePtr<InertialPosition>;
142        fn Bulb_orientation_bulb_group(bulb: &Bulb) -> UniquePtr<Rotation>;
143        fn color(self: &Bulb) -> &BulbColor;
144        // We can't automatically use the name `type` as it is a reserved keyword in Rust.
145        fn Bulb_type(bulb: &Bulb) -> &BulbType;
146        fn Bulb_arrow_orientation_rad(bulb: &Bulb) -> UniquePtr<FloatWrapper>;
147        fn Bulb_states(bulb: &Bulb) -> UniquePtr<CxxVector<BulbState>>;
148        fn GetDefaultState(self: &Bulb) -> BulbState;
149        fn IsValidState(self: &Bulb, state: &BulbState) -> bool;
150        fn Bulb_bounding_box_min(bulb: &Bulb) -> UniquePtr<Vector3>;
151        fn Bulb_bounding_box_max(bulb: &Bulb) -> UniquePtr<Vector3>;
152        fn Bulb_bulb_group(bulb: &Bulb) -> *const BulbGroup;
153
154        // BulbGroup bindings definitions.
155        type BulbGroup;
156        fn BulbGroup_id(bulb_group: &BulbGroup) -> String;
157        fn BulbGroup_unique_id(bulb: &BulbGroup) -> UniquePtr<UniqueBulbGroupId>;
158        fn BulbGroup_position_traffic_light(bulb_group: &BulbGroup) -> UniquePtr<InertialPosition>;
159        fn BulbGroup_orientation_traffic_light(bulb_group: &BulbGroup) -> UniquePtr<Rotation>;
160        fn BulbGroup_bulbs(bulb_group: &BulbGroup) -> UniquePtr<CxxVector<ConstBulbPtr>>;
161        fn BulbGroup_GetBulb(bulb_group: &BulbGroup, id: &String) -> *const Bulb;
162        fn BulbGroup_traffic_light(bulb_group: &BulbGroup) -> *const TrafficLight;
163
164        // UniqueBulbId bindings definitions.
165        type UniqueBulbId;
166        fn string(self: &UniqueBulbId) -> &CxxString;
167        fn UniqueBulbId_traffic_light_id(id: &UniqueBulbId) -> String;
168        fn UniqueBulbId_bulb_group_id(id: &UniqueBulbId) -> String;
169        fn UniqueBulbId_bulb_id(id: &UniqueBulbId) -> String;
170
171        // UniqueBulbGroupId bindings definitions.
172        type UniqueBulbGroupId;
173        fn string(self: &UniqueBulbGroupId) -> &CxxString;
174        fn UniqueBulbGroupId_traffic_light_id(id: &UniqueBulbGroupId) -> String;
175        fn UniqueBulbGroupId_bulb_group_id(id: &UniqueBulbGroupId) -> String;
176
177        // QueryResults bindings definitions.
178        type QueryResults;
179        fn QueryResults_discrete_value_rules(query_results: &QueryResults) -> Vec<String>;
180        fn QueryResults_range_value_rules(query_results: &QueryResults) -> Vec<String>;
181
182        // RoadRulebook bindings definitions.
183        type RoadRulebook;
184        fn RoadRulebook_GetDiscreteValueRule(
185            book: &RoadRulebook,
186            rule_id: &String,
187        ) -> Result<UniquePtr<DiscreteValueRule>>;
188        fn RoadRulebook_GetRangeValueRule(book: &RoadRulebook, rule_id: &String) -> Result<UniquePtr<RangeValueRule>>;
189        fn RoadRulebook_Rules(book: &RoadRulebook) -> UniquePtr<QueryResults>;
190        #[allow(clippy::needless_lifetimes)]
191        fn RoadRulebook_FindRules(
192            book: &RoadRulebook,
193            ranges: &Vec<ConstLaneSRangeRef>,
194            tolerance: f64,
195        ) -> Result<UniquePtr<QueryResults>>;
196
197        // DiscreteValueRule::DiscreteValue bindings definitions.
198        type DiscreteValueRuleDiscreteValue;
199        fn DiscreteValueRuleDiscreteValue_value(value: &DiscreteValueRuleDiscreteValue) -> String;
200        fn DiscreteValueRuleDiscreteValue_severity(value: &DiscreteValueRuleDiscreteValue) -> i32;
201        fn DiscreteValueRuleDiscreteValue_related_rules(
202            value: &DiscreteValueRuleDiscreteValue,
203        ) -> UniquePtr<CxxVector<RelatedRule>>;
204        fn DiscreteValueRuleDiscreteValue_related_unique_ids(
205            value: &DiscreteValueRuleDiscreteValue,
206        ) -> UniquePtr<CxxVector<RelatedUniqueId>>;
207
208        // DiscreteValueRule bindings definitions.
209        type DiscreteValueRule;
210        fn states(self: &DiscreteValueRule) -> &CxxVector<DiscreteValueRuleDiscreteValue>;
211        fn DiscreteValueRule_id(rule: &DiscreteValueRule) -> String;
212        fn DiscreteValueRule_type_id(rule: &DiscreteValueRule) -> String;
213        fn DiscreteValueRule_zone(rule: &DiscreteValueRule) -> UniquePtr<LaneSRoute>;
214
215        // RangeValueRule::Range bindings definitions.
216        type RangeValueRuleRange;
217        fn RangeValueRuleRange_description(range: &RangeValueRuleRange) -> String;
218        fn RangeValueRuleRange_min(range: &RangeValueRuleRange) -> f64;
219        fn RangeValueRuleRange_max(range: &RangeValueRuleRange) -> f64;
220        fn RangeValueRuleRange_severity(range: &RangeValueRuleRange) -> i32;
221        fn RangeValueRuleRange_related_rules(range: &RangeValueRuleRange) -> UniquePtr<CxxVector<RelatedRule>>;
222        fn RangeValueRuleRange_related_unique_ids(range: &RangeValueRuleRange)
223            -> UniquePtr<CxxVector<RelatedUniqueId>>;
224        // RangeValueRule::Range bindings definitions.
225        type RangeValueRule;
226        fn RangeValueRule_id(rule: &RangeValueRule) -> String;
227        fn RangeValueRule_type_id(rule: &RangeValueRule) -> String;
228        fn RangeValueRule_zone(rule: &RangeValueRule) -> UniquePtr<LaneSRoute>;
229        fn states(self: &RangeValueRule) -> &CxxVector<RangeValueRuleRange>;
230    }
231}