use serde::Deserialize;
use serde::Serialize;
use serde_json::Value;
use std::future::Future;
use std::pin::Pin;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StoreModel {
pub key: String,
pub value: Value,
}
pub trait Store: Send + Sync {
fn initialize(
&self,
) -> Pin<Box<dyn Future<Output = Result<(), StoreError>> + Send + '_>>;
fn get(
&self,
key: &str,
) -> Pin<
Box<dyn Future<Output = Result<Option<Value>, StoreError>> + Send + '_>,
>;
fn list(
&self,
) -> Pin<
Box<
dyn Future<Output = Result<Vec<StoreModel>, StoreError>>
+ Send
+ '_,
>,
>;
fn set(
&self,
key: &str,
value: Value,
ttl: Option<u64>,
) -> Pin<
Box<
dyn Future<Output = Result<Option<StoreModel>, StoreError>>
+ Send
+ '_,
>,
>;
fn remove(
&self,
key: &str,
) -> Pin<Box<dyn Future<Output = Result<(), StoreError>> + Send + '_>>;
fn remove_many(
&self,
keys: &[&str],
) -> Pin<Box<dyn Future<Output = Result<(), StoreError>> + Send + '_>>;
fn clear(
&self,
) -> Pin<Box<dyn Future<Output = Result<(), StoreError>> + Send + '_>>;
}
#[derive(thiserror::Error, Debug)]
pub enum StoreError {
#[error("Failed to connect to the database backend: {0}")]
ConnectionError(String),
#[error("Error while serializing or deserializing data")]
SerializationError {
#[from]
source: serde_json::Error,
},
#[error("Database operation failed")]
DatabaseError {
#[source]
source: Box<dyn std::error::Error + Send + Sync>,
},
#[error("Database query error: {0}")]
QueryError(String),
#[error("The requested key was not found")]
NotFound,
#[error("An unknown error has occurred")]
Unknown,
}