Skip to main content

state_m/
state.rs

1use chrono::{DateTime, Utc};
2use derivative::Derivative;
3use std::fmt::{self, Debug, Display};
4use tokio::sync::mpsc;
5
6/// State type.
7/// * `value` - the state value.
8/// * `timestamp` - timestamp at which the current state value was created.
9#[derive(Clone)]
10pub struct State<S>
11where
12    S: Default,
13{
14    pub value: S,
15    pub timestamp: DateTime<Utc>,
16}
17
18impl<S> Default for State<S>
19where
20    S: Default,
21{
22    fn default() -> Self {
23        Self {
24            value: Default::default(),
25            timestamp: Utc::now(),
26        }
27    }
28}
29
30impl<S> Debug for State<S>
31where
32    S: Debug + Default,
33{
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        write!(f, "{} | {:?}", self.timestamp, self.value)
36    }
37}
38
39impl<S> Display for State<S>
40where
41    S: Display + Default,
42{
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        write!(f, "{} | {}", self.timestamp, self.value)
45    }
46}
47
48/// State change event.
49/// * `state` - state associated with event.
50#[derive(Clone, Derivative)]
51#[derivative(Debug)]
52pub struct StateEvent<S>
53where
54    S: Default,
55{
56    pub state: State<S>,
57    pub(crate) is_touch: bool,
58    #[derivative(Debug = "ignore")]
59    pub(crate) close_handle: Option<mpsc::Sender<()>>,
60}
61
62impl<S> Default for StateEvent<S>
63where
64    S: Default,
65{
66    fn default() -> Self {
67        Self {
68            state: Default::default(),
69            is_touch: Default::default(),
70            close_handle: Default::default(),
71        }
72    }
73}