use std::fmt;
use std::borrow::Cow;
use std::vec;
use url::Url;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AuthorizationErrorType {
InvalidRequest,
UnauthorizedClient,
AccessDenied,
UnsupportedResponseType,
InvalidScope,
ServerError,
TemporarilyUnavailable,
}
impl AuthorizationErrorType {
fn description(self) -> &'static str {
match self {
AuthorizationErrorType::InvalidRequest => "invalid_request",
AuthorizationErrorType::UnauthorizedClient => "unauthorized_client",
AuthorizationErrorType::AccessDenied => "access_denied",
AuthorizationErrorType::UnsupportedResponseType => "unsupported_response_type",
AuthorizationErrorType::InvalidScope => "invalid_scope",
AuthorizationErrorType::ServerError => "server_error",
AuthorizationErrorType::TemporarilyUnavailable => "temporarily_unavailable",
}
}
}
#[derive(Clone, Debug)]
pub struct AuthorizationError {
error: AuthorizationErrorType,
description: Option<Cow<'static, str>>,
uri: Option<Cow<'static, str>>,
}
impl AuthorizationError {
#[allow(dead_code)]
pub(crate) fn new(error: AuthorizationErrorType) -> Self {
AuthorizationError {
error,
description: None,
uri: None,
}
}
pub fn set_type(&mut self, new_type: AuthorizationErrorType) {
self.error = new_type;
}
pub fn kind(&mut self) -> AuthorizationErrorType {
self.error
}
pub fn explain<D: Into<Cow<'static, str>>>(&mut self, description: D) {
self.description = Some(description.into())
}
pub fn explain_uri(&mut self, uri: Url) {
self.uri = Some(String::from(uri).into())
}
pub fn iter(&self) -> <Self as IntoIterator>::IntoIter {
self.into_iter()
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AccessTokenErrorType {
InvalidRequest,
InvalidClient,
InvalidGrant,
UnauthorizedClient,
UnsupportedGrantType,
InvalidScope,
}
impl AccessTokenErrorType {
fn description(self) -> &'static str {
match self {
AccessTokenErrorType::InvalidRequest => "invalid_request",
AccessTokenErrorType::InvalidClient => "invalid_client",
AccessTokenErrorType::InvalidGrant => "invalid_grant",
AccessTokenErrorType::UnauthorizedClient => "unauthorized_client",
AccessTokenErrorType::UnsupportedGrantType => "unsupported_grant_type",
AccessTokenErrorType::InvalidScope => "invalid_scope",
}
}
}
#[derive(Clone, Debug)]
pub struct AccessTokenError {
error: AccessTokenErrorType,
description: Option<Cow<'static, str>>,
uri: Option<Cow<'static, str>>,
}
impl AccessTokenError {
pub(crate) fn new(error: AccessTokenErrorType) -> Self {
AccessTokenError {
error,
description: None,
uri: None,
}
}
pub fn set_type(&mut self, new_type: AccessTokenErrorType) {
self.error = new_type;
}
pub fn kind(&mut self) -> AccessTokenErrorType {
self.error
}
pub fn explain<D: Into<Cow<'static, str>>>(&mut self, description: D) {
self.description = Some(description.into())
}
pub fn explain_uri(&mut self, uri: Url) {
self.uri = Some(String::from(uri).into())
}
pub fn iter(&self) -> <Self as IntoIterator>::IntoIter {
self.into_iter()
}
}
impl Default for AuthorizationError {
fn default() -> Self {
AuthorizationError {
error: AuthorizationErrorType::InvalidRequest,
description: None,
uri: None,
}
}
}
impl AsRef<str> for AuthorizationErrorType {
fn as_ref(&self) -> &str {
self.description()
}
}
impl fmt::Display for AuthorizationErrorType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.as_ref())
}
}
impl Default for AccessTokenError {
fn default() -> Self {
AccessTokenError {
error: AccessTokenErrorType::InvalidRequest,
description: None,
uri: None,
}
}
}
impl AsRef<str> for AccessTokenErrorType {
fn as_ref(&self) -> &str {
self.description()
}
}
impl fmt::Display for AccessTokenErrorType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.as_ref())
}
}
impl IntoIterator for AuthorizationError {
type Item = (&'static str, Cow<'static, str>);
type IntoIter = vec::IntoIter<(&'static str, Cow<'static, str>)>;
fn into_iter(self) -> Self::IntoIter {
let mut vec = vec![("error", Cow::Borrowed(self.error.description()))];
if let Some(description) = self.description {
vec.push(("description", description));
}
if let Some(uri) = self.uri {
vec.push(("uri", uri));
}
vec.into_iter()
}
}
impl IntoIterator for &'_ AuthorizationError {
type Item = (&'static str, Cow<'static, str>);
type IntoIter = vec::IntoIter<(&'static str, Cow<'static, str>)>;
fn into_iter(self) -> Self::IntoIter {
let mut vec = vec![("error", Cow::Borrowed(self.error.description()))];
if let Some(description) = &self.description {
vec.push(("description", description.clone().to_owned()));
}
if let Some(uri) = &self.uri {
vec.push(("uri", uri.clone().to_owned()));
}
vec.into_iter()
}
}
impl IntoIterator for AccessTokenError {
type Item = (&'static str, Cow<'static, str>);
type IntoIter = vec::IntoIter<(&'static str, Cow<'static, str>)>;
fn into_iter(self) -> Self::IntoIter {
let mut vec = vec![("error", Cow::Borrowed(self.error.description()))];
if let Some(description) = self.description {
vec.push(("description", description));
}
if let Some(uri) = self.uri {
vec.push(("uri", uri));
}
vec.into_iter()
}
}
impl IntoIterator for &'_ AccessTokenError {
type Item = (&'static str, Cow<'static, str>);
type IntoIter = vec::IntoIter<(&'static str, Cow<'static, str>)>;
fn into_iter(self) -> Self::IntoIter {
let mut vec = vec![("error", Cow::Borrowed(self.error.description()))];
if let Some(description) = &self.description {
vec.push(("description", description.clone().to_owned()));
}
if let Some(uri) = &self.uri {
vec.push(("uri", uri.clone().to_owned()));
}
vec.into_iter()
}
}