rsiot/components_config/http_server/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! Конфигурация HTTP-сервера
//!
//! Тестирование:
//!
//! ```bash
//! cargo test -p rsiot-components-config --doc http_server
//! ```

use crate::message::*;

/// Конфигурация компонента http-server
#[derive(Clone, Debug)]
pub struct Config<TMsg>
where
    TMsg: MsgDataBound,
{
    pub this_service: TMsg::TService,
    pub client_service: TMsg::TService,

    /// Порт, через который доступен сервер
    pub port: u16,

    /// Функция преобразования сообщений в текст
    ///
    /// # Примеры
    ///
    /// ## Заглушка
    ///
    /// ```rust
    /// # use rsiot_components_config::http_server as cmp_http_server;
    /// # use rsiot_messages_core::{example_message::*, *};
    /// # // insert from tests::stub
    /// # cmp_http_server::Config::<ExampleMessage> {
    /// #     port: 8000,
    /// fn_input: |_| Ok(None),
    /// #     fn_output: |_| Ok(None),
    /// # };
    /// ```
    ///
    /// ## Сериализация в json
    ///
    /// ```rust
    /// # use rsiot_components_config::http_server as cmp_http_server;
    /// # use rsiot_messages_core::{example_message::*, *};
    /// # // insert from tests::fn_input_json
    /// # cmp_http_server::Config::<ExampleMessage> {
    /// #     port: 8000,
    /// fn_input: |msg: &Message<ExampleMessage>| {
    ///     let text = msg.serialize()?;
    ///     Ok(Some(text))
    /// },
    /// #    fn_output: |_| Ok(None),
    /// # };
    /// ```
    pub fn_input: fn(&Message<TMsg>) -> anyhow::Result<Option<String>>,

    /// Данные из компонента `cmp_plc`
    pub cmp_plc: fn(&Message<TMsg>) -> ConfigCmpPlcData,

    /// Функция преобразования текста в сообщения
    ///
    /// # Примеры
    ///
    /// ## Заглушка
    ///
    /// ```rust
    /// # use rsiot_components_config::http_server as cmp_http_server;
    /// # use rsiot_messages_core::{example_message::*, *};
    /// # // insert from tests::stub
    /// # cmp_http_server::Config::<ExampleMessage> {
    /// #     port: 8000,
    /// #     fn_input: |_| Ok(None),
    /// fn_output: |_| Ok(None),
    /// # };
    /// ```
    ///
    /// ## Десериализация из json
    ///
    /// ```rust
    /// # use rsiot_components_config::http_server as cmp_http_server;
    /// # use rsiot_messages_core::{example_message::*, *};
    /// # // insert from tests::fn_input_json
    /// # cmp_http_server::Config::<ExampleMessage> {
    /// #     port: 8000,
    /// #     fn_input: |_| Ok(None),
    /// fn_output: |text: &str| {
    ///     let msg = Message::deserialize(text)?;
    ///     Ok(Some(msg))
    /// },
    /// # };
    /// ```
    pub fn_output: fn(&str) -> anyhow::Result<Option<Message<TMsg>>>,
}

/// Данные, получаемые из компонента `cmp_plc`
pub enum ConfigCmpPlcData {
    /// Данные не относятся к компоненту cmp_plc
    NoData,
    /// Состояние области `input`
    Input(String),
    /// Состояние области `output`
    Output(String),
    /// Состояние области `static`
    Static(String),
}

#[cfg(test)]
mod tests {
    use super::{Config, ConfigCmpPlcData};
    use crate::message::{example_message::*, example_service::Service, *};

    #[allow(clippy::no_effect)]
    #[test]
    fn stub() {
        Config::<Custom> {
            this_service: Service::example_service,
            client_service: Service::example_service,
            port: 8000,
            fn_input: |_| Ok(None),
            fn_output: |_| Ok(None),
            cmp_plc: |_| ConfigCmpPlcData::NoData,
        };
    }

    #[allow(clippy::no_effect)]
    #[test]
    fn fn_input_json() {
        Config::<Custom> {
            this_service: Service::example_service,
            client_service: Service::example_service,
            port: 8000,
            fn_input: |msg: &Message<Custom>| {
                let text = msg.serialize()?;
                Ok(Some(text))
            },
            fn_output: |_| Ok(None),
            cmp_plc: |_| ConfigCmpPlcData::NoData,
        };
    }

    #[allow(clippy::no_effect)]
    #[test]
    fn fn_output_json() {
        Config::<Custom> {
            this_service: Service::example_service,
            client_service: Service::example_service,
            port: 8000,
            fn_input: |_| Ok(None),
            fn_output: |text: &str| {
                let msg = Message::deserialize(text)?;
                Ok(Some(msg))
            },
            cmp_plc: |_| ConfigCmpPlcData::NoData,
        };
    }
}