rsiot/message/
example_message.rs1use super::{Deserialize, MsgDataBound, MsgKey, Serialize, TimeToLiveValue};
4
5#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, MsgKey)]
7#[allow(missing_docs)]
8pub enum Custom {
9    ValueInstantF64(f64),
11    ValueInstantBool(bool),
13    ValueInstantString(String),
15    DataUnit(()),
17    DataGroup(DataGroup),
19    Tuple((String, (bool, bool))),
21    ValueStruct {
23        a: f64,
24    },
25    EspBootButton(bool),
27    EspRelay(bool),
29    SaveToFilesystem(u64),
30    }
32
33#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
35pub struct StructInDataGroup {
36    pub struct_field1: bool,
38    pub struct_field2: f64,
40}
41
42#[allow(missing_docs)]
44#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, MsgKey)]
45pub enum DataGroup {
46    DataGroupF64(f64),
48    DataGroupStruct(StructInDataGroup),
50    DataGroupVectorBool(Vec<bool>),
51    DataGroupVectorTuple(Vec<(bool, String)>),
52}
53
54impl MsgDataBound for Custom {
61    fn define_time_to_live(&self) -> TimeToLiveValue {
62        TimeToLiveValue::Infinite
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::super::Message;
69    use super::*;
70
71    #[test]
72    fn test1() {
73        let _msg = Custom::ValueInstantF64(12.3456);
74    }
75
76    #[test]
82    fn test_key() {
83        let msg = Message::new_custom(Custom::DataUnit(()));
84        assert_eq!("Custom-DataUnit", msg.key);
85
86        let msg = Message::new_custom(Custom::ValueInstantF64(0.0));
87        assert_eq!("Custom-ValueInstantF64", msg.key);
88
89        let msg = Message::new_custom(Custom::DataGroup(DataGroup::DataGroupF64(0.0)));
90        assert_eq!("Custom-DataGroup-DataGroupF64", msg.key);
91
92        let msg = Message::new_custom(Custom::DataGroup(DataGroup::DataGroupStruct(
93            StructInDataGroup {
94                struct_field1: false,
95                struct_field2: 0.0,
96            },
97        )));
98        assert_eq!("Custom-DataGroup-DataGroupStruct", msg.key);
99    }
100}