From 4240da240908453a68c9a7c306aad990c71aef3f Mon Sep 17 00:00:00 2001 From: Awiteb Date: Mon, 12 Aug 2024 20:13:18 +0000 Subject: [PATCH] feat: Function `new_captcha` for `CaptchaStorage` trait to create and store captcha Signed-off-by: Awiteb --- src/storage/mod.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/storage/mod.rs b/src/storage/mod.rs index a76c609..52ec968 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -51,6 +51,24 @@ pub trait CaptchaStorage: Send + Sync + 'static { &self, token: &str, ) -> impl std::future::Future> + Send; + + /// Create a new captcha image and return the answer and the image encoded as png. + /// + /// This method will store the answer in the storage. + fn new_captcha( + &self, + generator: G, + ) -> impl std::future::Future< + Output = Result<(String, Vec), either::Either>, + > + Send { + async move { + let (answer, image) = generator.new_captcha().await.map_err(either::Right)?; + Ok(( + self.store_answer(answer).await.map_err(either::Left)?, + image, + )) + } + } } impl CaptchaStorage for Arc