use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use crate::{Aggregate, Result, Snapshot};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(bound(
serialize = "S: Serialize",
deserialize = "S: serde::de::DeserializeOwned"
))]
pub struct StoredSnapshot<S: Snapshot> {
aggregate_id: String,
version: i64,
snapshot: S,
}
impl<S: Snapshot> StoredSnapshot<S> {
pub fn new(aggregate_id: String, version: i64, snapshot: S) -> Self {
Self {
aggregate_id,
version,
snapshot,
}
}
pub fn aggregate_id(&self) -> &str {
&self.aggregate_id
}
pub fn version(&self) -> i64 {
self.version
}
pub fn into_snapshot(self) -> S {
self.snapshot
}
}
#[async_trait]
pub trait SnapshotStore<A: Aggregate>: Send + Sync {
async fn save(&self, aggregate_id: &A::Id, version: i64, snapshot: A::Snapshot) -> Result<()>;
async fn load(&self, aggregate_id: &A::Id) -> Result<Option<StoredSnapshot<A::Snapshot>>>;
}