cqrs_es2/queries/
query_context.rs

1use log::trace;
2use std::{
3    fmt::Debug,
4    marker::PhantomData,
5};
6
7use crate::{
8    commands::ICommand,
9    events::IEvent,
10};
11
12use super::i_query::IQuery;
13
14/// Returns the query and context around it that is needed when
15/// committing in a query store implementation.
16#[derive(Debug, PartialEq, Clone)]
17pub struct QueryContext<C: ICommand, E: IEvent, Q: IQuery<C, E>> {
18    /// The id of the aggregate instance.
19    pub aggregate_id: String,
20
21    /// The current version number for this query instance.
22    pub version: i64,
23
24    /// The current state of the query instance.
25    pub payload: Q,
26
27    _phantom: PhantomData<(C, E)>,
28}
29
30impl<C: ICommand, E: IEvent, Q: IQuery<C, E>> QueryContext<C, E, Q> {
31    /// Constructor
32    pub fn new(
33        aggregate_id: String,
34        version: i64,
35        payload: Q,
36    ) -> Self {
37        let x = Self {
38            aggregate_id,
39            version,
40            payload,
41            _phantom: PhantomData,
42        };
43
44        trace!("Created new {:?}", x,);
45
46        x
47    }
48}