use super::{FilesystemStateManager, MemoryStateManager, StateManager, StateOptions, StateSnapshot, VersionedState};
use crate::error::Result;
use serde::{de::DeserializeOwned, Serialize};
#[derive(Clone)]
pub enum ConcreteStateManager {
Memory(MemoryStateManager),
Filesystem(FilesystemStateManager),
}
macro_rules! delegate {
($self:expr, $method:ident $(, $arg:expr)*) => {
match $self {
Self::Memory(m) => m.$method($($arg),*).await,
Self::Filesystem(f) => f.$method($($arg),*).await,
}
};
}
impl ConcreteStateManager {
pub async fn save<T: Serialize + Send + Sync>(&self, node_id: &str, state: &T) -> Result<()> {
delegate!(self, save, node_id, state)
}
pub async fn save_with_options<T: Serialize + Send + Sync>(
&self,
node_id: &str,
state: &T,
options: StateOptions,
) -> Result<()> {
delegate!(self, save_with_options, node_id, state, options)
}
pub async fn load<T: DeserializeOwned + Send + Sync>(&self, node_id: &str) -> Result<Option<T>> {
delegate!(self, load, node_id)
}
pub async fn load_versioned<T: DeserializeOwned + Send + Sync>(
&self,
node_id: &str,
) -> Result<Option<VersionedState<T>>> {
delegate!(self, load_versioned, node_id)
}
pub async fn delete(&self, node_id: &str) -> Result<()> {
delegate!(self, delete, node_id)
}
pub async fn exists(&self, node_id: &str) -> Result<bool> {
delegate!(self, exists, node_id)
}
pub async fn list_nodes(&self) -> Result<Vec<String>> {
delegate!(self, list_nodes)
}
pub async fn size(&self, node_id: &str) -> Result<Option<usize>> {
delegate!(self, size, node_id)
}
pub async fn create_snapshot(&self, node_id: &str, description: Option<String>) -> Result<StateSnapshot> {
delegate!(self, create_snapshot, node_id, description)
}
pub async fn restore_snapshot(&self, snapshot_id: &str) -> Result<()> {
delegate!(self, restore_snapshot, snapshot_id)
}
pub async fn list_snapshots(&self, node_id: &str) -> Result<Vec<StateSnapshot>> {
delegate!(self, list_snapshots, node_id)
}
pub async fn delete_snapshot(&self, snapshot_id: &str) -> Result<()> {
delegate!(self, delete_snapshot, snapshot_id)
}
pub async fn compare_and_swap<T: Serialize + DeserializeOwned + Send + Sync>(
&self,
node_id: &str,
expected: &T,
new_value: &T,
) -> Result<bool> {
delegate!(self, compare_and_swap, node_id, expected, new_value)
}
}