1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! A State contains the initial conditions that a `Decision` uses to make the changes.
use serde::Deserialize;
use serde::{de::DeserializeOwned, Serialize};

use crate::stream_query::{StreamFilter, StreamQuery};
use crate::{all_the_tuples, union, BoxDynError, StateSnapshotter};
use crate::{event::Event, PersistedEvent};
use async_trait::async_trait;
use paste::paste;
use std::error::Error as StdError;
use std::ops::Deref;

/// A mutable state that can be changed by events from the event store.
pub trait StateMutate: StateQuery {
    /// Mutates the state object based on the provided event.
    ///
    /// # Arguments
    ///
    /// * `event` - The event to be applied to mutate the state.
    fn mutate(&mut self, event: Self::Event);
}

/// A group of states that can be queried and modified together.
///
/// The states can be mutated collectively based on an event
/// retrieved from the event store, and a unified query can be generated for all sub-states.
///
/// # Type Parameters
///
/// - `E`: The type of events that the multi-state object handles.
pub trait MultiState<E: Event + Clone> {
    /// Mutates all sub-states based on the provided event.
    ///
    /// # Arguments
    ///
    /// * `event` - The event to be applied to mutate the sub-states.
    fn mutate_all(&mut self, event: PersistedEvent<E>);
    /// The unified query that represents the union of queries for all sub-states.
    ///
    /// This query can be used to retrieve a stream of events relevant to the entire multi-state
    /// object.
    ///
    /// # Returns
    ///
    /// A `StreamQuery` representing the combined query for all sub-states.
    fn query_all(&self) -> StreamQuery<E>;

    /// Returns the version of the multi-state.
    ///
    /// The multi-state version is determined as the maximum of the versions
    /// of its sub-states.
    ///
    /// # Returns
    ///
    /// The method returns an `i64` representing the version of the multi-state.
    fn version(&self) -> i64;
}

macro_rules! impl_multi_state {
    (
        [$($ty:ident),*], $last:ident
    ) => {
        #[allow(unused_parens)]
        impl<E, $($ty,)* $last> MultiState<E> for ($(StatePart<$ty>,)* StatePart<$last>)
        where
            E: Event + Clone,
            $($ty: StateQuery + StateMutate,)*
            $last: StateQuery + StateMutate,
            <$last as StateQuery>::Event: TryFrom<E> + Into<E>,
            $(<$ty as StateQuery>::Event: TryFrom<E> + Into<E>,)*
            <$last as StateQuery>::Event: TryFrom<E> + Into<E>,
            $(<<$ty as StateQuery>::Event as TryFrom<E>>::Error:
                StdError + 'static + Send + Sync,)*
            <<$last as StateQuery>::Event as TryFrom<E>>::Error:
                StdError + 'static + Send + Sync,
        {
            fn mutate_all(&mut self, event: PersistedEvent<E>) {
                paste! {
                    let ($([<state_ $ty:lower>],)* [<state_ $last:lower>])= self;
                    $(
                        if [<state_ $ty:lower>].matches_event(&event) {
                            [<state_ $ty:lower>].mutate_part(event.clone());
                        }
                    )*
                    if [<state_ $last:lower>].matches_event(&event) {
                         [<state_ $last:lower>].mutate_part(event.clone());
                    }
                }
            }

            fn query_all(&self) -> StreamQuery<E> {
                paste!{
                    let ($([<state_ $ty:lower>],)* [<state_ $last:lower>])= self;
                    union!($([<state_ $ty:lower>].query_part(),)* [<state_ $last:lower>].query_part())
                }
            }
            fn version(&self) -> i64 {
                paste!{
                    let ($([<state_ $ty:lower>],)* [<state_ $last:lower>])= self;
                    let version = [<state_ $last:lower>].version();
                    $(
                        let version = version.max([<state_ $ty:lower>].version());
                    )*
                    version
                }
            }
        }
    }
}

all_the_tuples!(impl_multi_state);

/// A multi-state snapshot.
///
/// A trait necessary to handle the snapshot of its sub-states' load and store.
///
/// # Type Parameters
///
/// - `T`: The type of snapshotter used for loading and storing snapshots.
/// - `E`: The type of events that the multi-state object handles.
#[async_trait]
pub trait MultiStateSnapshot<T: StateSnapshotter> {
    // Loads the state of all sub-states using the provided snapshotter
    /// and returns the version of the multi-state object.
    ///
    /// # Arguments
    ///
    /// * `backend` - The snapshotter used to load the state of sub-states.
    ///
    /// # Returns
    ///
    /// Returns the version of the multi-state object after loading its state.
    async fn load_all(&mut self, backend: &T) -> i64;
    /// Stores the snapshot of all sub-states using the provided snapshotter.
    ///
    /// # Arguments
    ///
    /// * `backend` - The snapshotter used to store the snapshot of sub-states.
    ///
    /// # Returns
    ///
    /// Returns a `Result` indicating the success or failure of the storage operation.
    async fn store_all(&self, backend: &T) -> Result<(), BoxDynError>;
}

macro_rules! impl_multi_state_snapshot {
    (
        [$($ty:ident),*], $last:ident
    ) => {
        #[async_trait]
        #[allow(unused_parens)]
        impl<B, $($ty,)* $last> MultiStateSnapshot<B> for ($(StatePart<$ty>,)* StatePart<$last>)
        where
            B: StateSnapshotter + Send + Sync,
            $($ty: StateQuery + Serialize + DeserializeOwned + 'static,)*
            $last: StateQuery + Serialize + DeserializeOwned + 'static,
        {
            async fn load_all(&mut self, backend: &B) -> i64 {
                paste! {

                    let ($([<state_ $ty:lower>],)* [<state_ $last:lower>]) = self;
                    *[<state_ $last:lower>] = backend.load_snapshot([<state_ $last:lower>].clone()).await;
                    let last_event_id = [<state_ $last:lower>].version;
                    $(
                        *[<state_ $ty:lower>] = backend.load_snapshot([<state_ $ty:lower>].clone()).await;
                        let last_event_id = last_event_id.max([<state_ $ty:lower>].version);
                    )*
                }
                last_event_id
            }

            async fn store_all(&self, backend: &B) -> Result<(), BoxDynError>{
                paste!{

                    let ($([<state_ $ty:lower>],)* [<state_ $last:lower>]) = self;
                    $(
                    backend.store_snapshot(&[<state_ $ty:lower>]).await?;
                    )*
                    backend.store_snapshot(&[<state_ $last:lower>]).await?;
                }
                Ok(())
            }
        }
    }
}
all_the_tuples!(impl_multi_state_snapshot);

/// Represents a state query used to retrieve events from the event store to build a state.
///
/// The query method returns a `StreamQuery` to be used for querying the event store.
pub trait StateQuery: Clone + Send + Sync {
    /// the unique name of the state query.
    const NAME: &'static str;
    /// The type of events queried by this state query.
    type Event: Event + Clone + Send + Sync;

    /// Returns the stream query used to retrieve relevant events for building the state.
    fn query(&self) -> StreamQuery<Self::Event>;
}

impl<S, E: Clone> From<&S> for StreamQuery<E>
where
    S: StateQuery<Event = E>,
{
    fn from(state: &S) -> Self {
        state.query()
    }
}

/// A structure representing a sub-state in a multi-state object. It encapsulates
/// the version, applied events count, and the payload of a sub-state.
///
/// # Type Parameters
///
/// - `S`: The type implementing the `StateMutate` trait, representing the sub-state.
#[derive(Clone, Serialize, Deserialize)]
pub struct StatePart<S: StateQuery> {
    /// The version of the sub-state.
    version: i64,
    /// The count of events applied to the sub-state.
    applied_events: u64,
    /// The payload of the sub-state.
    inner: S,
}

impl<S: StateQuery> StatePart<S> {
    pub fn new(version: i64, payload: S) -> Self {
        Self {
            version,
            applied_events: 0,
            inner: payload,
        }
    }
    pub fn version(&self) -> i64 {
        self.version
    }
    pub fn applied_events(&self) -> u64 {
        self.applied_events
    }
    pub fn query_part(&self) -> StreamQuery<<S as StateQuery>::Event> {
        self.inner.query().change_origin(self.version)
    }

    pub fn matches_event<U>(&self, event: &PersistedEvent<U>) -> bool
    where
        U: Event + Clone,
        <S as StateQuery>::Event: Into<U>,
    {
        matches_filter(event, self.query_part().convert().filter())
    }
    pub fn mutate_part<E>(&mut self, event: PersistedEvent<E>)
    where
        E: Event,
        S: StateMutate,
        <S as StateQuery>::Event: TryFrom<E>,
        <<S as StateQuery>::Event as TryFrom<E>>::Error: StdError + 'static + Send + Sync,
    {
        self.version = event.id;
        self.applied_events += 1;
        self.inner.mutate(event.event.try_into().unwrap());
    }
}

impl<S: StateQuery> Deref for StatePart<S> {
    type Target = S;

    fn deref(&self) -> &S {
        &self.inner
    }
}

/// Converts an state into `StatePart`s.
///
/// This trait is used to initialize a multi-state object by converting a state into a state part
/// with version and event information.
///
/// # Type Parameters
///
/// - `T`: The type of the object that can be converted into a `StatePart`.
///
/// # Associated Types
///
/// - `Target`: The resulting type after conversion, representing a `StatePart`.
pub trait IntoStatePart<T>: Sized {
    type Target;
    /// Converts the object into a `StatePart`.
    ///
    /// # Returns
    ///
    /// Returns the resulting `StatePart` after the conversion.
    fn into_state_part(self) -> Self::Target;
}

/// Extracts the state payload from a `StatePart`.
///
/// # Type Parameters
///
/// - `T`: The type representing the concrete state to be obtained from the `StatePart`.
pub trait IntoState<T>: Sized {
    /// Converts the `StatePart` into a concrete state type.
    ///
    /// # Returns
    ///
    /// Returns the concrete state obtained from the `StatePart`.
    fn into_state(self) -> T;
}

fn matches_filter<E: Event>(event: &PersistedEvent<E>, filter: &StreamFilter) -> bool {
    match filter {
        StreamFilter::Events { names } => names.contains(&event.name()),
        StreamFilter::ExcludeEvents { names } => !names.contains(&event.name()),
        StreamFilter::Eq { ident, value } => event
            .domain_identifiers()
            .get(ident)
            .map(|v| v == value)
            .unwrap_or(true),
        StreamFilter::And { l, r } => matches_filter(event, l) && matches_filter(event, r),
        StreamFilter::Or { l, r } => matches_filter(event, l) || matches_filter(event, r),
        StreamFilter::Origin { id } => event.id() > *id,
    }
}

macro_rules! impl_from_state {
    (
        [$($ty:ident),*], $last:ident
    ) => {
        #[allow(unused_parens)]
        impl<$($ty,)* $last> IntoStatePart<($($ty,)* $last)> for ($($ty,)* $last) where
            $($ty: StateQuery,)*
            $last: StateQuery,
        {
            type Target = ($(StatePart<$ty>,)* StatePart<$last>);
            paste::paste! {
                fn into_state_part(self) -> ($(StatePart<$ty>,)*StatePart<$last>){
                    let ($([<state_ $ty:lower>],)* [<state_ $last:lower>])= self;
                    ($(StatePart{ inner: [<state_ $ty:lower>], version: 0, applied_events: 0},)* StatePart{inner: [<state_ $last:lower>], version: 0, applied_events: 0})
                }
            }
        }

        #[allow(unused_parens)]
        impl<$($ty,)* $last> IntoState<($($ty,)* $last)> for ($(StatePart<$ty>,)* StatePart<$last>) where
            $($ty: StateQuery,)*
            $last: StateQuery,
        {
            paste::paste! {
                fn into_state(self) -> ($($ty,)* $last){
                    let ($([<state_ $ty:lower>],)* [<state_ $last:lower>])= self;
                    ($( [<state_ $ty:lower>].inner,)* [<state_ $last:lower>].inner)
                }
            }
        }
    }
}

all_the_tuples!(impl_from_state);

#[cfg(test)]
mod test {
    use futures::executor::block_on;

    use super::*;
    use crate::utils::tests::*;

    #[test]
    fn it_mutates_all() {
        let mut state = (Cart::new("c1"), Cart::new("c2")).into_state_part();
        state.mutate_all(PersistedEvent::new(1, item_added_event("p1", "c1")));
        state.mutate_all(PersistedEvent::new(2, item_added_event("p2", "c2")));
        let (cart1, cart2) = state;
        assert_eq!(cart1.version, 1);
        assert_eq!(cart1.applied_events, 1);
        assert_eq!(cart1.into_state(), cart("c1", ["p1".to_string()]));
        assert_eq!(cart2.version, 2);
        assert_eq!(cart2.applied_events, 1);
        assert_eq!(cart2.into_state(), cart("c2", ["p2".to_string()]));
    }

    #[test]
    fn it_queries_all() {
        let cart1 = Cart::new("c1");
        let cart2 = Cart::new("c2");
        let state = (cart1.clone(), cart2.clone()).into_state_part();
        let query: StreamQuery<ShoppingCartEvent> = state.query_all();
        assert_eq!(
            query,
            union!(
                cart1.query().change_origin(0),
                cart2.query().change_origin(0)
            )
        );
    }

    #[test]
    fn it_stores_all() {
        let multi_state = (cart("c1", []), cart("c2", [])).into_state_part();
        let mut snapshotter = MockStateSnapshotter::new();
        snapshotter
            .expect_store_snapshot()
            .once()
            .withf(|s: &StatePart<Cart>| s.inner == cart("c1", []))
            .return_once(|_| Ok(()));
        snapshotter
            .expect_store_snapshot()
            .once()
            .withf(|s: &StatePart<Cart>| s.inner == cart("c2", []))
            .return_once(|_| Ok(()));
        block_on(multi_state.store_all(&snapshotter)).unwrap();
    }

    #[test]
    fn it_loads_all() {
        let mut multi_state = (cart("c1", []), cart("c2", [])).into_state_part();
        let mut snapshotter = MockStateSnapshotter::new();
        snapshotter
            .expect_load_snapshot()
            .once()
            .withf(|q| q.inner == cart("c1", []))
            .returning(|_| cart("c1", ["p1".to_owned()]).into_state_part());
        snapshotter
            .expect_load_snapshot()
            .once()
            .withf(|q| q.inner == cart("c2", []))
            .returning(|_| cart("c2", ["p2".to_owned()]).into_state_part());
        block_on(multi_state.load_all(&snapshotter));
        let (cart1, cart2) = multi_state;
        assert_eq!(cart1.inner, cart("c1", ["p1".to_owned()]));
        assert_eq!(cart2.inner, cart("c2", ["p2".to_owned()]));
    }
}