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
/*
    Appellation: actors <module>
    Contributors: FL03 <jo3mccain@icloud.com> (https://github.com)
    Description:
        ... Summary ...
*/
use crate::{messages::Message, states::Stateful};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fmt::Display;

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

impl<T: Display> State<T> {
    pub fn new(events: Vec<String>, message: Message<T>) -> Self {
        let metadata = Value::default();
        let timestamp = chrono::Utc::now().timestamp();
        Self {
            events,
            message,
            metadata,
            timestamp,
        }
    }
}

impl<T: Clone + Default + Display + Serialize> Stateful for State<T> {
    type Data = T;

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

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

impl<T: Default + std::fmt::Display> Default for State<T> {
    fn default() -> Self {
        Self::new(Default::default(), Default::default())
    }
}

impl<T: Display> std::convert::From<T> for State<T> {
    fn from(data: T) -> Self {
        Self::new(Default::default(), Message::from(data))
    }
}

impl<T: Display + Serialize> std::fmt::Display for State<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(
            f,
            "{}",
            serde_json::to_string_pretty(&self).unwrap().to_lowercase()
        )
    }
}