google_maps/elevation/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::elevation::error::Error;
5use phf::phf_map;
6use serde::{Deserialize, Deserializer, Serialize};
7
8// -----------------------------------------------------------------------------
9//
10/// Indicates the status of the response.
11#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
12#[serde(rename_all(serialize = "SCREAMING_SNAKE_CASE", deserialize = "SCREAMING_SNAKE_CASE"))]
13pub enum Status {
14 /// Indicates that the request was malformed.
15 #[serde(alias = "InvalidRequest")]
16 InvalidRequest,
17
18 /// Indicates that the request was successful.
19 #[serde(alias = "Ok")]
20 Ok,
21
22 /// Indicates any of the following:
23 /// * The API key is missing or invalid.
24 /// * Billing has not been enabled on your account.
25 /// * A self-imposed usage cap has been exceeded.
26 /// * The provided method of payment is no longer valid (for example, a
27 /// credit card has expired).
28 ///
29 /// See the [Maps
30 /// FAQ](https://developers.google.com/maps/faq#over-limit-key-error) to
31 /// learn how to fix this.
32 #[serde(alias = "OverDailyLimit")]
33 OverDailyLimit,
34
35 /// Indicates the requestor has exceeded quota.
36 #[serde(alias = "OverQueryLimit")]
37 OverQueryLimit,
38
39 /// Indicates that the API did not complete the request.
40 #[serde(alias = "RequestDenied")]
41 RequestDenied,
42
43 /// Indicates an unknown error.
44 #[serde(alias = "UnknownError")]
45 UnknownError,
46} // enum
47
48// -----------------------------------------------------------------------------
49
50impl<'de> Deserialize<'de> for Status {
51 /// Manual implementation of `Deserialize` for `serde`. This will take
52 /// advantage of the `phf`-powered `TryFrom` implementation for this type.
53 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
54 let string = String::deserialize(deserializer)?;
55 match Self::try_from(string.as_str()) {
56 Ok(variant) => Ok(variant),
57 Err(error) => Err(serde::de::Error::custom(error.to_string())),
58 } // match
59 } // fn
60} // impl
61
62// -----------------------------------------------------------------------------
63
64impl std::convert::From<&Status> for String {
65 /// Converts a `Status` enum to a `String` that contains a
66 /// [status](https://developers.google.com/maps/documentation/elevation/intro#ElevationResponses)
67 /// code.
68 fn from(status: &Status) -> Self {
69 match status {
70 Status::InvalidRequest => Self::from("INVALID_REQUEST"),
71 Status::Ok => Self::from("OK"),
72 Status::OverDailyLimit => Self::from("OVER_DAILY_LIMIT"),
73 Status::OverQueryLimit => Self::from("OVER_QUERY_LIMIT"),
74 Status::RequestDenied => Self::from("REQUEST_DENIED"),
75 Status::UnknownError => Self::from("UNKNOWN_ERROR"),
76 } // match
77 } // fn
78} // impl
79
80// -----------------------------------------------------------------------------
81
82static STATUSES_BY_CODE: phf::Map<&'static str, Status> = phf_map! {
83 "INVALID_REQUEST" => Status::InvalidRequest,
84 "OK" => Status::Ok,
85 "OVER_DAILY_LIMIT" => Status::OverDailyLimit,
86 "OVER_QUERY_LIMIT" => Status::OverQueryLimit,
87 "REQUEST_DENIED" => Status::RequestDenied,
88 "UNKNOWN_ERROR" => Status::UnknownError,
89};
90
91impl std::convert::TryFrom<&str> for Status {
92 // Error definitions are contained in the
93 // `google_maps\src\elevation\error.rs` module.
94 type Error = crate::elevation::error::Error;
95 /// Gets a `Status` enum from a `String` that contains a valid
96 /// [status](https://developers.google.com/maps/documentation/elevation/intro#ElevationResponses)
97 /// code.
98 fn try_from(status_code: &str) -> Result<Self, Self::Error> {
99 STATUSES_BY_CODE
100 .get(status_code)
101 .cloned()
102 .ok_or_else(|| Error::InvalidStatusCode(status_code.to_string()))
103 } // fn
104} // impl
105
106impl std::str::FromStr for Status {
107 // Error definitions are contained in the
108 // `google_maps\src\elevation\error.rs` module.
109 type Err = crate::elevation::error::Error;
110 /// Gets a `Status` enum from a `String` that contains a valid
111 /// [status](https://developers.google.com/maps/documentation/elevation/intro#ElevationResponses)
112 /// code.
113 fn from_str(status_code: &str) -> Result<Self, Self::Err> {
114 STATUSES_BY_CODE
115 .get(status_code)
116 .cloned()
117 .ok_or_else(|| Error::InvalidStatusCode(status_code.to_string()))
118 } // fn
119} // impl
120
121// -----------------------------------------------------------------------------
122
123impl std::default::Default for Status {
124 /// Returns a reasonable default variant for the `Status` enum type.
125 fn default() -> Self {
126 Self::Ok
127 } // fn
128} // impl
129
130// -----------------------------------------------------------------------------
131
132impl std::fmt::Display for Status {
133 /// Formats a `Status` enum into a string that is presentable to the end
134 /// user.
135 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
136 match self {
137 Self::InvalidRequest => write!(f, "Invalid Request"),
138 Self::Ok => write!(f, "OK"),
139 Self::OverDailyLimit => write!(f, "Over Daily Limit"),
140 Self::OverQueryLimit => write!(f, "Over Query Limit"),
141 Self::RequestDenied => write!(f, "Request Denied"),
142 Self::UnknownError => write!(f, "Unknown Error"),
143 } // match
144 } // fn
145} // impl