rsiot/message/
msg_data.rs

1use serde::{Deserialize, Serialize};
2
3use super::{system_messages::*, MsgDataBound, MsgKey, TimeToLiveValue};
4
5/// Тип сообщения
6#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
7pub enum MsgData<TCustom> {
8    /// Системные сообщения
9    System(System),
10    /// Пользовательские сообщения
11    Custom(TCustom),
12}
13
14impl<TMsg> MsgData<TMsg>
15where
16    TMsg: MsgDataBound,
17{
18    /// Задать ограничение времени жизни сообщения
19    pub fn define_time_to_live(&self) -> TimeToLiveValue {
20        match &self {
21            MsgData::System(_) => TimeToLiveValue::Infinite,
22            MsgData::Custom(data) => data.define_time_to_live(),
23        }
24    }
25
26    /// Получить ключ сообщения
27    pub fn key(&self) -> String {
28        match &self {
29            MsgData::System(system) => format!("System-{}", system.key()),
30            MsgData::Custom(custom) => format!("Custom-{}", custom.key()),
31        }
32    }
33}
34
35impl<TCustom> MsgData<TCustom> where TCustom: MsgDataBound {}