fmodel_rust/
view.rs

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
use crate::{EvolveFunction, InitialStateFunction, Sum};

/// [View] represents the event handling algorithm, responsible for translating the events into denormalized state, which is more adequate for querying.
/// It has two generic parameters `S`/State, `E`/Event , representing the type of the values that View may contain or use.
/// `'a` is used as a lifetime parameter, indicating that all references contained within the struct (e.g., references within the function closures) must have a lifetime that is at least as long as 'a.
///
/// ## Example
/// ```
/// use fmodel_rust::view::View;
///
/// fn view<'a>() -> View<'a, OrderViewState, OrderEvent> {
///     View {
///        // Exhaustive pattern matching is used to handle the events (modeled as Enum - SUM/OR type).
///        evolve: Box::new(|state, event| {
///             let mut new_state = state.clone();
///             match event {
///                OrderEvent::Created(created_event) => {
///                     new_state.order_id = created_event.order_id;
///                     new_state.customer_name = created_event.customer_name.to_owned();
///                     new_state.items = created_event.items.to_owned();
///                 }
///                 OrderEvent::Updated(updated_event) => {
///                     new_state.items = updated_event.updated_items.to_owned();
///                 }
///                 OrderEvent::Cancelled(_) => {
///                     new_state.is_cancelled = true;
///                 }
///             }
///             new_state
///         }),
///         initial_state: Box::new(|| OrderViewState {
///             order_id: 0,
///             customer_name: "".to_string(),
///             items: Vec::new(),
///             is_cancelled: false,
///         }),
///     }
/// }
///
/// #[derive(Debug)]
/// pub enum OrderEvent {
///     Created(OrderCreatedEvent),
///     Updated(OrderUpdatedEvent),
///     Cancelled(OrderCancelledEvent),
/// }
///
/// #[derive(Debug)]
/// pub struct OrderCreatedEvent {
///     pub order_id: u32,
///     pub customer_name: String,
///     pub items: Vec<String>,
/// }
///
/// #[derive(Debug)]
/// pub struct OrderUpdatedEvent {
///     pub order_id: u32,
///     pub updated_items: Vec<String>,
/// }
///
/// #[derive(Debug)]
/// pub struct OrderCancelledEvent {
///     pub order_id: u32,
/// }
///
/// #[derive(Debug, Clone)]
/// struct OrderViewState {
///     order_id: u32,
///     customer_name: String,
///     items: Vec<String>,
///     is_cancelled: bool,
/// }
///
/// let view: View<OrderViewState, OrderEvent> = view();
/// let order_created_event = OrderEvent::Created(OrderCreatedEvent {
///     order_id: 1,
///     customer_name: "John Doe".to_string(),
///     items: vec!["Item 1".to_string(), "Item 2".to_string()],
/// });
/// let new_state = (view.evolve)(&(view.initial_state)(), &order_created_event);
/// ```
pub struct View<'a, S: 'a, E: 'a> {
    /// The `evolve` function is the main state evolution algorithm.
    pub evolve: EvolveFunction<'a, S, E>,
    /// The `initial_state` function is the initial state.
    pub initial_state: InitialStateFunction<'a, S>,
}

impl<'a, S, E> View<'a, S, E> {
    /// Maps the View over the S/State type parameter.
    /// Creates a new instance of [View]`<S2, E>`.
    pub fn map_state<S2, F1, F2>(self, f1: &'a F1, f2: &'a F2) -> View<'a, S2, E>
    where
        F1: Fn(&S2) -> S + Send + Sync,
        F2: Fn(&S) -> S2 + Send + Sync,
    {
        let new_evolve = Box::new(move |s2: &S2, e: &E| {
            let s = f1(s2);
            f2(&(self.evolve)(&s, e))
        });

        let new_initial_state = Box::new(move || f2(&(self.initial_state)()));

        View {
            evolve: new_evolve,
            initial_state: new_initial_state,
        }
    }

    /// Maps the View over the E/Event type parameter.
    /// Creates a new instance of [View]`<S, E2>`.
    pub fn map_event<E2, F>(self, f: &'a F) -> View<'a, S, E2>
    where
        F: Fn(&E2) -> E + Send + Sync,
    {
        let new_evolve = Box::new(move |s: &S, e2: &E2| {
            let e = f(e2);
            (self.evolve)(s, &e)
        });

        let new_initial_state = Box::new(move || (self.initial_state)());

        View {
            evolve: new_evolve,
            initial_state: new_initial_state,
        }
    }

    /// Combines two views into one.
    /// Creates a new instance of a View by combining two views of type `S`, `E` and `S2`, `E2` into a new view of type `(S, S2)`, `Sum<E, E2>`
    pub fn combine<S2: Clone, E2>(self, view2: View<'a, S2, E2>) -> View<'a, (S, S2), Sum<E, E2>>
    where
        S: Clone,
        S2: Clone,
    {
        let new_evolve = Box::new(move |s: &(S, S2), e: &Sum<E, E2>| match e {
            Sum::First(e) => {
                let s1 = &s.0;
                let new_state = (self.evolve)(s1, e);
                (new_state, s.1.to_owned())
            }
            Sum::Second(e) => {
                let s2 = &s.1;
                let new_state = (view2.evolve)(s2, e);
                (s.0.to_owned(), new_state)
            }
        });

        let new_initial_state = Box::new(move || {
            let s1 = (self.initial_state)();
            let s2 = (view2.initial_state)();
            (s1, s2)
        });

        View {
            evolve: new_evolve,
            initial_state: new_initial_state,
        }
    }
}

/// Formalizes the `State Computation` algorithm for the `view` to handle events based on the current state, and produce new state.
pub trait ViewStateComputation<E, S> {
    /// Computes new state based on the current state and the events.
    fn compute_new_state(&self, current_state: Option<S>, events: &[&E]) -> S;
}

impl<'a, S, E> ViewStateComputation<E, S> for View<'a, S, E> {
    /// Computes new state based on the current state and the events.
    fn compute_new_state(&self, current_state: Option<S>, events: &[&E]) -> S {
        let effective_current_state = current_state.unwrap_or_else(|| (self.initial_state)());
        events.iter().fold(effective_current_state, |state, event| {
            (self.evolve)(&state, event)
        })
    }
}