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
use std::fmt::Debug;

use tokio::sync::mpsc;

use rsiot_messages_core::message_v2::{Message, MsgDataBound, MsgSource};
use uuid::Uuid;

use crate::ComponentError;

#[derive(Clone, Debug)]
pub struct CmpOutput<TMsg> {
    channel: mpsc::Sender<Message<TMsg>>,
    msg_source: MsgSource,
}

impl<TMsg> CmpOutput<TMsg>
where
    TMsg: MsgDataBound,
{
    pub fn new(
        channel: mpsc::Sender<Message<TMsg>>,
        executor_name: &str,
        executor_id: Uuid,
    ) -> Self {
        let msg_source = MsgSource::new(executor_name, executor_id);
        Self {
            channel,
            msg_source,
        }
    }

    pub(crate) fn set_component_id(&mut self, component_name: &str, component_id: Uuid) {
        self.msg_source.set_component(component_name, component_id);
    }

    pub(crate) fn set_session_id(&mut self, session_name: &str, session_id: Uuid) {
        self.msg_source.set_session(session_name, session_id);
    }
    pub async fn send(&self, mut msg: Message<TMsg>) -> Result<(), ComponentError> {
        msg.cmp_set(&self.msg_source);
        self.channel
            .send(msg)
            .await
            .map_err(|e| ComponentError::CmpOutput(e.to_string()))
    }
}