google_api_rust_client_unoffical/services/route_service/
common_models.rs

1
2use std::collections::HashMap;
3
4use serde::{Deserialize, Serialize};
5use anyhow::{Ok, Result};
6use serde_json::Value;
7
8/// Encapsulates a waypoint.
9/// Waypoints mark both the beginning and end of a route, and include intermediate stops along the route. <br>
10/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/Waypoint
11#[derive(Debug, Clone, Serialize, Deserialize)]
12#[serde(rename_all = "camelCase")]
13pub struct WayPoint {
14    // Exactly one of the following need to be present
15    // Union field location_type can be only one of the following:
16    #[serde(skip_serializing_if = "Option::is_none")]
17    location: Option<Location>,
18    #[serde(skip_serializing_if = "Option::is_none")]
19    place_id: Option<String>,
20    #[serde(skip_serializing_if = "Option::is_none")]
21    address: Option<String>,
22    // End of list of possible types for union field location_type.
23
24    // optional fields
25    #[serde(skip_serializing_if = "Option::is_none", flatten)]
26    params: Option<WayPointOptionalParams>
27}
28
29
30/// Optional parameters for WayPoint
31/// * `via`: Marks this waypoint as a milestone rather a stopping point.
32/// * `vehicle_stopover`: Indicates that the waypoint is meant for vehicles to stop at, where the intention is to either pickup or drop-off.
33/// * `side_of_road`: Indicates that the location of this waypoint is meant to have a preference for the vehicle to stop at a particular side of road.
34#[derive(Debug, Clone, Serialize, Deserialize)]
35#[serde(rename_all = "camelCase")]
36pub struct WayPointOptionalParams {
37    #[serde(skip_serializing_if = "Option::is_none")]
38    via: Option<bool>,
39    #[serde(skip_serializing_if = "Option::is_none")]
40    vehicle_stopover: Option<bool>,
41    #[serde(skip_serializing_if = "Option::is_none")]
42    side_of_road: Option<bool>
43}
44
45impl WayPoint {
46    pub fn new_from_location(location: Location, params: Option<HashMap<String, Value>>) -> Result<Self> {
47        let additional_params = Self::get_additional_params(params)?;
48        Ok(Self{
49            location: Some(location),
50            place_id: None,
51            address: None,
52            params: additional_params
53        })
54    }
55
56    pub fn new_from_place_id(place_id: &str, params: Option<HashMap<String, Value>>) -> Result<Self> {
57        let additional_params = Self::get_additional_params(params)?;
58        Ok(Self{
59            location: None,
60            place_id: Some(place_id.to_owned()),
61            address: None,
62            params: additional_params
63        })
64    }
65
66    pub fn new_from_address(address: &str, params: Option<HashMap<String, Value>>) -> Result<Self> {
67        let additional_params = Self::get_additional_params(params)?;
68        Ok(Self{
69            location: None,
70            place_id: None,
71            address: Some(address.to_owned()),
72            params: additional_params
73        })
74    }
75
76    fn get_additional_params(params: Option<HashMap<String, Value>>) -> Result<Option<WayPointOptionalParams>> {
77        let additional_params: Option<WayPointOptionalParams> = if let Some(params) = params {
78            let additional_params_string = serde_json::to_string(&params)?;
79            serde_json::from_str(&additional_params_string)?
80        } else {
81            None
82        };
83        Ok(additional_params)
84    }
85
86}
87
88/// Encapsulates a location (a geographic point, and an optional heading). <br>
89/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/Location
90#[derive(Debug, Clone, Serialize, Deserialize)]
91#[serde(rename_all = "camelCase")]
92pub struct Location {
93    pub lat_lng: LatLng,
94    /// Heading values can be from 0 to 360,
95    /// where 0 specifies a heading of due North, 90 specifies a heading of due East, and so on.
96    /// You can use this field only for DRIVE and TWO_WHEELER RouteTravelMode.
97    pub heading: Option<u32>
98}
99
100impl Location {
101    /// Create a Location at a geographic point, and an optional heading
102    pub fn new(latitude: f32, longitude: f32, heading: Option<u32>) -> Self {
103        Self {
104            lat_lng: LatLng { latitude, longitude },
105            heading
106        }
107    }
108}
109
110
111/// An object that represents a latitude/longitude pair. <br>
112/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/LatLng
113#[derive(Debug, Clone, Serialize, Deserialize)]
114#[serde(rename_all = "camelCase")]
115pub struct LatLng {
116    pub latitude: f32,
117    pub longitude: f32,
118}
119
120
121
122/// A set of values used to specify the mode of travel.
123/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/RouteTravelMode
124#[derive(Debug, Clone, Serialize, Deserialize)]
125#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
126pub enum RouteTravelMode {
127    TravelModeUnspecified,
128    Drive,
129    Bycycle,
130    Walk,
131    TwoWheeler,
132    Transit
133}
134
135
136/// Localized variant of a text in a particular language.
137/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/LocalizedText
138#[derive(Debug, Clone, Serialize, Deserialize)]
139#[serde(rename_all = "camelCase")]
140pub struct LocalizedText {
141    #[serde(skip_serializing_if = "Option::is_none")]
142    pub text: Option<String>,
143    #[serde(skip_serializing_if = "Option::is_none")]
144    pub language_code: Option<String>
145}
146
147
148
149/// A set of optional conditions to satisfy when calculating the routes.
150/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/RouteModifiers
151#[derive(Debug, Clone, Serialize, Deserialize)]
152#[serde(rename_all = "camelCase")]
153pub struct RouteModifiers {
154    #[serde(skip_serializing_if = "Option::is_none")]
155    avoid_tolls: Option<bool>,
156    #[serde(skip_serializing_if = "Option::is_none")]
157    avoid_highways: Option<bool>,
158    #[serde(skip_serializing_if = "Option::is_none")]
159    avoid_ferries: Option<bool>,
160    #[serde(skip_serializing_if = "Option::is_none")]
161    avoid_indoor: Option<bool>,
162    #[serde(skip_serializing_if = "Option::is_none")]
163    vehicle_info: Option<VehicleInfo>,
164    #[serde(skip_serializing_if = "Option::is_none")]
165    toll_passes: Option<Vec<String>>
166}
167
168
169impl RouteModifiers {
170    /// A set of optional conditions to satisfy when calculating the routes.
171    /// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/RouteModifiers
172    pub fn new(avoid_tolls: Option<bool>, avoid_highways: Option<bool>, avoid_ferries: Option<bool>, avoid_indoor: Option<bool>, emission_type: Option<EmissionType>, toll_passes: Option<Vec<String>>) -> Self{
173        let vehicle_info:Option<VehicleInfo>= if let Some(emission_type) = emission_type {
174            Some(VehicleInfo {emission_type})
175        } else {
176            None
177        };
178        Self {
179            avoid_tolls, avoid_highways, avoid_ferries, avoid_indoor,vehicle_info,toll_passes
180        }
181    }
182}
183
184
185
186#[derive(Debug, Clone, Serialize, Deserialize)]
187#[serde(rename_all = "camelCase")]
188struct VehicleInfo {
189    emission_type: EmissionType
190}
191
192#[derive(Debug, Clone, Serialize, Deserialize)]
193#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
194pub enum EmissionType {
195    VehicleEmissionTypeUnspecified,
196    Gasoline,
197    Electric,
198    Hybrid,
199    Diesel
200}
201
202/// A set of values that specify factors to take into consideration when calculating the route.
203/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/RoutingPreference
204#[derive(Debug, Clone, Serialize, Deserialize)]
205#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
206pub enum RoutingPreference {
207    RoutingPreferenceUnspecified,
208    TrafficUnaware,
209    TrafficAware,
210    TrafficAwareOptimal,
211}
212
213
214/// Values that specify the unit of measure used in the display.
215/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/Units
216#[derive(Debug, Clone, Serialize, Deserialize)]
217#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
218pub enum Unit {
219    UnitsUnspecified,
220    Metric,
221    Imperial,
222}
223
224
225/// Extra computations to perform while completing the request.
226/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/TopLevel/computeRoutes
227#[derive(Debug, Clone, Serialize, Deserialize)]
228#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
229pub enum ExtraComputation {
230    ExtraComputationUnspecified,
231    Tolls,
232    FuelConsumption,
233    TrafficOnPolyline,
234    HtmlFormattedNavigationInstructions
235}
236
237
238/// Specifies the assumptions to use when calculating time in traffic.
239/// This setting affects the value returned in the duration field in the response, which contains the predicted time in traffic based on historical averages.
240/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/TrafficModel
241#[derive(Debug, Clone, Serialize, Deserialize)]
242#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
243pub enum TrafficModel {
244    TrafficModelUnspecified,
245    BestGuess,
246    Pessimistic,
247    Optimistic,
248}
249
250/// Preferences for TRANSIT based routes that influence the route that is returned.
251/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/TransitPreferences
252#[derive(Debug, Clone, Serialize, Deserialize)]
253#[serde(rename_all = "camelCase")]
254pub struct TransitPreferences {
255    allowed_travel_modes: Option<Vec<TransitTravelMode>>,
256    routing_preference: Option<TransitRoutingPreference>
257}
258
259impl TransitPreferences {
260    pub fn new(allowed_travel_modes: Option<Vec<TransitTravelMode>>, routing_preference: Option<TransitRoutingPreference>) -> Self {
261        Self { allowed_travel_modes, routing_preference }
262    }
263}
264
265
266
267/// A set of values used to specify the mode of transit.
268/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/TransitPreferences
269#[derive(Debug, Clone, Serialize, Deserialize)]
270#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
271pub enum TransitTravelMode {
272    TransitTravelModeUnspecified,
273    Bus,
274    Subway,
275    Train,
276    LightRail,
277    Rail
278}
279
280/// Specifies routing preferences for transit routes.
281/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/TransitPreferences
282#[derive(Debug, Clone, Serialize, Deserialize)]
283#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
284pub enum TransitRoutingPreference {
285    TransitRoutingPreferenceUnspecified,
286    LessWalking,
287    FewerTransfers,
288}
289
290
291
292
293/// Information related to how and why a fallback result was used.
294/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/FallbackInfo
295#[derive(Debug, Clone, Serialize, Deserialize)]
296#[serde(rename_all = "camelCase")]
297pub struct FallbackInfo {
298    #[serde(skip_serializing_if = "Option::is_none")]
299    pub routing_mode: Option<FallbackRoutingMode>,
300    #[serde(skip_serializing_if = "Option::is_none")]
301    pub reason: Option<FallbackReason>
302}
303
304
305/// Actual routing mode used for returned fallback response.
306/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/FallbackInfo
307#[derive(Debug, Clone, Serialize, Deserialize)]
308#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
309pub enum FallbackRoutingMode {
310    FallbackRoutingModeUnspecified,
311    FallbackTrafficUnaware,
312    FallbackTrafficAware
313}
314
315/// Actual routing mode used for returned fallback response.
316/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/FallbackInfo
317#[derive(Debug, Clone, Serialize, Deserialize)]
318#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
319pub enum FallbackReason {
320    FallbackReasonUnspecified,
321    ServerError,
322    LatencyExceeded
323}
324
325
326/// Contains the additional information that the user should be informed about, such as possible traffic zone restrictions.
327/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/RouteTravelAdvisory
328#[derive(Debug, Clone, Serialize, Deserialize)]
329#[serde(rename_all = "camelCase")]
330pub struct RouteTravelAdvisory {
331    #[serde(skip_serializing_if = "Option::is_none")]
332    pub toll_info: Option<TollInfo>,
333    #[serde(skip_serializing_if = "Option::is_none")]
334    pub speed_reading_intervals: Option<SpeedReadingInterval>,
335    #[serde(skip_serializing_if = "Option::is_none")]
336    pub fuel_consumption_microliters: Option<String>,
337    #[serde(skip_serializing_if = "Option::is_none")]
338    pub route_restrictions_partially_ignored: Option<bool>,
339    #[serde(skip_serializing_if = "Option::is_none")]
340    pub transit_fare: Option<Money>
341}
342
343
344/// Encapsulates toll information on a Route or on a RouteLeg.
345/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/TollInfo
346#[derive(Debug, Clone, Serialize, Deserialize)]
347#[serde(rename_all = "camelCase")]
348pub struct TollInfo {
349    #[serde(skip_serializing_if = "Option::is_none")]
350    pub estimated_price: Option<Vec<Money>>
351}
352
353/// Represents an amount of money with its currency type.
354/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/Money
355#[derive(Debug, Clone, Serialize, Deserialize)]
356#[serde(rename_all = "camelCase")]
357pub struct Money {
358    #[serde(skip_serializing_if = "Option::is_none")]
359    pub currency_code: Option<String>,
360    #[serde(skip_serializing_if = "Option::is_none")]
361    pub units: Option<String>,
362    #[serde(skip_serializing_if = "Option::is_none")]
363    pub nanos: Option<i128>,
364}
365
366
367
368/// Traffic density indicator on a contiguous segment of a polyline or path. Given a path with points P_0, P_1, ... , P_N (zero-based index), the SpeedReadingInterval defines an interval and describes its traffic using the following categories.
369/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/SpeedReadingInterval
370#[derive(Debug, Clone, Serialize, Deserialize)]
371#[serde(rename_all = "camelCase")]
372pub struct SpeedReadingInterval {
373    #[serde(skip_serializing_if = "Option::is_none")]
374    pub start_polyline_point_index: Option<u32>,
375    #[serde(skip_serializing_if = "Option::is_none")]
376    pub end_polyline_point_index: Option<u32>,
377    #[serde(skip_serializing_if = "Option::is_none")]
378    pub speed: Option<Speed>
379}
380
381/// The classification of polyline speed based on traffic data.
382/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/TopLevel/computeRoutes
383#[derive(Debug, Clone, Serialize, Deserialize)]
384#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
385pub enum Speed {
386    SpeedUnspecified,
387    Normal,
388    Slow,
389    TrafficJam
390}
391
392
393
394/// The Status type defines a logical error model that is suitable for different programming environments, including REST APIs and RPC APIs.
395/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/Status
396#[derive(Debug, Clone, Serialize, Deserialize)]
397#[serde(rename_all = "camelCase")]
398pub struct Status {
399    #[serde(skip_serializing_if = "Option::is_none")]
400    pub code: Option<i32>,
401    #[serde(skip_serializing_if = "Option::is_none")]
402    pub message: Option<String>,
403    #[serde(skip_serializing_if = "Option::is_none")]
404    pub details: Option<Value>
405}
406
407/// Text representations of certain properties.
408/// See https://developers.google.com/maps/documentation/routes/reference/rest/v2/TopLevel/computeRoutes
409#[derive(Debug, Clone, Serialize, Deserialize)]
410#[serde(rename_all = "camelCase")]
411pub struct RouteLocalizedValues {
412    #[serde(skip_serializing_if = "Option::is_none")]
413    pub distance: Option<LocalizedText>,
414    #[serde(skip_serializing_if = "Option::is_none")]
415    pub duration: Option<LocalizedText>,
416    #[serde(skip_serializing_if = "Option::is_none")]
417    pub static_duration: Option<LocalizedText>,
418    #[serde(skip_serializing_if = "Option::is_none")]
419    pub transit_fare: Option<LocalizedText>,
420}