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
/// Reduce trait that enables reducing self via an action. It is possibly to implement this trait
/// for a different Action types. Action is usually an enum.
/// ```
/// extern crate reduxr;
///
/// use reduxr::*;
///
/// struct LampState {
/// power: bool,
/// }
///
/// enum LampOnOffAction {
/// TurnOn,
/// TurnOff,
/// }
///
/// impl Reduce<LampOnOffAction> for LampState {
/// fn reduce(self, action: LampOnOffAction) -> Self {
/// match action {
/// LampOnOffAction::TurnOn => LampState { power: true },
/// LampOnOffAction::TurnOff => LampState { power: false },
/// }
/// }
/// }
///
/// enum LampSwitchAction {
/// Switch,
/// }
///
/// impl Reduce<LampSwitchAction> for LampState {
/// fn reduce(self, action: LampSwitchAction) -> Self {
/// match action {
/// LampSwitchAction::Switch => LampState { power: !self.power },
/// }
/// }
/// }
///
/// let state = LampState { power: false };
/// assert_eq!(state.power, false);
///
/// let state = state.reduce(LampOnOffAction::TurnOn);
/// assert_eq!(state.power, true);
///
/// let state = state.reduce(LampOnOffAction::TurnOff);
/// assert_eq!(state.power, false);
///
/// let state = state.reduce(LampSwitchAction::Switch);
/// assert_eq!(state.power, true);
///
/// let state = state.reduce(LampSwitchAction::Switch);
/// assert_eq!(state.power, false);
/// ```