hpc_captcha 0.1.4

hpc_captcha
Documentation

hpc_captcha

use std::{
  marker::PhantomData,
  sync::atomic::{AtomicBool, Ordering},
};

use aok::Result;
use ih::{CodeBody, State};

pub trait GenCaptcha {
  fn get() -> impl Future<Output = Result<Vec<u8>>> + Send;
}

pub struct Captcha<G: GenCaptcha> {
  exist: AtomicBool,
  _g: PhantomData<G>,
}

impl<G: GenCaptcha> Default for Captcha<G> {
  fn default() -> Self {
    Self::new()
  }
}

impl<G: GenCaptcha> Captcha<G> {
  pub fn new() -> Self {
    Captcha {
      exist: AtomicBool::new(false),
      _g: PhantomData,
    }
  }

  pub async fn get(&self) -> Result<CodeBody, CodeBody> {
    if self.exist.swap(true, Ordering::SeqCst) {
      Ok((State::CAPTCHA, vec![]))
    } else {
      match G::get().await {
        Ok(bin) => Ok((State::CAPTCHA, bin)),
        Err(err) => Err((State::MIDDLEWARE_ERROR, format!("captcha {err}").into())),
      }
    }
  }
}

About

This project is an open-source component of i18n.site ⋅ Internationalization Solution.

关于

本项目为 i18n.site ⋅ 国际化解决方案 的开源组件。