use crate::BoxError;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::BTreeMap;
#[async_trait]
pub trait Persist: Send + Sync {
async fn save_current(&self, value: &CurrentValues) -> Result<(), BoxError>;
async fn load_current(&self) -> Result<Option<CurrentValues>, BoxError>;
async fn save_history(&self, key: &str, value: &ValueHistory) -> Result<(), BoxError>;
async fn load_history(&self, key: &str) -> Result<Option<ValueHistory>, BoxError>;
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CurrentValues {
pub version: i32,
pub date: DateTime<Utc>,
pub feattles: BTreeMap<String, CurrentValue>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CurrentValue {
pub modified_at: DateTime<Utc>,
pub modified_by: String,
pub value: Value,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct ValueHistory {
pub entries: Vec<HistoryEntry>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct HistoryEntry {
pub value: Value,
pub value_overview: String,
pub modified_at: DateTime<Utc>,
pub modified_by: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NoPersistence;
#[async_trait]
impl Persist for NoPersistence {
async fn save_current(&self, _value: &CurrentValues) -> Result<(), BoxError> {
Ok(())
}
async fn load_current(&self) -> Result<Option<CurrentValues>, BoxError> {
Ok(None)
}
async fn save_history(&self, _key: &str, _value: &ValueHistory) -> Result<(), BoxError> {
Ok(())
}
async fn load_history(&self, _key: &str) -> Result<Option<ValueHistory>, BoxError> {
Ok(None)
}
}