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
pub mod module_communication {
    tonic::include_proto!("module_communication");
}

pub mod client_communication {
    tonic::include_proto!("communication");
}

pub use rmp_serde::{decode, encode};

impl module_communication::MessagePack {
    pub fn decode<Target>(self) -> Result<Target, decode::Error>
    where
        Target: serde::de::DeserializeOwned,
    {
        Ok(rmp_serde::decode::from_slice(self.data.as_slice())?)
    }

    pub fn encode<T>(target: T) -> Result<Self, encode::Error>
    where
        T: serde::Serialize,
    {
        Ok(module_communication::MessagePack {
            data: rmp_serde::encode::to_vec(&target)?,
        })
    }
}

impl client_communication::MessagePack {
    pub fn decode<Target>(self) -> Result<Target, decode::Error>
    where
        Target: serde::de::DeserializeOwned,
    {
        Ok(rmp_serde::decode::from_slice(self.data.as_slice())?)
    }

    pub fn encode<T>(target: T) -> Result<client_communication::MessagePack, encode::Error>
    where
        T: serde::Serialize,
    {
        Ok(client_communication::MessagePack {
            data: rmp_serde::encode::to_vec(&target)?,
        })
    }
}