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
/*
    Appellation: state <module>
    Contrib: FL03 <jo3mccain@icloud.com> (https://github.com/FL03)
    Description: ... Summary ...
*/
use super::{StatePack, Stateful, StatefulExt};
use crate::messages::Message;

use decanter::prelude::{Hash, Hashable, H256};
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum States {
    #[default]
    Error = 0,
    Idle = 1,
}

impl StatePack for States {}

/// Implement the standard structure of a state
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct State<S: StatePack = States, T: Default = H256> {
    pub events: Vec<String>,
    pub message: Message<T>,
    pub metadata: Value,
    pub state: S,
    pub timestamp: i64,
}

impl<S: StatePack, T: Default> State<S, T> {
    pub fn new(events: Option<Vec<String>>, message: Option<Message<T>>, state: Option<S>) -> Self {
        Self {
            events: events.unwrap_or_default(),
            message: message.unwrap_or_default(),
            metadata: Default::default(),
            state: state.unwrap_or_default(),
            timestamp: chrono::Utc::now().timestamp(),
        }
    }
}

impl<S: Clone + StatePack, T: Clone + Default> Stateful<S> for State<S, T> {
    type Data = T;

    fn message(self) -> Message<Self::Data> {
        self.message
    }

    fn timestamp(self) -> i64 {
        self.timestamp
    }

    fn state(self) -> S {
        self.state
    }
}
impl<S: Clone + StatePack, T: Clone + Default> StatefulExt<S> for State<S, T> {
    fn update_state(&mut self, msg: Option<Message<Self::Data>>, state: S) -> &Self {
        self.message = msg.unwrap_or_default();
        self.state = state;
        self.timestamp = Self::now();
        self
    }
}
impl<S: StatePack, T: Default> Default for State<S, T> {
    fn default() -> Self {
        Self::new(None, None, None)
    }
}

impl<S: StatePack, T: Default> From<T> for State<S, T> {
    fn from(data: T) -> Self {
        Self::new(None, Some(Message::from(data)), None)
    }
}

impl std::fmt::Display for States {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap())
    }
}

impl<S: StatePack, T: Default> std::fmt::Display for State<S, T>
where
    S: Serialize,
    T: Serialize,
{
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string(&self).unwrap())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
    enum States {
        #[default]
        A = 0,
        B = 1,
    }

    impl StatePack for States {}

    impl std::fmt::Display for States {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            write!(f, "{}", serde_json::to_string(&self).unwrap())
        }
    }

    #[test]
    fn test_default_state() {
        let a = State::<States, serde_json::Value>::default();
        let b = a.clone();

        assert_eq!(&a, &b);
    }
}