1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
use crate::distance_matrix::response::status::Status;
#[derive(Debug)]
pub enum Error {
ArrivalTimeIsForTransitOnly(String, String),
EitherAlternativesOrWaypoints(usize),
EitherDepartureTimeOrArrivalTime(String, String),
EitherRestrictionsOrWaypoints(usize, String),
EitherWaypointsOrTransitMode(usize),
GoogleMapsService(Status, Option<String>),
HttpUnsuccessful(String),
InvalidAvoidCode(String),
InvalidElementStatusCode(String),
InvalidManeuverTypeCode(String),
InvalidStatusCode(String),
InvalidTrafficModelCode(String),
InvalidTransitModeCode(String),
InvalidTransitRoutePreferenceCode(String),
InvalidTravelModeCode(String),
InvalidUnitSystemCode(String),
InvalidVehicleTypeCode(String),
QueryNotBuilt,
RequestNotValidated,
#[cfg(feature = "enable-reqwest")]
Reqwest(reqwest::Error),
#[cfg(feature = "enable-reqwest")]
ReqwestMessage(String),
SerdeJson(serde_json::error::Error),
TooManyWaypoints(usize),
TransitModeIsForTransitOnly(String, String),
TransitRoutePreferenceIsForTransitOnly(String, String),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Error::ArrivalTimeIsForTransitOnly(travel_mode, arrival_time) => write!(f,
"Google Maps Distance Matrix API client: \
The with_arrival_time() method may only be used when with_travel_mode() is set to `TravelMode::Transit`. \
The travel mode is set to `{}` and the arrival time is set to `{}`. \
Try again either with a travel mode of `TravelMode::Transit` or no arrival time.",
travel_mode,
arrival_time),
Error::EitherAlternativesOrWaypoints(waypoint_count) => write!(f,
"Google Maps Distance Matrix API client: \
The with_alternatives() method cannot be set to `true` if with_waypoints() has been set. \
{} waypoint(s) are set. \
Try again either with no waypoints or no alternatives.",
waypoint_count),
Error::EitherDepartureTimeOrArrivalTime(arrival_time, departure_time) => write!(f,
"Google Maps Distance Matrix API client: \
The with_departure_time() method cannot be used when with_arrival_time() has been set. \
The arrival time is set to `{}` and the departure time is set to `{}`. \
Try again either with no arrival time or no departure time.",
arrival_time,
departure_time),
Error::EitherRestrictionsOrWaypoints(waypoint_count, restrictions) => write!(f,
"Google Maps Distance Matrix API client: \
The with_restrictions() method cannot be used when with_waypoints() has been set. \
{} waypoint(s) are set and the restrictions(s) are set to `{}`. \
Try again either with no waypoints or no restrictions.",
waypoint_count,
restrictions),
Error::EitherWaypointsOrTransitMode(waypoint_count) => write!(f,
"Google Maps Distance Matrix API client: \
The with_waypoints() method cannot be used when with_travel_mode() is set to `TravelMode::Transit`. \
{} waypoint(s) are set. \
Try again either with a different travel mode or no waypoints.",
waypoint_count),
Error::GoogleMapsService(status, error_message) => match error_message {
Some(error_message) => write!(f, "Google Maps Distance Matrix API service: {}", error_message),
None => match status {
Status::InvalidRequest => write!(f,
"Google Maps Distance Matrix API service: \
Invalid request. \
This may indicate that the query (address, components, or latlng) is missing, an invalid result type, or an invalid location type."),
Status::MaxElementsExceeded => write!(f,
"Google Maps Distance Matrix API service: \
Maximum elements exceeded. \
The product of origins and destinations exceeds the per-query limit."),
Status::Ok => write!(f,
"Google Maps Distance Matrix API service: \
Ok. \
The request was successful."),
Status::OverDailyLimit => write!(f,
"Google Maps Distance Matrix API service: \
Over daily limit. \
Usage cap has been exceeded, API key is invalid, billing has not been enabled, or method of payment is no longer valid."),
Status::OverQueryLimit => write!(f,
"Google Maps Distance Matrix API service: \
Over query limit. \
Requestor has exceeded quota."),
Status::RequestDenied => write!(f,
"Google Maps Distance Matrix API service: \
Request denied. \
Service did not complete the request."),
Status::UnknownError => write!(f,
"Google Maps Distance Matrix API service: \
Unknown error."),
}
},
Error::HttpUnsuccessful(status) => write!(f,
"Google Maps Distance Matrix API client: \
Could not successfully query the Google Cloud Platform service. \
The service last responded with a `{}` status.", status),
Error::InvalidAvoidCode(avoid_code) => write!(f,
"Google Maps Distance Matrix API client: \
`{}` is not a valid restrictions code. \
Valid codes are `ferries`, `highways`, `indoor`, and `tolls`.",
avoid_code),
Error::InvalidElementStatusCode(element_status_code) => write!(f,
"Google Maps Distance Matrix API client: \
`{}` is not a valid geocoder status code. \
Valid codes are `MAX_ROUTE_LENGTH_EXCEEDED`, `NOT_FOUND`, \
`OK`, and `ZERO_RESULTS`.", element_status_code),
Error::InvalidManeuverTypeCode(maneuver_type_code) => write!(f,
"Google Maps Distance Matrix API client: \
`{}` is not a valid maneuver type code. \
Valid codes are `ferry`, `ferry-train`, `fork-left`, \
`fork-right`, `keep-left`, `keep-right`, `merge`, `ramp-left`, \
`ramp-right`, `roundabout-left`, `roundabout-right`, \
`straight`, `turn-left`, `turn-right`, `turn-sharp-left`, \
`turn-sharp-right`, `turn-slight-left`, `turn-slight-right`, \
`uturn-left`, and `uturn-right`.", maneuver_type_code),
Error::InvalidStatusCode(status_code) => write!(f,
"Google Maps Distance Matrix API client: \
`{}` is not a valid status code. \
Valid codes are `INVALID_REQUEST`, `MAX_ROUTE_LENGTH_EXCEEDED` \
`MAX_WAYPOINTS_EXCEEDED`, `NOT_FOUND`, `OK`, \
`OVER_DAILY_LIMIT`, `OVER_QUERY_LIMIT`, `REQUEST_DENIED`, \
`UNKNOWN_ERROR`, and `ZERO_RESULTS`.", status_code),
Error::InvalidTrafficModelCode(traffic_model_code) => write!(f,
"Google Maps Distance Matrix API client: \
`{}` is not a valid traffic model code. \
Valid codes are `best_guess`, `optimistic`, and `pessimistic`.",
traffic_model_code),
Error::InvalidTransitModeCode(transit_mode_code) => write!(f,
"Google Maps Distance Matrix API client: \
`{}` is not a valid transit mode code. Valid codes are `bus`,
`rail`, `subway`, `train`, and `tram`.", transit_mode_code),
Error::InvalidTransitRoutePreferenceCode(transit_route_preference_code) =>
write!(f, "Google Maps Distance Matrix API client: \
`{}` is not a valid transit route preference code. \
Valid codes are `fewer_transfers` and `less_walking`.",
transit_route_preference_code),
Error::InvalidTravelModeCode(travel_mode_code) => write!(f,
"Google Maps Distance Matrix API client: \
`{}` is not a valid travel mode code. \
Valid codes are `bicycling`, `driving`, `transit`, and \
`walking`.", travel_mode_code),
Error::InvalidUnitSystemCode(unit_system_code) => write!(f,
"Google Maps Distance Matrix API client: \
`{}` is not a valid unit system code. \
Valid codes are `imperial`, and `metric`.", unit_system_code),
Error::InvalidVehicleTypeCode(vehicle_type_code) => write!(f,
"Google Maps Distance Matrix API client: \
`{}` is not a valid vehicle type code. \
Valid codes are `BUS`, `CABLE_CAR`, `COMMUTER_TRAIN`, \
`FERRY`, `FUNICULAR`, `GONDOLA_LIFT`, `HEAVY_RAIL`, \
`HIGH_SPEED_TRAIN`, `INTERCITY_BUS`, `LONG_DISTANCE_TRAIN`, \
`METRO_RAIL`, `MONORAIL`, `OTHER`, `RAIL`, `SHARE_TAXI`, \
`SUBWAY`, `TRAM`, and `TROLLEYBUS`.", vehicle_type_code),
Error::QueryNotBuilt => write!(f,
"Google Maps Distance Matrix API client: \
The query string must be built before the request may be sent to the Google Cloud Maps Platform. \
Ensure the build() method is called before run()."),
Error::RequestNotValidated => write!(f,
"Google Maps Distance Matrix API client: \
The request must be validated before a query string may be built. \
Ensure the validate() method is called before build()."),
#[cfg(feature = "enable-reqwest")]
Error::Reqwest(error) => write!(f, "Google Maps Distance Matrix API client in the Reqwest library: {}", error),
#[cfg(feature = "enable-reqwest")]
Error::ReqwestMessage(error) => write!(f, "Google Maps Geocoding API client in the Reqwest library: {}", error),
Error::SerdeJson(error) => write!(f, "Google Maps Distance Matrix API client in the Serde JSON library: {}", error),
Error::TooManyWaypoints(waypoint_count) => write!(f,
"Google Maps Distance Matrix API client: \
The maximum allowed number of waypoints is 25 plus the origin and destination. \
{} waypoints are set. \
Try again with {} fewer waypoint(s).",
waypoint_count,
waypoint_count - 25),
Error::TransitModeIsForTransitOnly(travel_mode, transit_modes) => write!(f,
"Google Maps Distance Matrix API client: \
The with_transit_modes() method may only be used when with_travel_mode() is set to `TravelMode::Transit`. \
The travel mode is set to `{}` and the transit mode(s) are set to `{}`. \
Try again either with a travel mode of `TravelMode::Transit` or no transit modes.",
travel_mode,
transit_modes),
Error::TransitRoutePreferenceIsForTransitOnly(travel_mode, transit_route_preference) => write!(f,
"Google Maps Distance Matrix API client: \
The with_transit_route_preference() method may only be used when with_travel_mode() is set to `TravelMode::Transit`. \
The travel mode is set to `{}` and the transit route preference is set to `{}`. \
Try again either with a travel mode of `TravelMode::Transit` or no transit route preference.",
travel_mode,
transit_route_preference),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::ArrivalTimeIsForTransitOnly(_travel_mode, _arrival_time) => None,
Error::EitherAlternativesOrWaypoints(_waypoint_count) => None,
Error::EitherDepartureTimeOrArrivalTime(_arrival_time, _departure_time) => None,
Error::EitherRestrictionsOrWaypoints(_waypoint_count, _restrictions) => None,
Error::EitherWaypointsOrTransitMode(_waypoint_count) => None,
Error::GoogleMapsService(_error, _message) => None,
Error::HttpUnsuccessful(_status) => None,
Error::InvalidAvoidCode(_avoid_code) => None,
Error::InvalidElementStatusCode(_element_status_code) => None,
Error::InvalidManeuverTypeCode(_maneuver_type_code) => None,
Error::InvalidStatusCode(_status_code) => None,
Error::InvalidTrafficModelCode(_traffic_model_code) => None,
Error::InvalidTransitModeCode(_transit_mode_code) => None,
Error::InvalidTransitRoutePreferenceCode(_transit_route_preference_code) => None,
Error::InvalidTravelModeCode(_travel_mode_code) => None,
Error::InvalidUnitSystemCode(_unit_system_code) => None,
Error::InvalidVehicleTypeCode(_vehicle_type_code) => None,
Error::QueryNotBuilt => None,
Error::RequestNotValidated => None,
#[cfg(feature = "enable-reqwest")]
Error::Reqwest(error) => Some(error),
#[cfg(feature = "enable-reqwest")]
Error::ReqwestMessage(_error) => None,
Error::SerdeJson(error) => Some(error),
Error::TooManyWaypoints(_waypoint_count) => None,
Error::TransitModeIsForTransitOnly(_travel_mode, _transit_modes) => None,
Error::TransitRoutePreferenceIsForTransitOnly(
_travel_mode,
_transit_route_preference,
) => None,
}
}
}
#[cfg(feature = "enable-reqwest")]
impl From<reqwest::Error> for Error {
fn from(error: reqwest::Error) -> Error {
Error::Reqwest(error)
}
}
impl From<serde_json::error::Error> for Error {
fn from(error: serde_json::error::Error) -> Error {
Error::SerdeJson(error)
}
}