use std::{
error::Error,
fmt::{Display, Error as FmtError, Formatter},
};
#[derive(Debug, Clone, Eq, PartialEq)]
#[non_exhaustive]
pub enum ReCaptchaErrorCode {
MissingInputSecret,
InvalidInputSecret,
MissingInputResponse,
InvalidInputResponse,
BadRequest,
TimeoutOrDuplicate,
Other(String),
}
impl ReCaptchaErrorCode {
#[inline]
pub fn as_str(&self) -> &str {
match self {
ReCaptchaErrorCode::MissingInputSecret => "missing-input-secret",
ReCaptchaErrorCode::InvalidInputSecret => "invalid-input-secret",
ReCaptchaErrorCode::MissingInputResponse => "missing-input-response",
ReCaptchaErrorCode::InvalidInputResponse => "invalid-input-response",
ReCaptchaErrorCode::BadRequest => "bad-request",
ReCaptchaErrorCode::TimeoutOrDuplicate => "timeout-or-duplicate",
ReCaptchaErrorCode::Other(code) => code.as_str(),
}
}
}
impl From<String> for ReCaptchaErrorCode {
#[inline]
fn from(code: String) -> Self {
match code.as_str() {
"missing-input-secret" => ReCaptchaErrorCode::MissingInputSecret,
"invalid-input-secret" => ReCaptchaErrorCode::InvalidInputSecret,
"missing-input-response" => ReCaptchaErrorCode::MissingInputResponse,
"invalid-input-response" => ReCaptchaErrorCode::InvalidInputResponse,
"bad-request" => ReCaptchaErrorCode::BadRequest,
"timeout-or-duplicate" => ReCaptchaErrorCode::TimeoutOrDuplicate,
_ => ReCaptchaErrorCode::Other(code),
}
}
}
impl From<&str> for ReCaptchaErrorCode {
#[inline]
fn from(code: &str) -> Self {
ReCaptchaErrorCode::from(code.to_string())
}
}
impl Display for ReCaptchaErrorCode {
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum ReCaptchaError {
ErrorCodes(Vec<ReCaptchaErrorCode>),
UnexpectedStatusCode(u16),
Request(String),
UnexpectedResponse(String),
}
impl ReCaptchaError {
#[inline]
pub fn error_codes(&self) -> &[ReCaptchaErrorCode] {
match self {
ReCaptchaError::ErrorCodes(error_codes) => error_codes.as_slice(),
_ => &[],
}
}
}
impl Display for ReCaptchaError {
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
match self {
ReCaptchaError::ErrorCodes(error_codes) => {
f.write_str("The `siteverify` API reported the error codes:")?;
for error_code in error_codes {
f.write_str(" ")?;
Display::fmt(error_code, f)?;
}
Ok(())
},
ReCaptchaError::UnexpectedStatusCode(status_code) => {
write!(f, "The response status code of the `siteverify` API is {status_code}.")
},
ReCaptchaError::Request(text) | ReCaptchaError::UnexpectedResponse(text) => {
f.write_str(text)
},
}
}
}
impl Error for ReCaptchaError {}