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
use crate::dispatcher::Dispatcher;
use crate::reactor::Reactor;
use crate::reducer::Reducer;
use core::{mem, ops::Deref};

#[cfg(feature = "async")]
use pin_project::*;

/// A reactive state container.
///
/// The only way to mutate the internal state managed by [`Store`] is by
/// [dispatching] actions on it.
/// The associated [`Reactor`] is notified upon every state transition.
///
/// [dispatching]: trait.Dispatcher.html#tymethod.dispatch
///
/// # Example
///
/// ```rust
/// use reducer::*;
/// use std::error::Error;
/// use std::io::{self, Write};
///
/// // The state of your app.
/// struct Calculator(i32);
///
/// // Actions the user can trigger.
/// struct Add(i32);
/// struct Sub(i32);
/// struct Mul(i32);
/// struct Div(i32);
///
/// impl Reducer<Add> for Calculator {
///     fn reduce(&mut self, Add(x): Add) {
///         self.0 += x;
///     }
/// }
///
/// impl Reducer<Sub> for Calculator {
///     fn reduce(&mut self, Sub(x): Sub) {
///         self.0 -= x;
///     }
/// }
///
/// impl Reducer<Mul> for Calculator {
///     fn reduce(&mut self, Mul(x): Mul) {
///         self.0 *= x;
///     }
/// }
///
/// impl Reducer<Div> for Calculator {
///     fn reduce(&mut self, Div(x): Div) {
///         self.0 /= x;
///     }
/// }
///
/// // The user interface.
/// struct Console;
///
/// impl Reactor<Calculator> for Console {
///     type Error = io::Error;
///     fn react(&mut self, state: &Calculator) -> io::Result<()> {
///         io::stdout().write_fmt(format_args!("{}\n", state.0))
///     }
/// }
///
/// fn main() -> Result<(), Box<dyn Error>> {
///     let mut store = Store::new(Calculator(0), Console);
///
///     store.dispatch(Add(5))?; // displays "5"
///     store.dispatch(Mul(3))?; // displays "15"
///     store.dispatch(Sub(1))?; // displays "14"
///     store.dispatch(Div(7))?; // displays "2"
///
///     Ok(())
/// }
/// ```
#[cfg_attr(feature = "async", pin_project)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)]
pub struct Store<S, R: Reactor<S>> {
    state: S,
    #[cfg_attr(feature = "async", pin)]
    reactor: R,
}

impl<S, R: Reactor<S>> Store<S, R> {
    /// Constructs the Store given the initial state and a [`Reactor`].
    pub fn new(state: S, reactor: R) -> Self {
        Self { state, reactor }
    }

    /// Replaces the [`Reactor`] and returns the previous one.
    pub fn subscribe(&mut self, reactor: impl Into<R>) -> R {
        mem::replace(&mut self.reactor, reactor.into())
    }
}

/// View Store as a read-only owning smart pointer to the state.
impl<S, R: Reactor<S>> Deref for Store<S, R> {
    type Target = S;

    /// Grants read access to the current state.
    fn deref(&self) -> &Self::Target {
        &self.state
    }
}

impl<A, S, R> Dispatcher<A> for Store<S, R>
where
    S: Reducer<A>,
    R: Reactor<S>,
{
    type Output = Result<(), R::Error>;

    /// Updates the state via [`Reducer::reduce`] and notifies the [`Reactor`],
    /// returning the result of calling [`Reactor::react`] with a reference
    /// to the new state.
    fn dispatch(&mut self, action: A) -> Self::Output {
        self.state.reduce(action);
        self.reactor.react(&self.state)
    }
}

#[cfg(feature = "async")]
mod sink {
    use super::*;
    use futures::sink::Sink;
    use futures::task::{Context, Poll};
    use std::pin::Pin;

    /// View Store as a Sink of actions (requires [`async`]).
    ///
    /// [`async`]: index.html#optional-features
    impl<A, S, R, E> Sink<A> for Store<S, R>
    where
        S: Reducer<A> + Clone,
        R: Reactor<S, Error = E> + Sink<S, Error = E>,
    {
        type Error = E;

        fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
            self.project().reactor.poll_ready(cx)
        }

        #[project]
        fn start_send(self: Pin<&mut Self>, action: A) -> Result<(), Self::Error> {
            #[project]
            let Store { state, reactor } = self.project();
            state.reduce(action);
            reactor.start_send(state.clone())
        }

        fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
            self.project().reactor.poll_flush(cx)
        }

        fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
            self.project().reactor.poll_close(cx)
        }
    }
}

#[cfg(feature = "async")]
pub use sink::*;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::reactor::MockReactor;
    use crate::reducer::MockReducer;
    use mockall::predicate::*;
    use proptest::prelude::*;

    #[cfg(feature = "async")]
    use futures::{executor::block_on, sink::SinkExt};

    #[test]
    fn default() {
        Store::<MockReducer<()>, MockReactor<_, ()>>::default();
    }

    #[test]
    fn deref() {
        let store = Store::new(MockReducer::<()>::new(), MockReactor::<_, ()>::new());
        assert_eq!(&*store as *const _, &store.state as *const _);
    }

    proptest! {
        #[test]
        fn new(a: usize, b: usize) {
            let mut reducer = MockReducer::<()>::new();
            reducer.expect_id().return_const(a);

            let mut reactor = MockReactor::<_, ()>::new();
            reactor.expect_id().return_const(b);

            let store = Store::new(reducer, reactor);

            assert_eq!(store.state.id(), a);
            assert_eq!(store.reactor.id(), b);
        }

        #[test]
        fn clone(a: usize, b: usize) {
            let mut reducer = MockReducer::<()>::new();
            reducer.expect_id().return_const(a);
            reducer.expect_clone().times(1).returning(move || {
                let mut mock = MockReducer::new();
                mock.expect_id().return_const(a);
                mock
            });

            let mut reactor = MockReactor::<_, ()>::new();
            reactor.expect_id().return_const(b);
            reactor.expect_clone().times(1).returning(move || {
                let mut mock = MockReactor::new();
                mock.expect_id().return_const(b);
                mock
            });

            #[allow(clippy::redundant_clone)]
            let store = Store::new(reducer, reactor).clone();

            assert_eq!(store.state.id(), a);
            assert_eq!(store.reactor.id(), b);
        }

        #[test]
        fn subscribe(a: usize, b: usize) {
            let mut mock = MockReactor::<_, ()>::new();
            mock.expect_id().return_const(a);

            let mut store = Store::new(MockReducer::<()>::new(), mock);

            let mut mock = MockReactor::<_, ()>::new();
            mock.expect_id().return_const(b);

            assert_eq!(store.subscribe(mock).id(), a);
            assert_eq!(store.reactor.id(), b);
        }

        #[test]
        fn dispatch(action: u8, result: Result<(), u8>, id: usize) {
            let mut reducer = MockReducer::new();
            reducer.expect_id().return_const(id);
            reducer.expect_clone().never();
            reducer
                .expect_reduce()
                .with(eq(action))
                .times(1)
                .return_const(());

            let mut reactor = MockReactor::new();
            reactor
                .expect_react()
                .with(function(move |x: &MockReducer<_>| x.id() == id))
                .times(1)
                .return_const(result);

            let mut store = Store::new(reducer, reactor);
            assert_eq!(Dispatcher::dispatch(&mut store, action), result);
        }

        #[cfg(feature = "async")]
        #[test]
        fn sink(action: u8, result: Result<(), u8>, id: usize) {
            let mut reducer = MockReducer::new();
            reducer.expect_id().return_const(id);
            reducer.expect_clone().returning(move || {
                let mut mock = MockReducer::new();
                mock.expect_id().return_const(id);
                mock.expect_reduce().never();
                mock.expect_clone().never();
                mock
            });

            reducer
                .expect_reduce()
                .with(eq(action))
                .times(1)
                .return_const(());

            let mut reactor = MockReactor::new();
            reactor
                .expect_react()
                .with(function(move |x: &MockReducer<_>| x.id() == id))
                .times(1)
                .return_const(result);

            let mut store = Store::new(reducer, reactor);
            assert_eq!(block_on(store.send(action)), result);
            assert_eq!(block_on(store.close()), Ok(()));
        }
    }
}