1use std::fmt;
6use std::borrow::Cow;
7use std::vec;
8use url::Url;
9
10#[derive(Clone, Copy, Debug, Eq, PartialEq)]
12pub enum AuthorizationErrorType {
13 InvalidRequest,
16
17 UnauthorizedClient,
19
20 AccessDenied,
22
23 UnsupportedResponseType,
25
26 InvalidScope,
28
29 ServerError,
33
34 TemporarilyUnavailable,
38}
39
40impl AuthorizationErrorType {
41 fn description(self) -> &'static str {
42 match self {
43 AuthorizationErrorType::InvalidRequest => "invalid_request",
44 AuthorizationErrorType::UnauthorizedClient => "unauthorized_client",
45 AuthorizationErrorType::AccessDenied => "access_denied",
46 AuthorizationErrorType::UnsupportedResponseType => "unsupported_response_type",
47 AuthorizationErrorType::InvalidScope => "invalid_scope",
48 AuthorizationErrorType::ServerError => "server_error",
49 AuthorizationErrorType::TemporarilyUnavailable => "temporarily_unavailable",
50 }
51 }
52}
53
54#[derive(Clone, Debug)]
58pub struct AuthorizationError {
59 error: AuthorizationErrorType,
60 description: Option<Cow<'static, str>>,
61 uri: Option<Cow<'static, str>>,
62}
63
64impl AuthorizationError {
65 #[allow(dead_code)]
66 pub(crate) fn new(error: AuthorizationErrorType) -> Self {
67 AuthorizationError {
68 error,
69 description: None,
70 uri: None,
71 }
72 }
73
74 pub fn set_type(&mut self, new_type: AuthorizationErrorType) {
76 self.error = new_type;
77 }
78
79 pub fn kind(&mut self) -> AuthorizationErrorType {
83 self.error
84 }
85
86 pub fn explain<D: Into<Cow<'static, str>>>(&mut self, description: D) {
88 self.description = Some(description.into())
89 }
90
91 pub fn explain_uri(&mut self, uri: Url) {
93 self.uri = Some(String::from(uri).into())
94 }
95
96 pub fn iter(&self) -> <Self as IntoIterator>::IntoIter {
102 self.into_iter()
103 }
104}
105
106#[derive(Clone, Copy, Debug, Eq, PartialEq)]
110pub enum AccessTokenErrorType {
111 InvalidRequest,
115
116 InvalidClient,
124
125 InvalidGrant,
129
130 UnauthorizedClient,
132
133 UnsupportedGrantType,
135
136 InvalidScope,
139}
140
141impl AccessTokenErrorType {
142 fn description(self) -> &'static str {
143 match self {
144 AccessTokenErrorType::InvalidRequest => "invalid_request",
145 AccessTokenErrorType::InvalidClient => "invalid_client",
146 AccessTokenErrorType::InvalidGrant => "invalid_grant",
147 AccessTokenErrorType::UnauthorizedClient => "unauthorized_client",
148 AccessTokenErrorType::UnsupportedGrantType => "unsupported_grant_type",
149 AccessTokenErrorType::InvalidScope => "invalid_scope",
150 }
151 }
152}
153
154#[derive(Clone, Debug)]
162pub struct AccessTokenError {
163 error: AccessTokenErrorType,
164 description: Option<Cow<'static, str>>,
165 uri: Option<Cow<'static, str>>,
166}
167
168impl AccessTokenError {
169 pub(crate) fn new(error: AccessTokenErrorType) -> Self {
170 AccessTokenError {
171 error,
172 description: None,
173 uri: None,
174 }
175 }
176
177 pub fn set_type(&mut self, new_type: AccessTokenErrorType) {
179 self.error = new_type;
180 }
181
182 pub fn kind(&mut self) -> AccessTokenErrorType {
186 self.error
187 }
188
189 pub fn explain<D: Into<Cow<'static, str>>>(&mut self, description: D) {
191 self.description = Some(description.into())
192 }
193
194 pub fn explain_uri(&mut self, uri: Url) {
196 self.uri = Some(String::from(uri).into())
197 }
198
199 pub fn iter(&self) -> <Self as IntoIterator>::IntoIter {
204 self.into_iter()
205 }
206}
207
208impl Default for AuthorizationError {
209 fn default() -> Self {
214 AuthorizationError {
215 error: AuthorizationErrorType::InvalidRequest,
216 description: None,
217 uri: None,
218 }
219 }
220}
221
222impl AsRef<str> for AuthorizationErrorType {
223 fn as_ref(&self) -> &str {
224 self.description()
225 }
226}
227
228impl fmt::Display for AuthorizationErrorType {
229 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
230 write!(f, "{}", self.as_ref())
231 }
232}
233
234impl Default for AccessTokenError {
235 fn default() -> Self {
240 AccessTokenError {
241 error: AccessTokenErrorType::InvalidRequest,
242 description: None,
243 uri: None,
244 }
245 }
246}
247
248impl AsRef<str> for AccessTokenErrorType {
249 fn as_ref(&self) -> &str {
250 self.description()
251 }
252}
253
254impl fmt::Display for AccessTokenErrorType {
255 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
256 write!(f, "{}", self.as_ref())
257 }
258}
259
260impl IntoIterator for AuthorizationError {
262 type Item = (&'static str, Cow<'static, str>);
263 type IntoIter = vec::IntoIter<(&'static str, Cow<'static, str>)>;
264
265 fn into_iter(self) -> Self::IntoIter {
266 let mut vec = vec![("error", Cow::Borrowed(self.error.description()))];
267 if let Some(description) = self.description {
268 vec.push(("description", description));
269 }
270 if let Some(uri) = self.uri {
271 vec.push(("uri", uri));
272 }
273 vec.into_iter()
274 }
275}
276
277impl IntoIterator for &'_ AuthorizationError {
278 type Item = (&'static str, Cow<'static, str>);
279 type IntoIter = vec::IntoIter<(&'static str, Cow<'static, str>)>;
280
281 fn into_iter(self) -> Self::IntoIter {
282 let mut vec = vec![("error", Cow::Borrowed(self.error.description()))];
283 if let Some(description) = &self.description {
284 vec.push(("description", description.clone().to_owned()));
285 }
286 if let Some(uri) = &self.uri {
287 vec.push(("uri", uri.clone().to_owned()));
288 }
289 vec.into_iter()
290 }
291}
292
293impl IntoIterator for AccessTokenError {
295 type Item = (&'static str, Cow<'static, str>);
296 type IntoIter = vec::IntoIter<(&'static str, Cow<'static, str>)>;
297
298 fn into_iter(self) -> Self::IntoIter {
299 let mut vec = vec![("error", Cow::Borrowed(self.error.description()))];
300 if let Some(description) = self.description {
301 vec.push(("description", description));
302 }
303 if let Some(uri) = self.uri {
304 vec.push(("uri", uri));
305 }
306 vec.into_iter()
307 }
308}
309
310impl IntoIterator for &'_ AccessTokenError {
311 type Item = (&'static str, Cow<'static, str>);
312 type IntoIter = vec::IntoIter<(&'static str, Cow<'static, str>)>;
313
314 fn into_iter(self) -> Self::IntoIter {
315 let mut vec = vec![("error", Cow::Borrowed(self.error.description()))];
316 if let Some(description) = &self.description {
317 vec.push(("description", description.clone().to_owned()));
318 }
319 if let Some(uri) = &self.uri {
320 vec.push(("uri", uri.clone().to_owned()));
321 }
322 vec.into_iter()
323 }
324}