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
//! Provides a trivial implementation of event/snapshot/entity source/sink/store constructs.

use cqrs_core::{
    Aggregate, AggregateEvent, AggregateId, EventNumber, EventSink, EventSource, Precondition,
    Since, SnapshotSink, SnapshotSource, Version, VersionedAggregate, VersionedEvent,
};
use std::{fmt, iter::Empty, marker::PhantomData};
use void::Void;

/// A trivial store that never has any events, and which always succeeds in
/// persisting data (which is immediately dropped).
#[derive(Clone, Copy)]
pub struct NullEventStore<A, E>(PhantomData<*const (A, E)>)
where
    A: Aggregate,
    E: AggregateEvent<A>;

impl<A, E> NullEventStore<A, E>
where
    A: Aggregate,
    E: AggregateEvent<A>,
{
    /// A constant value representing a trivial event store.
    pub const DEFAULT: Self = NullEventStore(PhantomData);
}

impl<A, E> Default for NullEventStore<A, E>
where
    A: Aggregate,
    E: AggregateEvent<A>,
{
    #[inline(always)]
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl<A, E> fmt::Debug for NullEventStore<A, E>
where
    A: Aggregate,
    E: AggregateEvent<A>,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_tuple("NullEventStore").finish()
    }
}

impl<A, E> EventSource<A, E> for NullEventStore<A, E>
where
    A: Aggregate,
    E: AggregateEvent<A>,
{
    type Error = Void;
    type Events = Empty<VersionedEvent<E>>;

    #[inline]
    fn read_events<I>(
        &self,
        _id: &I,
        _version: Since,
        _max_count: Option<u64>,
    ) -> Result<Option<Self::Events>, Self::Error>
    where
        I: AggregateId<A>,
    {
        Ok(None)
    }
}

impl<A, E, M> EventSink<A, E, M> for NullEventStore<A, E>
where
    A: Aggregate,
    E: AggregateEvent<A>,
{
    type Error = Void;

    #[inline]
    fn append_events<I>(
        &self,
        _id: &I,
        _events: &[E],
        _expect: Option<Precondition>,
        _metadata: M,
    ) -> Result<EventNumber, Self::Error>
    where
        I: AggregateId<A>,
    {
        Ok(EventNumber::MIN_VALUE)
    }
}

/// A trivial store that never has any snapshots, and which always succeeds in
/// persisting data (which is immediately dropped).
#[derive(Clone, Copy)]
pub struct NullSnapshotStore<A>(PhantomData<*const A>)
where
    A: Aggregate;

impl<A> NullSnapshotStore<A>
where
    A: Aggregate,
{
    /// A constant value representing a trivial event store.
    pub const DEFAULT: Self = NullSnapshotStore(PhantomData);
}

impl<A> Default for NullSnapshotStore<A>
where
    A: Aggregate,
{
    #[inline(always)]
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl<A> fmt::Debug for NullSnapshotStore<A>
where
    A: Aggregate,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_tuple("NullSnapshotStore").finish()
    }
}

impl<A> SnapshotSource<A> for NullSnapshotStore<A>
where
    A: Aggregate,
{
    type Error = Void;

    #[inline]
    fn get_snapshot<I>(&self, _id: &I) -> Result<Option<VersionedAggregate<A>>, Self::Error>
    where
        I: AggregateId<A>,
        Self: Sized,
    {
        Ok(None)
    }
}

impl<A> SnapshotSink<A> for NullSnapshotStore<A>
where
    A: Aggregate,
{
    type Error = Void;

    #[inline]
    fn persist_snapshot<I>(
        &self,
        _id: &I,
        _aggregate: &A,
        _version: Version,
        last_snapshot_version: Option<Version>,
    ) -> Result<Version, Self::Error>
    where
        I: AggregateId<A>,
        Self: Sized,
    {
        Ok(last_snapshot_version.unwrap_or_default())
    }
}

#[cfg(test)]
#[path = "trivial_tests.rs"]
mod tests;