use crate::directions::response::status::Status;
#[derive(Debug)]
pub enum Error {
ArrivalTimeIsForTransitOnly(String, String),
EitherAlternativesOrWaypoints(usize),
EitherDepartureTimeOrArrivalTime(String, String),
EitherRestrictionsOrWaypoints(usize, String),
EitherWaypointsOrTransitMode(usize),
GoogleMapsDirectionsServer(Status, Option<String>),
QueryNotBuilt,
RequestNotValidated,
Reqwest(reqwest::Error),
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 Directions API client library: \
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 Directions API client library: \
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 Directions API client library: \
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 Directions API client library: \
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 Directions API client library: \
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::GoogleMapsDirectionsServer(status, error_message) => match error_message {
Some(error_message) => write!(f, "Google Maps Directions API server: {}", error_message),
None => match status {
Status::InvalidRequest => write!(f, "Google Maps Directions API server: \
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 server:"),
Status::MaxWaypointsExceeded => write!(f, "Google Maps Directions API server:"),
Status::NotFound => write!(f, "Google Maps Directions API server:"),
Status::Ok => write!(f, "Google Maps Directions server: \
Ok. \
The request was successful."),
Status::OverDailyLimit => write!(f, "Google Maps Directions API server: \
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 server: \
Over query limit. \
Requestor has exceeded quota."),
Status::RequestDenied => write!(f, "Google Maps Directions API server: \
Request denied \
Service did not complete the request."),
Status::UnknownError => write!(f, "Google Maps Directions API server: \
Unknown error."),
Status::ZeroResults => write!(f, "Google Maps Directions API server: \
Zero results.
This may occur if the geocoder was passed a non-existent address."),
}
},
Error::QueryNotBuilt => write!(f, "Google Maps Directions API client library: \
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 Directions API client library: \
The request must be validated before a query string may be built. \
Ensure the validate() method is called before build()."),
Error::Reqwest(error) => write!(f, "Google Maps Directions API client in the Reqwest library: {}", error),
Error::SerdeJson(error) => write!(f, "Google Maps Directions API client in the Serde JSON library: {}", error),
Error::TooManyWaypoints(waypoint_count) => write!(f,
"Google Directions API client library: \
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 Directions API client library: \
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 Directions API client library: \
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::GoogleMapsDirectionsServer(_error, _message) => None,
Error::QueryNotBuilt => None,
Error::RequestNotValidated => None,
Error::Reqwest(error) => Some(error),
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,
}
}
}
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)
}
}