1#![allow(clippy::module_inception)]
2
3use std::fmt::Debug;
4
5use serde::{Deserialize, Serialize};
6use uuid::Uuid;
7
8use super::{MsgData, MsgDataBound, Timestamp};
9
10#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
13pub struct Message<TMsg> {
14 pub data: MsgData<TMsg>,
16 pub key: String,
18 pub ts: Timestamp,
20
21 cmp_source: Uuid,
22}
23impl<TMsg> Message<TMsg>
26where
27 TMsg: MsgDataBound,
28{
29 pub fn new(data: MsgData<TMsg>) -> Self {
31 let key = data.key();
32 Self {
33 data,
34 key,
35 ts: Default::default(),
36 cmp_source: Uuid::default(),
37 }
38 }
39
40 pub fn new_custom(custom_data: TMsg) -> Self {
42 let data = MsgData::Custom(custom_data);
43 let key = data.key();
44 Self {
45 data,
46 key,
47 ts: Default::default(),
48 cmp_source: Uuid::default(),
49 }
50 }
51
52 pub fn get_custom_data(&self) -> Option<TMsg> {
54 match &self.data {
55 MsgData::System(_) => None,
56 MsgData::Custom(data) => Some(data.clone()),
57 }
58 }
59
60 pub fn set_cmp_source(&mut self, id: &Uuid) {
62 self.cmp_source = *id;
63 }
64
65 pub fn check_source(&self, id: &Uuid) -> bool {
67 &self.cmp_source == id
68 }
69}