From 4be7a66d03875f0f07082f7a34c5453434de7eeb Mon Sep 17 00:00:00 2001 From: Awiteb Date: Sat, 10 Aug 2024 15:53:06 +0000 Subject: [PATCH] refactor: Move the `CacacheStorage` to another module Signed-off-by: Awiteb --- .../cacache_storage.rs} | 81 +---------------- src/storage/mod.rs | 90 +++++++++++++++++++ 2 files changed, 92 insertions(+), 79 deletions(-) rename src/{storage.rs => storage/cacache_storage.rs} (78%) create mode 100644 src/storage/mod.rs diff --git a/src/storage.rs b/src/storage/cacache_storage.rs similarity index 78% rename from src/storage.rs rename to src/storage/cacache_storage.rs index 6043ddb..42b440a 100644 --- a/src/storage.rs +++ b/src/storage/cacache_storage.rs @@ -9,62 +9,22 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -use std::{sync::Arc, time::Duration}; - -#[cfg(feature = "cacache-storage")] use std::{ path::{Path, PathBuf}, - time::SystemTime, + time::{Duration, SystemTime}, }; -/// Trait to store the captcha token and answer. is also clear the expired captcha. -/// -/// The trait will be implemented for `Arc` if `T` implements the trait. -/// -/// The trait is thread safe, so the storage can be shared between threads. -pub trait CaptchaStorage: Send + Sync + 'static -where - Self: Clone + std::fmt::Debug, -{ - /// The error type of the storage. - type Error: std::fmt::Display + std::fmt::Debug + Send; - - /// Store the captcha token and answer. - fn store_answer( - &self, - answer: String, - ) -> impl std::future::Future> + Send; - - /// Returns the answer of the captcha token. This method will return None if the token is not exist. - fn get_answer( - &self, - token: &str, - ) -> impl std::future::Future, Self::Error>> + Send; - - /// Clear the expired captcha. - fn clear_expired( - &self, - expired_after: Duration, - ) -> impl std::future::Future> + Send; - - /// Clear the captcha by token. - fn clear_by_token( - &self, - token: &str, - ) -> impl std::future::Future> + Send; -} +use crate::CaptchaStorage; /// The [`cacache`] storage. /// /// [`cacache`]: https://github.com/zkat/cacache-rs -#[cfg(feature = "cacache-storage")] #[derive(Debug, Clone)] pub struct CacacheStorage { /// The cacache cache directory. cache_dir: PathBuf, } -#[cfg(feature = "cacache-storage")] impl CacacheStorage { /// Create a new CacacheStorage pub fn new(cache_dir: impl Into) -> Self { @@ -79,7 +39,6 @@ impl CacacheStorage { } } -#[cfg(feature = "cacache-storage")] impl CaptchaStorage for CacacheStorage { type Error = cacache::Error; @@ -144,43 +103,7 @@ impl CaptchaStorage for CacacheStorage { } } -impl CaptchaStorage for Arc -where - T: CaptchaStorage, -{ - type Error = T::Error; - - fn store_answer( - &self, - answer: String, - ) -> impl std::future::Future> + Send { - self.as_ref().store_answer(answer) - } - - fn get_answer( - &self, - token: &str, - ) -> impl std::future::Future, Self::Error>> + Send { - self.as_ref().get_answer(token) - } - - fn clear_expired( - &self, - expired_after: Duration, - ) -> impl std::future::Future> + Send { - self.as_ref().clear_expired(expired_after) - } - - fn clear_by_token( - &self, - token: &str, - ) -> impl std::future::Future> + Send { - self.as_ref().clear_by_token(token) - } -} - #[cfg(test)] -#[cfg(feature = "cacache-storage")] mod tests { use super::*; diff --git a/src/storage/mod.rs b/src/storage/mod.rs new file mode 100644 index 0000000..afa6af1 --- /dev/null +++ b/src/storage/mod.rs @@ -0,0 +1,90 @@ +// Copyright (c) 2024, Awiteb +// A captcha middleware for Salvo framework. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +use std::{sync::Arc, time::Duration}; + +#[cfg(feature = "cacache-storage")] +mod cacache_storage; + +#[cfg(feature = "cacache-storage")] +pub use cacache_storage::*; + +/// Trait to store the captcha token and answer. is also clear the expired captcha. +/// +/// The trait will be implemented for `Arc` if `T` implements the trait. +/// +/// The trait is thread safe, so the storage can be shared between threads. +pub trait CaptchaStorage: Send + Sync + 'static +where + Self: Clone + std::fmt::Debug, +{ + /// The error type of the storage. + type Error: std::fmt::Display + std::fmt::Debug + Send; + + /// Store the captcha token and answer. + fn store_answer( + &self, + answer: String, + ) -> impl std::future::Future> + Send; + + /// Returns the answer of the captcha token. This method will return None if the token is not exist. + fn get_answer( + &self, + token: &str, + ) -> impl std::future::Future, Self::Error>> + Send; + + /// Clear the expired captcha. + fn clear_expired( + &self, + expired_after: Duration, + ) -> impl std::future::Future> + Send; + + /// Clear the captcha by token. + fn clear_by_token( + &self, + token: &str, + ) -> impl std::future::Future> + Send; +} + +impl CaptchaStorage for Arc +where + T: CaptchaStorage, +{ + type Error = T::Error; + + fn store_answer( + &self, + answer: String, + ) -> impl std::future::Future> + Send { + self.as_ref().store_answer(answer) + } + + fn get_answer( + &self, + token: &str, + ) -> impl std::future::Future, Self::Error>> + Send { + self.as_ref().get_answer(token) + } + + fn clear_expired( + &self, + expired_after: Duration, + ) -> impl std::future::Future> + Send { + self.as_ref().clear_expired(expired_after) + } + + fn clear_by_token( + &self, + token: &str, + ) -> impl std::future::Future> + Send { + self.as_ref().clear_by_token(token) + } +}