Skip to main content

rsiot/message/
example_message.rs

1//! Пример реализации сообщения. Можно использовать для тестирования компонентов
2
3use super::{Deserialize, MsgDataBound, MsgKey, Serialize};
4
5/// Пример реализации сообщения. Можно использовать для тестирования компонентов
6#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, MsgKey)]
7#[allow(missing_docs)]
8pub enum Custom {
9    /// Мгновенное значение типа f64
10    ValueInstantF64(f64),
11    /// Мгновенное значение типа bool
12    ValueInstantBool(bool),
13    /// Мгновенное значение типа String
14    ValueInstantString(String),
15    /// Значение типа unit
16    DataUnit(()),
17    /// Вложенная группа
18    DataGroup(DataGroup),
19    /// Вложенный кортеж
20    Tuple((String, (bool, bool))),
21    /// ValueStruct
22    ValueStruct {
23        a: f64,
24    },
25    /// ESP - кнопка BOOT
26    EspBootButton(bool),
27    /// ESP - выход на реле
28    EspRelay(bool),
29    SaveToFilesystem(u64),
30}
31
32/// Пример структуры
33#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
34pub struct StructInDataGroup {
35    /// Поле 1
36    pub struct_field1: bool,
37    /// Поле 2
38    pub struct_field2: f64,
39}
40
41/// Вложенная группа
42#[allow(missing_docs)]
43#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, MsgKey)]
44pub enum DataGroup {
45    /// Значение типа f64 в структуре
46    DataGroupF64(f64),
47    /// Вложенная в группу структура
48    DataGroupStruct(StructInDataGroup),
49    DataGroupVectorBool(Vec<bool>),
50    DataGroupVectorTuple(Vec<(bool, String)>),
51}
52
53impl MsgDataBound for Custom {}
54
55#[cfg(test)]
56mod tests {
57    use super::super::Message;
58    use super::*;
59
60    #[test]
61    fn test1() {
62        let _msg = Custom::ValueInstantF64(12.3456);
63    }
64
65    /// Запуск:
66    ///
67    /// ```bash
68    /// cargo test --target="x86_64-unknown-linux-gnu" -- message::example_message::tests
69    /// ```
70    #[test]
71    fn test_key() {
72        let msg = Message::new_custom(Custom::DataUnit(()));
73        assert_eq!("Custom-DataUnit", msg.key);
74
75        let msg = Message::new_custom(Custom::ValueInstantF64(0.0));
76        assert_eq!("Custom-ValueInstantF64", msg.key);
77
78        let msg = Message::new_custom(Custom::DataGroup(DataGroup::DataGroupF64(0.0)));
79        assert_eq!("Custom-DataGroup-DataGroupF64", msg.key);
80
81        let msg = Message::new_custom(Custom::DataGroup(DataGroup::DataGroupStruct(
82            StructInDataGroup {
83                struct_field1: false,
84                struct_field2: 0.0,
85            },
86        )));
87        assert_eq!("Custom-DataGroup-DataGroupStruct", msg.key);
88    }
89}