cqrs_eventsourcing/
aggregate.rs1use serde::{de::DeserializeOwned, Serialize};
2use std::fmt::Debug;
3use uuid::Uuid;
4
5pub trait Aggregate: Debug + Default + Serialize + DeserializeOwned + Sync + Send {
6 fn aggregate_type() -> &'static str;
7}
8
9pub struct AggregateContext<A: Aggregate> {
10 pub id: String,
11 pub version: usize,
12 pub aggregate: A,
13}
14
15impl<A: Aggregate> AggregateContext<A> {
16 pub fn aggregate(&self) -> &A {
17 &self.aggregate
18 }
19
20 pub fn set_id(&mut self, id: Option<String>) {
21 self.id = match id {
22 Some(x) => x,
23 None => Uuid::new_v4().to_string(),
24 };
25 }
26}
27
28impl<A: Aggregate> Default for AggregateContext<A> {
29 fn default() -> AggregateContext<A> {
30 AggregateContext {
31 id: String::default(),
32 version: 0_usize,
33 aggregate: A::default(),
34 }
35 }
36}