lix 0.18.0

Embeddable version control for apps and AI agents.
Documentation
use crate::storage::{
    ReadOptions, Storage, StorageChangeWatch, StorageError, StorageSessionToken, WriteOptions,
};

pub trait StorageFactory: Sync {
    type Storage: Storage;
    type Fixture: StorageFixture<Storage = Self::Storage>;

    fn create_fixture(&self) -> Self::Fixture;

    fn config(&self) -> StorageTestConfig {
        StorageTestConfig::default()
    }
}

pub trait StorageFixture: Send + Sync {
    type Storage: Storage;

    fn open(&self) -> impl Future<Output = Self::Storage> + Send;
}

pub(crate) struct OpenStorage<F>
where
    F: StorageFactory,
{
    _fixture: F::Fixture,
    storage: F::Storage,
}

pub(crate) async fn open_storage<F>(factory: &F) -> OpenStorage<F>
where
    F: StorageFactory,
{
    let fixture = factory.create_fixture();
    let storage = fixture.open().await;
    OpenStorage {
        _fixture: fixture,
        storage,
    }
}

impl<F> Storage for OpenStorage<F>
where
    F: StorageFactory,
{
    type Read<'a>
        = <F::Storage as Storage>::Read<'a>
    where
        Self: 'a;
    type Write<'a>
        = <F::Storage as Storage>::Write<'a>
    where
        Self: 'a;

    fn acquire_session(
        &self,
    ) -> impl Future<Output = Result<StorageSessionToken, StorageError>> + Send {
        self.storage.acquire_session()
    }

    fn begin_read(
        &self,
        opts: ReadOptions,
    ) -> impl Future<Output = Result<Self::Read<'_>, StorageError>> + Send {
        self.storage.begin_read(opts)
    }

    fn begin_write(
        &self,
        opts: WriteOptions,
    ) -> impl Future<Output = Result<Self::Write<'_>, StorageError>> + Send {
        self.storage.begin_write(opts)
    }

    fn watch_for_changes(
        &self,
    ) -> impl Future<Output = Result<StorageChangeWatch, StorageError>> + Send {
        self.storage.watch_for_changes()
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct StorageTestConfig {
    pub max_key_len: usize,
    pub max_value_len: usize,
    pub ephemeral: bool,
    pub supports_concurrent_writers: bool,
}

impl Default for StorageTestConfig {
    fn default() -> Self {
        Self {
            max_key_len: 256,
            max_value_len: 4096,
            ephemeral: false,
            supports_concurrent_writers: false,
        }
    }
}