cinderblock-core 0.3.0

Resource trait definitions, CRUD operations, runtime context, and in-memory data layer for cinderblock
Documentation
use std::{
    any::{Any, TypeId},
    collections::HashMap,
    sync::{Arc, LazyLock},
};

use tokio::sync::RwLock;

use crate::{PerformRead, ReadAction, Resource, data_layer::DataLayer};

type State =
    LazyLock<Arc<RwLock<HashMap<TypeId, HashMap<String, Box<dyn Any + Send + Sync + 'static>>>>>>;

static STATE: State = LazyLock::new(Arc::default);

#[derive(Debug)]
pub struct InMemoryDataLayer {}
impl InMemoryDataLayer {
    pub(crate) fn new() -> Self {
        Self {}
    }
}

impl<R: Resource + 'static> DataLayer<R> for InMemoryDataLayer {
    async fn create(&self, resource: R) -> crate::Result<R> {
        let state = STATE.clone();
        let mut state = state.write().await;

        let map = state.entry(TypeId::of::<R>()).or_default();
        map.insert(
            resource.primary_key().to_string(),
            Box::new(resource.clone()),
        );

        Ok(resource)
    }

    async fn read(&self, primary_key: &R::PrimaryKey) -> crate::Result<R> {
        let state = STATE.clone();
        let state = state.read().await;

        let key = primary_key.to_string();

        state
            .get(&TypeId::of::<R>())
            .and_then(|map| map.get(&key))
            .and_then(|boxed| boxed.downcast_ref::<R>())
            .cloned()
            .ok_or_else(|| format!("resource not found for primary key `{key}`").into())
    }

    async fn update(&self, resource: R) -> crate::Result<()> {
        let state = STATE.clone();
        let mut state = state.write().await;

        let key = resource.primary_key().to_string();

        let map = state
            .get_mut(&TypeId::of::<R>())
            .ok_or_else(|| format!("resource not found for primary key `{key}`"))?;

        if !map.contains_key(&key) {
            return Err(format!("resource not found for primary key `{key}`").into());
        }

        map.insert(key, Box::new(resource.clone()));

        Ok(())
    }

    async fn destroy(&self, primary_key: &R::PrimaryKey) -> crate::Result<R> {
        let state = STATE.clone();
        let mut state = state.write().await;

        let key = primary_key.to_string();

        let map = state
            .get_mut(&TypeId::of::<R>())
            .ok_or_else(|| format!("resource not found for primary key `{key}`"))?;

        let boxed = map
            .remove(&key)
            .ok_or_else(|| format!("resource not found for primary key `{key}`"))?;

        boxed
            .downcast::<R>()
            .map(|r| *r)
            .map_err(|_| "failed to downcast destroyed resource".into())
    }
}

/// Filter trait for non-paged in-memory read actions. The generated
/// `resource!` macro emits an impl for each non-paged read action.
pub trait InMemoryReadAction: ReadAction {
    fn filter(row: &Self::Output, args: &Self::Arguments) -> bool;
}

/// Filter trait for paged in-memory read actions. Same filter interface as
/// [`InMemoryReadAction`], but applied to paged actions which additionally
/// require `Arguments: Paged`.
pub trait InMemoryPagedReadAction: ReadAction {
    fn filter(row: &Self::Output, args: &Self::Arguments) -> bool;
}

/// Unified execution trait that bridges the filter-based traits
/// ([`InMemoryReadAction`] and [`InMemoryPagedReadAction`]) to the
/// framework's [`PerformRead`] trait.
///
/// The `resource!` macro generates explicit impls of this trait for each
/// read action, delegating to the appropriate filter logic. A single
/// blanket `PerformRead` impl then dispatches to this trait.
pub trait InMemoryPerformRead: ReadAction {
    fn execute(
        all: impl Iterator<Item = Self::Output>,
        args: &Self::Arguments,
    ) -> Self::Response;
}

/// Single `PerformRead` impl for `InMemoryDataLayer` that delegates to
/// `InMemoryPerformRead::execute`.
impl<R, A> PerformRead<A> for InMemoryDataLayer
where
    R: Resource + 'static,
    A: ReadAction<Output = R> + InMemoryPerformRead + 'static,
{
    async fn read(&self, args: &A::Arguments) -> crate::Result<A::Response> {
        let state = STATE.clone();
        let state = state.read().await;

        let type_map = state.get(&TypeId::of::<R>());
        let all = type_map
            .iter()
            .flat_map(|map| map.values())
            .filter_map(|boxed| boxed.downcast_ref::<R>())
            .cloned();

        Ok(A::execute(all, args))
    }
}