use crate::directions::response::status::Status;
use miette::Diagnostic;
use thiserror::Error;
#[derive(Debug, Diagnostic, Error)]
#[diagnostic(code(google_maps::directions::error), url(docsrs))]
pub enum Error {
ArrivalTimeIsForTransitOnly(String, String),
EitherAlternativesOrWaypoints(usize),
EitherDepartureTimeOrArrivalTime(String, String),
EitherRestrictionsOrWaypoints(usize, String),
EitherWaypointsOrTransitMode(usize),
GoogleMapsService(Status, Option<String>),
HttpUnsuccessful(String),
InvalidAvoidCode(String),
InvalidCurrencyCode(String),
InvalidGeocoderStatusCode(String),
InvalidDrivingManeuverCode(String),
InvalidStatusCode(String),
InvalidTimeZoneName(String),
InvalidTrafficModelCode(String),
InvalidTransitModeCode(String),
InvalidTransitRoutePreferenceCode(String),
InvalidTravelModeCode(String),
InvalidUnitSystemCode(String),
InvalidVehicleTypeCode(String),
InvalidDepartureTime(String),
QueryNotBuilt,
RequestNotValidated,
#[cfg(feature = "enable-reqwest")]
Reqwest(crate::ReqError),
#[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 {
Self::ArrivalTimeIsForTransitOnly(travel_mode, arrival_time) => write!(f,
"Google Maps Directions 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 `{travel_mode}` and the arrival time is set to `{arrival_time}`. \
Try again either with a travel mode of `TravelMode::Transit` or no arrival time."),
Self::EitherAlternativesOrWaypoints(waypoint_count) => write!(f,
"Google Maps Directions API client: \
The with_alternatives() method cannot be set to `true` if with_waypoints() has been set. \
{waypoint_count} waypoint(s) are set. \
Try again either with no waypoints or no alternatives."),
Self::EitherDepartureTimeOrArrivalTime(arrival_time, departure_time) => write!(f,
"Google Maps Directions API client: \
The with_departure_time() method cannot be used when with_arrival_time() has been set. \
The arrival time is set to `{arrival_time}` and the departure time is set to `{departure_time}`. \
Try again either with no arrival time or no departure time."),
Self::EitherRestrictionsOrWaypoints(waypoint_count, restrictions) => write!(f,
"Google Maps Directions API client: \
The with_restrictions() method cannot be used when with_waypoints() has been set. \
{waypoint_count} waypoint(s) are set and the restriction(s) are set to `{restrictions}`. \
Try again either with no waypoints or no restrictions."),
Self::EitherWaypointsOrTransitMode(waypoint_count) => write!(f,
"Google Maps Directions API client: \
The with_waypoints() method cannot be used when with_travel_mode() is set to `TravelMode::Transit`. \
{waypoint_count} waypoint(s) are set. \
Try again either with a different travel mode or no waypoints."),
Self::GoogleMapsService(status, error_message) => match error_message {
Some(error_message) => write!(f, "Google Maps Directions API service: {error_message}"),
None => match status {
Status::InvalidRequest => write!(f, "Google Maps Directions 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::MaxRouteLengthExceeded => write!(f, "Google Maps Directions API service: \
Maximum route length exceeded. \
Try reducing the number of waypoints, turns, or instructions."),
Status::MaxWaypointsExceeded => write!(f, "Google Maps Directions API service: \
Maximum waypoints exceeded. \
The maximum allowed number of waypoints is 25, plus the origin and destination."),
Status::NotFound => write!(f, "Google Maps Directions API service: \
Not found. \
An origin, destination, or waypoint could not be geocoded."),
Status::Ok => write!(f, "Google Maps Directions server: \
Ok. \
The request was successful."),
Status::OverDailyLimit => write!(f, "Google Maps Directions 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 Directions API service: \
Over query limit. \
Requestor has exceeded quota."),
Status::RequestDenied => write!(f, "Google Maps Directions API service: \
Request denied. \
Service did not complete the request."),
Status::UnknownError => write!(f, "Google Maps Directions API service: \
Unknown error."),
Status::ZeroResults => write!(f, "Google Maps Directions API service: \
Zero results. \
This may occur if the geocoder was passed a non-existent address."),
} }, Self::HttpUnsuccessful(status) => write!(f,
"Google Maps Directions API client: \
Could not successfully query the Google Cloud Platform service. \
The service last responded with a `{status}` status."),
Self::InvalidAvoidCode(avoid_code) => write!(f,
"Google Maps Directions API client: \
`{avoid_code}` is not a valid restrictions code. \
Valid codes are `ferries`, `highways`, `indoor`, and `tolls`."),
Self::InvalidCurrencyCode(currency_code) => write!(f,
"Google Maps Directions API client: \
`{currency_code}` is not a recognized currency code. \
For a list of supported currencies see \
https://en.wikipedia.org/wiki/ISO_4217"),
Self::InvalidGeocoderStatusCode(geocoder_status_code) => write!(f,
"Google Maps Directions API client: \
`{geocoder_status_code}` is not a valid geocoder status code. \
Valid codes are `OK`, and `ZERO_RESULTS`."),
Self::InvalidDrivingManeuverCode(maneuver_type_code) => write!(f,
"Google Maps Directions API client: \
`{maneuver_type_code}` 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`."),
Self::InvalidStatusCode(status_code) => write!(f,
"Google Maps Directions API client: \
`{status_code}` 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`."),
Self::InvalidTimeZoneName(time_zone_name) => write!(f,
"Google Maps Directions API client: \
`{time_zone_name}` is not a recognized time zone name. \
For a list of supported time zones see \
https://www.iana.org/time-zones"),
Self::InvalidTrafficModelCode(traffic_model_code) => write!(f,
"Google Maps Directions API client: \
`{traffic_model_code}` is not a valid traffic model code. \
Valid codes are `best_guess`, `optimistic`, and `pessimistic`."),
Self::InvalidTransitModeCode(transit_mode_code) => write!(f,
"Google Maps Directions API client: \
`{transit_mode_code}` is not a valid transit mode code. Valid codes are `bus`,
`rail`, `subway`, `train`, and `tram`."),
Self::InvalidTransitRoutePreferenceCode(transit_route_preference_code) =>
write!(f, "Google Maps Directions API client: \
`{transit_route_preference_code}` is not a valid transit route preference code. \
Valid codes are `fewer_transfers` and `less_walking`."),
Self::InvalidTravelModeCode(travel_mode_code) => write!(f,
"Google Maps Directions API client: \
`{travel_mode_code}` is not a valid travel mode code. \
Valid codes are `bicycling`, `driving`, `transit`, and \
`walking`."),
Self::InvalidUnitSystemCode(unit_system_code) => write!(f,
"Google Maps Directions API client: \
`{unit_system_code}` is not a valid unit system code. \
Valid codes are `imperial`, and `metric`."),
Self::InvalidVehicleTypeCode(vehicle_type_code) => write!(f,
"Google Maps Directions API client: \
`{vehicle_type_code}` 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`."),
Self::InvalidDepartureTime(invalid_departure_time_string) => write!(f,
"Google Maps Directions API client: \
`{invalid_departure_time_string}` is not a valid departure time. \
Valid departure times are `now`, or a UNIX timestamp."
),
Self::QueryNotBuilt => write!(f,
"Google Maps Directions 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()."),
Self::RequestNotValidated => write!(f,
"Google Maps Directions 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")]
Self::Reqwest(error) => write!(f, "Google Maps Directions API client in the Reqwest library: {error}"),
#[cfg(feature = "enable-reqwest")]
Self::ReqwestMessage(error) => write!(f, "Google Maps Geocoding API client in the Reqwest library: {error}"),
Self::SerdeJson(error) => write!(f, "Google Maps Directions API client in the Serde JSON library: {error}"),
Self::TooManyWaypoints(waypoint_count) => write!(f,
"Google Maps Directions 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),
Self::TransitModeIsForTransitOnly(travel_mode, transit_modes) => write!(f,
"Google Maps Directions 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 `{travel_mode}` and the transit mode(s) are set to `{transit_modes}`. \
Try again either with a travel mode of `TravelMode::Transit` or no transit modes."),
Self::TransitRoutePreferenceIsForTransitOnly(travel_mode, transit_route_preference) => write!(f,
"Google Maps Directions 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 `{travel_mode}` and the transit route preference is set to `{transit_route_preference}`. \
Try again either with a travel mode of `TravelMode::Transit` or no transit route preference."),
} } }
#[cfg(feature = "enable-reqwest")]
impl From<crate::ReqError> for Error {
fn from(error: crate::ReqError) -> Self {
Self::Reqwest(error)
} }
impl From<serde_json::error::Error> for Error {
fn from(error: serde_json::error::Error) -> Self {
Self::SerdeJson(error)
} }