use crate::CaptchaGenerator;
use std::fmt::Display;
#[derive(Debug, Clone, Copy)]
pub enum CaptchaName {
Normal,
SlightlyTwisted,
VeryTwisted,
}
#[derive(Debug, Clone, Copy)]
pub enum CaptchaDifficulty {
Easy,
Medium,
Hard,
}
impl From<CaptchaName> for captcha::CaptchaName {
fn from(value: CaptchaName) -> Self {
match value {
CaptchaName::Normal => Self::Lucy,
CaptchaName::SlightlyTwisted => Self::Amelia,
CaptchaName::VeryTwisted => Self::Mila,
}
}
}
impl From<CaptchaDifficulty> for captcha::Difficulty {
fn from(value: CaptchaDifficulty) -> captcha::Difficulty {
match value {
CaptchaDifficulty::Easy => Self::Easy,
CaptchaDifficulty::Medium => Self::Medium,
CaptchaDifficulty::Hard => Self::Hard,
}
}
}
#[derive(Debug)]
pub enum SimpleGeneratorError {
FaildEncodedToPng,
}
impl Display for SimpleGeneratorError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Faild to encode the captcha to png image")
}
}
impl std::error::Error for SimpleGeneratorError {}
pub struct SimpleGenerator {
name: CaptchaName,
difficulty: CaptchaDifficulty,
}
impl SimpleGenerator {
pub const fn new(name: CaptchaName, difficulty: CaptchaDifficulty) -> Self {
Self { name, difficulty }
}
}
impl CaptchaGenerator for SimpleGenerator {
type Error = SimpleGeneratorError;
async fn new_captcha(&self) -> Result<(String, Vec<u8>), Self::Error> {
captcha::by_name(self.difficulty.into(), self.name.into())
.as_tuple()
.ok_or(SimpleGeneratorError::FaildEncodedToPng)
}
}