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
use crate::{
    aggregate::{Aggregate, AggregateEvent, AggregateId},
    types::{
        CqrsError, EventNumber, Precondition, Since, SnapshotRecommendation, Version,
        VersionedAggregate, VersionedEvent,
    },
};

/// A source for reading/loading events.
pub trait EventSource<A, E>
where
    A: Aggregate,
    E: AggregateEvent<A>,
{
    /// Represents the sequence of events read from the event source.
    type Events: IntoIterator<Item = VersionedEvent<E>>;

    /// The error type.
    type Error: CqrsError;

    /// Reads events from the event source for a given identifier.
    ///
    /// Only loads events after the event number provided in `since` (See [Since]), and will only load a maximum of
    /// `max_count` events, if given. If not given, will read all remaining events.
    fn read_events<I>(
        &self,
        id: &I,
        since: Since,
        max_count: Option<u64>,
    ) -> Result<Option<Self::Events>, Self::Error>
    where
        I: AggregateId<A>;
}

/// A sink for writing/persisting events with associated metadata.
pub trait EventSink<A, E, M>
where
    A: Aggregate,
    E: AggregateEvent<A>,
{
    /// The error type.
    type Error: CqrsError;

    /// Appends events to a given source, with an optional precondition, and associated metadata.
    ///
    /// The associated metadata is applied to all events in the append group.
    fn append_events<I>(
        &self,
        id: &I,
        events: &[E],
        precondition: Option<Precondition>,
        metadata: M,
    ) -> Result<EventNumber, Self::Error>
    where
        I: AggregateId<A>;
}

/// A source for reading/loading snapshots of aggregates.
pub trait SnapshotSource<A>
where
    A: Aggregate,
{
    /// The error type.
    type Error: CqrsError;

    /// Loads a versioned aggregate from the snapshot source.
    fn get_snapshot<I>(&self, id: &I) -> Result<Option<VersionedAggregate<A>>, Self::Error>
    where
        I: AggregateId<A>;
}

/// A sink for writing/persisting snapshots of aggregates.
pub trait SnapshotSink<A>
where
    A: Aggregate,
{
    /// The error type.
    type Error: CqrsError;

    /// Writes an aggregate with its version to the sink. Returns the version number of the latest snapshot.
    fn persist_snapshot<I>(
        &self,
        id: &I,
        aggregate: &A,
        version: Version,
        last_snapshot_version: Option<Version>,
    ) -> Result<Version, Self::Error>
    where
        I: AggregateId<A>;
}

/// A strategy determining when to recommend a snapshot be taken.
pub trait SnapshotStrategy {
    /// Gives the sink's recommendation on whether or not to perform a snapshot
    fn snapshot_recommendation(
        &self,
        version: Version,
        last_snapshot_version: Option<Version>,
    ) -> SnapshotRecommendation;
}

/// A snapshot strategy that will never recommend taking a snapshot.
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
pub struct NeverSnapshot;

impl SnapshotStrategy for NeverSnapshot {
    fn snapshot_recommendation(&self, _: Version, _: Option<Version>) -> SnapshotRecommendation {
        SnapshotRecommendation::DoNotSnapshot
    }
}

/// A snapshot strategy that will always recommend taking a snapshot.
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
pub struct AlwaysSnapshot;

impl SnapshotStrategy for AlwaysSnapshot {
    fn snapshot_recommendation(&self, _: Version, _: Option<Version>) -> SnapshotRecommendation {
        SnapshotRecommendation::ShouldSnapshot
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{AggregateCommand, AggregateEvent, Event};
    use void::Void;

    /// A test aggregate with no state
    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
    pub struct TestAggregate;

    /// A test event with no data
    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
    pub struct TestEvent;

    /// A test command with no data
    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
    pub struct TestCommand;

    /// A test metadata with no data
    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
    pub struct TestMetadata;

    impl Aggregate for TestAggregate {
        fn aggregate_type() -> &'static str {
            "test"
        }
    }

    impl AggregateEvent<TestAggregate> for TestEvent {
        fn apply_to(self, _aggregate: &mut TestAggregate) {}
    }

    impl AggregateCommand<TestAggregate> for TestCommand {
        type Error = Void;
        type Event = TestEvent;
        type Events = Vec<TestEvent>;

        fn execute_on(self, _aggregate: &TestAggregate) -> Result<Self::Events, Self::Error> {
            Ok(Vec::default())
        }
    }

    impl Event for TestEvent {
        fn event_type(&self) -> &'static str {
            "test"
        }
    }
}