use std::fmt;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum OAuthErrorCode {
InvalidRequest,
UnauthorizedClient,
AccessDenied,
UnsupportedResponseType,
InvalidScope,
ServerError,
TemporarilyUnavailable,
InvalidGrant,
InvalidClient,
UnsupportedGrantType,
Unknown(String),
}
impl OAuthErrorCode {
#[must_use]
pub fn as_str(&self) -> &str {
match self {
Self::InvalidRequest => "invalid_request",
Self::UnauthorizedClient => "unauthorized_client",
Self::AccessDenied => "access_denied",
Self::UnsupportedResponseType => "unsupported_response_type",
Self::InvalidScope => "invalid_scope",
Self::ServerError => "server_error",
Self::TemporarilyUnavailable => "temporarily_unavailable",
Self::InvalidGrant => "invalid_grant",
Self::InvalidClient => "invalid_client",
Self::UnsupportedGrantType => "unsupported_grant_type",
Self::Unknown(value) => value,
}
}
}
impl From<&str> for OAuthErrorCode {
fn from(value: &str) -> Self {
match value {
"invalid_request" => Self::InvalidRequest,
"unauthorized_client" => Self::UnauthorizedClient,
"access_denied" => Self::AccessDenied,
"unsupported_response_type" => Self::UnsupportedResponseType,
"invalid_scope" => Self::InvalidScope,
"server_error" => Self::ServerError,
"temporarily_unavailable" => Self::TemporarilyUnavailable,
"invalid_grant" => Self::InvalidGrant,
"invalid_client" => Self::InvalidClient,
"unsupported_grant_type" => Self::UnsupportedGrantType,
other => Self::Unknown(other.to_owned()),
}
}
}
impl fmt::Display for OAuthErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl Serialize for OAuthErrorCode {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}
impl<'de> Deserialize<'de> for OAuthErrorCode {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
String::deserialize(deserializer).map(|value| Self::from(value.as_str()))
}
}
impl crate::request::QueryParamValue for OAuthErrorCode {
fn encode(&self) -> String {
self.to_string()
}
}