refactor: Move the CacacheStorage to another module
All checks were successful
Rust CI / Rust CI (push) Successful in 1m51s

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-08-10 15:53:06 +00:00
parent dfa35daf0f
commit 4be7a66d03
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
2 changed files with 92 additions and 79 deletions

View file

@ -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<T>` 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<Output = Result<String, Self::Error>> + 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<Output = Result<Option<String>, Self::Error>> + Send;
/// Clear the expired captcha.
fn clear_expired(
&self,
expired_after: Duration,
) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send;
/// Clear the captcha by token.
fn clear_by_token(
&self,
token: &str,
) -> impl std::future::Future<Output = Result<(), Self::Error>> + 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<PathBuf>) -> 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<T> CaptchaStorage for Arc<T>
where
T: CaptchaStorage,
{
type Error = T::Error;
fn store_answer(
&self,
answer: String,
) -> impl std::future::Future<Output = Result<String, Self::Error>> + Send {
self.as_ref().store_answer(answer)
}
fn get_answer(
&self,
token: &str,
) -> impl std::future::Future<Output = Result<Option<String>, Self::Error>> + Send {
self.as_ref().get_answer(token)
}
fn clear_expired(
&self,
expired_after: Duration,
) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send {
self.as_ref().clear_expired(expired_after)
}
fn clear_by_token(
&self,
token: &str,
) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send {
self.as_ref().clear_by_token(token)
}
}
#[cfg(test)]
#[cfg(feature = "cacache-storage")]
mod tests {
use super::*;

90
src/storage/mod.rs Normal file
View file

@ -0,0 +1,90 @@
// Copyright (c) 2024, Awiteb <a@4rs.nl>
// 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<T>` 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<Output = Result<String, Self::Error>> + 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<Output = Result<Option<String>, Self::Error>> + Send;
/// Clear the expired captcha.
fn clear_expired(
&self,
expired_after: Duration,
) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send;
/// Clear the captcha by token.
fn clear_by_token(
&self,
token: &str,
) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send;
}
impl<T> CaptchaStorage for Arc<T>
where
T: CaptchaStorage,
{
type Error = T::Error;
fn store_answer(
&self,
answer: String,
) -> impl std::future::Future<Output = Result<String, Self::Error>> + Send {
self.as_ref().store_answer(answer)
}
fn get_answer(
&self,
token: &str,
) -> impl std::future::Future<Output = Result<Option<String>, Self::Error>> + Send {
self.as_ref().get_answer(token)
}
fn clear_expired(
&self,
expired_after: Duration,
) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send {
self.as_ref().clear_expired(expired_after)
}
fn clear_by_token(
&self,
token: &str,
) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send {
self.as_ref().clear_by_token(token)
}
}