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