google_maps/distance_matrix/response/status.rs
1//! Contains the `Status` enum and its associated traits. It may contain
2//! debugging information to help you track down why the service request failed.
3
4use crate::distance_matrix::error::Error;
5use phf::phf_map;
6use serde::{Deserialize, Deserializer, Serialize};
7
8// -----------------------------------------------------------------------------
9//
10/// The `status` fields within the response object contain the status of the
11/// request, and may contain useful debugging information. The Distance Matrix
12/// API returns a top-level status field, with information about the request in
13/// general, as well as a status field for each element field, with information
14/// about that particular origin-destination pairing.
15#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
16#[serde(rename_all(serialize = "SCREAMING_SNAKE_CASE", deserialize = "SCREAMING_SNAKE_CASE"))]
17pub enum Status {
18 /// Indicates that the provided request was invalid. Common causes of this
19 /// status include an invalid parameter or parameter value.
20 #[serde(alias = "InvalidRequest")]
21 InvalidRequest,
22
23 /// Indicates that the product of origins and destinations exceeds the
24 /// per-query
25 /// [limit](https://developers.google.com/maps/documentation/distance-matrix/usage-and-billing).
26 #[serde(alias = "MaxElementsExceeded")]
27 MaxElementsExceeded,
28
29 /// Indicates that the number of origins or destinations exceeds the
30 /// per-query [limit](https://developers.google.com/maps/documentation/distance-matrix/usage-and-billing).
31 #[serde(alias = "MaxDimensionsExceeded")]
32 MaxDimensionsExceeded,
33
34 /// Indicates the response contains a valid `result`.
35 #[serde(alias = "Ok")]
36 Ok,
37
38 /// Indicates any of the following:
39 /// * The API key is missing or invalid.
40 /// * Billing has not been enabled on your account.
41 /// * A self-imposed usage cap has been exceeded.
42 /// * The provided method of payment is no longer valid (for example, a
43 /// credit card has expired).
44 ///
45 /// See the [Maps
46 /// FAQ](https://developers.google.com/maps/faq#over-limit-key-error) to
47 /// learn how to fix this.
48 #[serde(alias = "OverDailyLimit")]
49 OverDailyLimit,
50
51 /// Indicates the service has received too many requests from your
52 /// application within the allowed time period.
53 #[serde(alias = "OverQueryLimit")]
54 OverQueryLimit,
55
56 /// Indicates that the service denied use of the Distance Matrix service by
57 /// your application.
58 #[serde(alias = "RequestDenied")]
59 RequestDenied,
60
61 /// Indicates a Distance Matrix request could not be processed due to a
62 /// server error. The request may succeed if you try again.
63 #[serde(alias = "UnknownError")]
64 UnknownError,
65} // enum
66
67// -----------------------------------------------------------------------------
68
69impl<'de> Deserialize<'de> for Status {
70 /// Manual implementation of `Deserialize` for `serde`. This will take
71 /// advantage of the `phf`-powered `TryFrom` implementation for this type.
72 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
73 let string = String::deserialize(deserializer)?;
74 match Self::try_from(string.as_str()) {
75 Ok(variant) => Ok(variant),
76 Err(error) => Err(serde::de::Error::custom(error.to_string())),
77 } // match
78 } // fn
79} // impl
80
81// -----------------------------------------------------------------------------
82
83impl std::convert::From<&Status> for String {
84 /// Converts a `Status` enum to a `String` that contains a
85 /// [status](https://developers.google.com/maps/documentation/distance-matrix/intro#top-level-status-codes)
86 /// code.
87 fn from(status: &Status) -> Self {
88 match status {
89 Status::InvalidRequest => Self::from("INVALID_REQUEST"),
90 Status::MaxDimensionsExceeded => Self::from("MAX_DIMENSIONS_EXCEEDED"),
91 Status::MaxElementsExceeded => Self::from("MAX_ELEMENTS_EXCEEDED"),
92 Status::Ok => Self::from("OK"),
93 Status::OverDailyLimit => Self::from("OVER_DAILY_LIMIT"),
94 Status::OverQueryLimit => Self::from("OVER_QUERY_LIMIT"),
95 Status::RequestDenied => Self::from("REQUEST_DENIED"),
96 Status::UnknownError => Self::from("UNKNOWN_ERROR"),
97 } // match
98 } // fn
99} // impl
100
101// -----------------------------------------------------------------------------
102
103static STATUSES_BY_CODE: phf::Map<&'static str, Status> = phf_map! {
104 "INVALID_REQUEST" => Status::InvalidRequest,
105 "MAX_DIMENSIONS_EXCEEDED" => Status::MaxDimensionsExceeded,
106 "MAX_ELEMENTS_EXCEEDED" => Status::MaxElementsExceeded,
107 "OK" => Status::Ok,
108 "OVER_DAILY_LIMIT" => Status::OverDailyLimit,
109 "OVER_QUERY_LIMIT" => Status::OverQueryLimit,
110 "REQUEST_DENIED" => Status::RequestDenied,
111 "UNKNOWN_ERROR" => Status::UnknownError,
112};
113
114impl std::convert::TryFrom<&str> for Status {
115 // Error definitions are contained in the
116 // `google_maps\src\distance_matrix\error.rs` module.
117 type Error = crate::distance_matrix::error::Error;
118 /// Gets a `Status` enum from a `String` that contains a valid
119 /// [status](https://developers.google.com/maps/documentation/distance-matrix/intro#top-level-status-codes)
120 /// code.
121 fn try_from(status_code: &str) -> Result<Self, Self::Error> {
122 STATUSES_BY_CODE
123 .get(status_code)
124 .cloned()
125 .ok_or_else(|| Error::InvalidStatusCode(status_code.to_string()))
126 } // fn
127} // impl
128
129impl std::str::FromStr for Status {
130 // Error definitions are contained in the
131 // `google_maps\src\distance_matrix\error.rs` module.
132 type Err = crate::distance_matrix::error::Error;
133 /// Gets a `Status` enum from a `String` that contains a valid
134 /// [status](https://developers.google.com/maps/documentation/distance-matrix/intro#top-level-status-codes)
135 /// code.
136 fn from_str(status_code: &str) -> Result<Self, Self::Err> {
137 STATUSES_BY_CODE
138 .get(status_code)
139 .cloned()
140 .ok_or_else(|| Error::InvalidStatusCode(status_code.to_string()))
141 } // fn
142} // impl
143
144// -----------------------------------------------------------------------------
145
146impl std::default::Default for Status {
147 /// Returns a reasonable default variant for the `Status` enum type.
148 fn default() -> Self {
149 Self::Ok
150 } // fn
151} // impl
152
153// -----------------------------------------------------------------------------
154
155impl std::fmt::Display for Status {
156 /// Formats a `Status` enum into a string that is presentable to the end
157 /// user.
158 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
159 match self {
160 Self::InvalidRequest => write!(f, "Invalid Request"),
161 Self::MaxDimensionsExceeded => write!(f, "Maximum Dimensions Exceeded"),
162 Self::MaxElementsExceeded => write!(f, "Maximum Elements Exceeded"),
163 Self::Ok => write!(f, "OK"),
164 Self::OverDailyLimit => write!(f, "Over Daily Limit"),
165 Self::OverQueryLimit => write!(f, "Over Query Limit"),
166 Self::RequestDenied => write!(f, "Request Denied"),
167 Self::UnknownError => write!(f, "Unknown Error"),
168 } // match
169 } // fn
170} // impl