ctx_ 0.1.8

Extract message form headers only once for one req ( support async and sync ) / 一个请求只提取一次消息, 支持异步和同步
Documentation
use aok::Result;
use captcha_verify::captcha_verify;
use hpc_captcha::GenCaptcha;
use ih::CodeBody;

use crate::{Ctx, Extract};

pub struct Captcha(pub bool);

impl Extract for Captcha {
  async fn from_ctx(ctx: &Ctx) -> Result<Self> {
    Ok(Self(captcha_verify(&ctx.req.headers).await))
  }
}

pub async fn captcha<G: GenCaptcha>(
  ctx: &Ctx,
  captcha: &hpc_captcha::Captcha<G>,
) -> Result<(), CodeBody> {
  if let Ok::<&Captcha, _>(c) = crate::get(ctx).await {
    if c.0 {
      return Ok(());
    }
  }
  Err(captcha.get().await?)
}