amethystate 0.26.0

A state manager for Rust applications
Documentation
use crate::Store;
use crate::StoreBackend;
use amethystate_core::path::StorePath;

use crate::store::StorageResult;
use amethystate_core::AmeBackendSync;
use serde::Serialize;
use serde::de::DeserializeOwned;
use uuid::Uuid;

pub(crate) struct SyncBridge {
    pub(crate) store: Store,
}

impl SyncBridge {
    pub(crate) fn new(store: Store) -> Self {
        Self { store }
    }
}

impl AmeBackendSync for SyncBridge {
    type Raw = Vec<u8>;
    type Borrowed = [u8];

    fn get<T>(&self, path: &StorePath) -> StorageResult<Option<T>>
    where
        T: DeserializeOwned,
    {
        Ok(self.store.get(path)?)
    }

    fn set_with_source<T: Serialize>(
        &self,
        path: &StorePath,
        value: &T,
        source: Option<Uuid>,
    ) -> StorageResult<()> {
        Ok(self.store.set_with_source(path, value, source)?)
    }

    fn set_owned_with_source<T: Serialize>(
        &self,
        path: StorePath,
        value: &T,
        source: Option<Uuid>,
    ) -> StorageResult<()> {
        Ok(self.store.set_owned_with_source(path, value, source)?)
    }

    fn set<T>(&self, path: &StorePath, value: &T) -> StorageResult<()>
    where
        T: Serialize,
    {
        Ok(self.store.set(path, value)?)
    }

    fn delete(&self, path: &StorePath) -> StorageResult<()> {
        Ok(self.store.delete(path)?)
    }

    fn delete_with_source(&self, path: &StorePath, source: Option<Uuid>) -> StorageResult<()> {
        self.store.delete_with_source(path, source)
    }

    fn delete_prefix(&self, prefix: &StorePath, source: Option<Uuid>) -> StorageResult<()> {
        self.store.delete_prefix_with_source(prefix, source)
    }

    fn scan_prefix(&self, prefix: &StorePath) -> StorageResult<Vec<(StorePath, Self::Raw)>> {
        Ok(self.store.scan_prefix(prefix)?)
    }

    fn scan_keys(&self, prefix: &StorePath) -> StorageResult<Vec<StorePath>> {
        Ok(self.store.scan_keys(prefix)?)
    }

    fn decode<T>(&self, raw: &[u8]) -> StorageResult<T>
    where
        T: DeserializeOwned + Default,
    {
        self.store.decode(raw)
    }
}