4 releases

Uses new Rust 2024

0.1.4 Mar 5, 2025
0.1.3 Feb 26, 2025
0.1.2 Feb 24, 2025
0.1.1 Feb 21, 2025

#1 in #exists

Download history 258/week @ 2025-02-19 214/week @ 2025-02-26 163/week @ 2025-03-05

635 downloads per month
Used in 3 crates

MulanPSL-2.0

52KB
1.5K SLoC

Rust 1K SLoC // 0.0% comments JavaScript 198 SLoC // 0.2% comments CoffeeScript 60 SLoC // 0.3% comments Shell 21 SLoC // 0.1% comments

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

Dependencies

~1MB
~17K SLoC