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
use std::{fmt, num::NonZeroU64};

/// Represents an event sequence number, starting at 1
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct EventNumber(NonZeroU64);

impl EventNumber {
    /// The minimum [EventNumber].
    #[allow(unsafe_code)]
    pub const MIN_VALUE: EventNumber =
        // One is absolutely non-zero, and this is required for this to be usable in a `const` context.
        EventNumber(unsafe {NonZeroU64::new_unchecked(1)});

    /// Attempts to create a new event number from a given number. Will return non if the given number is `0`.
    #[inline]
    pub fn new(x: u64) -> Option<Self> {
        Some(EventNumber(NonZeroU64::new(x)?))
    }

    /// Extracts the event number as a plain `u64`.
    #[inline]
    pub fn get(self) -> u64 {
        self.0.get()
    }

    /// Increments the event number to the next value.
    #[inline]
    pub fn incr(&mut self) {
        self.0 = NonZeroU64::new(self.0.get() + 1).unwrap();
    }

    /// Gets the event number after the current one.
    #[inline]
    #[must_use]
    pub fn next(self) -> Self {
        let mut slf = self;
        slf.0 = NonZeroU64::new(self.0.get() + 1).unwrap();
        slf
    }
}

impl fmt::Display for EventNumber {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(&self.0, f)
    }
}

//impl PartialEq<usize> for EventNumber {
//    #[inline]
//    fn eq(&self, rhs: &usize) -> bool {
//        *rhs == self.0
//    }
//}
//
//impl PartialEq<EventNumber> for usize {
//    #[inline]
//    fn eq(&self, rhs: &EventNumber) -> bool {
//        rhs.0 == *self
//    }
//}

/// An aggregate version.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum Version {
    /// The version of an aggregate that has not had any events applied to it.
    Initial,
    /// The version of the last event applied to the aggregate.
    Number(EventNumber),
}

impl Version {
    /// Creates a new `Version` from a number.
    ///
    /// The number `0` gets interpreted as being `Version::Initial`, while any other number is interpreted as the
    /// latest event number applied.
    #[inline]
    pub fn new(number: u64) -> Self {
        NonZeroU64::new(number)
            .map(EventNumber)
            .map(Version::Number)
            .unwrap_or(Version::Initial)
    }

    /// Increments the version number to the next in sequence.
    #[inline]
    pub fn incr(&mut self) {
        match *self {
            Version::Initial => *self = Version::Number(EventNumber::MIN_VALUE),
            Version::Number(ref mut en) => en.incr(),
        }
    }

    /// Returns the next event number in the sequence.
    #[inline]
    pub fn next_event(self) -> EventNumber {
        match self {
            Version::Initial => EventNumber::MIN_VALUE,
            Version::Number(mut en) => {
                en.incr();
                en
            }
        }
    }

    /// Gets the version number as a raw `u64`.
    #[inline]
    pub fn get(self) -> u64 {
        match self {
            Version::Initial => 0,
            Version::Number(en) => en.get(),
        }
    }

    /// Gets the version number as an [EventNumber], returning `None` if the current verison is [Version::Initial].
    #[inline]
    pub fn event_number(self) -> Option<EventNumber> {
        match self {
            Version::Initial => None,
            Version::Number(en) => Some(en),
        }
    }
}

impl fmt::Display for Version {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Version::Initial => f.write_str("initial"),
            Version::Number(ref event_number) => event_number.fmt(f),
        }
    }
}

impl Default for Version {
    #[inline]
    fn default() -> Self {
        Version::Initial
    }
}

//impl PartialEq<EventNumber> for Version {
//    fn eq(&self, rhs: &EventNumber) -> bool {
//        if let Version::Number(ref v) = *self {
//            v == rhs
//        } else {
//            false
//        }
//    }
//}

impl From<EventNumber> for Version {
    #[inline]
    fn from(event_number: EventNumber) -> Self {
        Version::Number(event_number)
    }
}

impl ::std::ops::Sub for Version {
    type Output = i64;

    fn sub(self, rhs: Version) -> Self::Output {
        let l = match self {
            Version::Initial => 0,
            Version::Number(n) => n.get() as i64,
        };
        let r = match rhs {
            Version::Initial => 0,
            Version::Number(n) => n.get() as i64,
        };

        l - r
    }
}

/// A precondition that must be upheld for a command to be executed or for events to be persisted.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum Precondition {
    /// Requires that the target aggregate must not have had any events previously applied to it.
    New,
    /// Requires that the target event at least exist, whether as a snapshot, or as having had at least one event applied.
    Exists,
    /// Requires that the target aggregate have the exact version specified.
    ExpectedVersion(Version),
}

impl Precondition {
    /// Verifies that the precondition holds, given the `current_version`. If the precondition is violated, an error is
    /// returned with the precondition.
    pub fn verify(self, current_version: Option<Version>) -> Result<(), Self> {
        match (self, current_version) {
            (Precondition::ExpectedVersion(Version::Initial), None) => Ok(()),
            (Precondition::ExpectedVersion(Version::Initial), Some(Version::Initial)) => Ok(()),
            (Precondition::ExpectedVersion(e), Some(x)) if e == x => Ok(()),
            (Precondition::New, None) => Ok(()),
            (Precondition::Exists, Some(_)) => Ok(()),
            (precondition, _) => Err(precondition),
        }
    }
}

impl From<Version> for Precondition {
    #[inline]
    fn from(v: Version) -> Self {
        Precondition::ExpectedVersion(v)
    }
}

impl fmt::Display for Precondition {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Precondition::Exists => f.write_str("expect aggregate exists"),
            Precondition::New => f.write_str("expect aggregate does not exist"),
            Precondition::ExpectedVersion(Version::Initial) => {
                f.write_str("expect aggregate to exist in initial state")
            }
            Precondition::ExpectedVersion(Version::Number(v)) => {
                write!(f, "expect aggregate to exist with version {}", v)
            }
        }
    }
}

/// A structured tuple combining an event number and an event.
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct VersionedEvent<E> {
    /// The event number.
    pub sequence: EventNumber,

    /// The event.
    pub event: E,
}

/// A structured tuple combining an event number and an event.
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct VersionedEventWithMetadata<E, M> {
    /// The event number.
    pub sequence: EventNumber,

    /// The event.
    pub event: E,

    /// The event metadata.
    pub metadata: M,
}

/// A structured tuple combining an aggregate and its current version.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct VersionedAggregate<A> {
    /// The current version of the aggregate.
    pub version: Version,

    /// The aggregate.
    pub payload: A,
}

/// The starting point when reading a stream of values from an [EventSource].
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum Since {
    /// Begins reading events from the very beginning of the stream.
    BeginningOfStream,

    /// Begins reading events after the given [EventNumber].
    ///
    /// E.g. if the event number were 4, then reading should begin at event number 5.
    Event(EventNumber),
}

impl From<Version> for Since {
    fn from(v: Version) -> Self {
        match v {
            Version::Initial => Since::BeginningOfStream,
            Version::Number(x) => Since::Event(x),
        }
    }
}

/// A recommendation on whether or not a snapshot should be persisted.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum SnapshotRecommendation {
    /// Recommends that a snapshot be taken.
    ShouldSnapshot,

    /// Recommends that a snapshot should not be taken.
    DoNotSnapshot,
}

/// Represents a common trait that all errors handled by CQRS should implement.
pub trait CqrsError: fmt::Debug + fmt::Display + Send + Sync + 'static {}

impl<T> CqrsError for T where T: fmt::Debug + fmt::Display + Send + Sync + 'static {}

/// An owned, raw view of event data.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct RawEvent {
    /// The event id.
    pub event_id: EventNumber,
    /// The aggregate type.
    pub aggregate_type: String,
    /// The entity id.
    pub entity_id: String,
    /// The sequence number of this event in the entity's event stream.
    pub sequence: EventNumber,
    /// The event type.
    pub event_type: String,
    /// The raw event payload.
    pub payload: Vec<u8>,
}

/// An owned, raw view of event data.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub struct BorrowedRawEvent<'row> {
    /// The event id.
    pub event_id: EventNumber,
    /// The aggregate type.
    pub aggregate_type: &'row str,
    /// The entity id.
    pub entity_id: &'row str,
    /// The sequence number of this event in the entity's event stream.
    pub sequence: EventNumber,
    /// The event type.
    pub event_type: &'row str,
    /// The raw event payload.
    pub payload: &'row [u8],
}