hitbox-core 0.2.2

Asynchronous caching framework core traits.
Documentation
use async_trait::async_trait;
use chrono::Utc;
use hitbox_core::{
    CachePolicy, CacheValue, CacheableResponse, EntityPolicyConfig, Predicate, PredicateResult,
};

#[derive(Clone, Debug)]
struct TestResponse {
    #[allow(dead_code)]
    field1: String,
    #[allow(dead_code)]
    field2: u8,
}

impl TestResponse {
    pub fn new() -> Self {
        Self {
            field1: "nope".to_owned(),
            field2: 42,
        }
    }
}

impl CacheableResponse for TestResponse {
    type Cached = Self;
    type Subject = Self;
    type IntoCachedFuture = std::future::Ready<CachePolicy<Self::Cached, Self>>;
    type FromCachedFuture = std::future::Ready<Self>;

    async fn cache_policy<P>(
        self,
        predicates: P,
        _config: &EntityPolicyConfig,
    ) -> hitbox_core::ResponseCachePolicy<Self>
    where
        P: hitbox_core::Predicate<Subject = Self::Subject> + Send + Sync,
    {
        match predicates.check(self).await {
            PredicateResult::Cacheable(cacheable) => match cacheable.into_cached().await {
                CachePolicy::Cacheable(res) => {
                    CachePolicy::Cacheable(CacheValue::new(res, Some(Utc::now()), Some(Utc::now())))
                }
                CachePolicy::NonCacheable(res) => CachePolicy::NonCacheable(res),
            },
            PredicateResult::NonCacheable(res) => CachePolicy::NonCacheable(res),
        }
    }

    fn into_cached(self) -> Self::IntoCachedFuture {
        std::future::ready(CachePolicy::Cacheable(self))
    }

    fn from_cached(cached: Self::Cached) -> Self::FromCachedFuture {
        std::future::ready(cached)
    }
}

#[derive(Debug)]
struct NeuralPredicate {}

impl NeuralPredicate {
    fn new() -> Self {
        NeuralPredicate {}
    }
}

#[async_trait]
impl Predicate for NeuralPredicate {
    type Subject = TestResponse;

    async fn check(&self, subject: Self::Subject) -> PredicateResult<Self::Subject> {
        PredicateResult::Cacheable(subject)
    }
}

#[tokio::test]
async fn test_cacheable_result() {
    let response: Result<TestResponse, ()> = Ok(TestResponse::new());
    let policy = response
        .cache_policy(NeuralPredicate::new(), &EntityPolicyConfig::default())
        .await;
    dbg!(&policy);

    let response: Result<TestResponse, ()> = Err(());
    let policy = response
        .cache_policy(NeuralPredicate::new(), &EntityPolicyConfig::default())
        .await;
    dbg!(&policy);
}