Skip to main content

mecha10_core/state/
concrete.rs

1//! `ConcreteStateManager`: a `dyn`-free enum wrapper over `StateManager` backends.
2
3use super::{FilesystemStateManager, MemoryStateManager, StateManager, StateOptions, StateSnapshot, VersionedState};
4use crate::error::Result;
5use serde::{de::DeserializeOwned, Serialize};
6
7#[derive(Clone)]
8pub enum ConcreteStateManager {
9    Memory(MemoryStateManager),
10    Filesystem(FilesystemStateManager),
11}
12
13macro_rules! delegate {
14    ($self:expr, $method:ident $(, $arg:expr)*) => {
15        match $self {
16            Self::Memory(m) => m.$method($($arg),*).await,
17            Self::Filesystem(f) => f.$method($($arg),*).await,
18        }
19    };
20}
21
22impl ConcreteStateManager {
23    pub async fn save<T: Serialize + Send + Sync>(&self, node_id: &str, state: &T) -> Result<()> {
24        delegate!(self, save, node_id, state)
25    }
26
27    pub async fn save_with_options<T: Serialize + Send + Sync>(
28        &self,
29        node_id: &str,
30        state: &T,
31        options: StateOptions,
32    ) -> Result<()> {
33        delegate!(self, save_with_options, node_id, state, options)
34    }
35
36    pub async fn load<T: DeserializeOwned + Send + Sync>(&self, node_id: &str) -> Result<Option<T>> {
37        delegate!(self, load, node_id)
38    }
39
40    pub async fn load_versioned<T: DeserializeOwned + Send + Sync>(
41        &self,
42        node_id: &str,
43    ) -> Result<Option<VersionedState<T>>> {
44        delegate!(self, load_versioned, node_id)
45    }
46
47    pub async fn delete(&self, node_id: &str) -> Result<()> {
48        delegate!(self, delete, node_id)
49    }
50
51    pub async fn exists(&self, node_id: &str) -> Result<bool> {
52        delegate!(self, exists, node_id)
53    }
54
55    pub async fn list_nodes(&self) -> Result<Vec<String>> {
56        delegate!(self, list_nodes)
57    }
58
59    pub async fn size(&self, node_id: &str) -> Result<Option<usize>> {
60        delegate!(self, size, node_id)
61    }
62
63    pub async fn create_snapshot(&self, node_id: &str, description: Option<String>) -> Result<StateSnapshot> {
64        delegate!(self, create_snapshot, node_id, description)
65    }
66
67    pub async fn restore_snapshot(&self, snapshot_id: &str) -> Result<()> {
68        delegate!(self, restore_snapshot, snapshot_id)
69    }
70
71    pub async fn list_snapshots(&self, node_id: &str) -> Result<Vec<StateSnapshot>> {
72        delegate!(self, list_snapshots, node_id)
73    }
74
75    pub async fn delete_snapshot(&self, snapshot_id: &str) -> Result<()> {
76        delegate!(self, delete_snapshot, snapshot_id)
77    }
78
79    pub async fn compare_and_swap<T: Serialize + DeserializeOwned + Send + Sync>(
80        &self,
81        node_id: &str,
82        expected: &T,
83        new_value: &T,
84    ) -> Result<bool> {
85        delegate!(self, compare_and_swap, node_id, expected, new_value)
86    }
87}