hpc_captcha

Crates.iohpc_captcha
lib.rshpc_captcha
version0.1.4
created_at2025-02-21 13:17:34.651948+00
updated_at2025-03-05 12:21:14.709306+00
descriptionhpc_captcha
homepagehttps://github.com/i18n-site/hpc/tree/dev/rust/hpc_captcha
repositoryhttps://github.com/i18n-site/hpc.git
max_upload_size
id1564064
size25,099
i18n.site (i18nsite)

documentation

README

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 ⋅ 国际化解决方案 的开源组件。

Commit count: 0

cargo fmt