ppaass_protocol/message/
mod.rs

1pub mod values;
2use crate::error::ProtocolError;
3use crate::message::values::address::UnifiedAddress;
4use bytes::Bytes;
5use derive_more::Constructor;
6use serde_derive::{Deserialize, Serialize};
7/// The encryption of the packet payload
8#[derive(Serialize, Deserialize, Debug, Clone)]
9pub enum PpaassPacketPayloadEncryption {
10    /// The packet payload is not encrypted
11    Plain,
12    /// The packet payload is encrypted with AES,
13    /// the content of the variant is the encryption
14    /// used for AES.
15    Aes(Bytes),
16}
17
18impl TryFrom<Bytes> for PpaassPacketPayloadEncryption {
19    type Error = ProtocolError;
20    fn try_from(value: Bytes) -> Result<Self, Self::Error> {
21        let result = bincode::deserialize::<PpaassPacketPayloadEncryption>(&value)?;
22        Ok(result)
23    }
24}
25
26impl TryFrom<PpaassPacketPayloadEncryption> for Bytes {
27    type Error = ProtocolError;
28    fn try_from(value: PpaassPacketPayloadEncryption) -> Result<Self, Self::Error> {
29        let result = bincode::serialize(&value)?;
30        Ok(result.into())
31    }
32}
33
34/// The payload of the packet
35#[derive(Serialize, Deserialize, Debug)]
36pub enum PpaassPacketPayload {
37    /// The payload is used for encryption key exchange,
38    /// the content of the encryption is the encryption key,
39    /// the encryption key is encrypted with RSA
40    KeyExchange,
41    /// The payload is used for transfer data.
42    Content {
43        /// The source address of the payload
44        src_address: UnifiedAddress,
45        /// The destination address of the payload
46        dest_address: UnifiedAddress,
47        /// The content of the payload
48        data: Bytes,
49    },
50}
51
52impl TryFrom<Bytes> for PpaassPacketPayload {
53    type Error = ProtocolError;
54    fn try_from(value: Bytes) -> Result<Self, Self::Error> {
55        let result = bincode::deserialize::<PpaassPacketPayload>(&value)?;
56        Ok(result)
57    }
58}
59
60impl TryFrom<PpaassPacketPayload> for Bytes {
61    type Error = ProtocolError;
62    fn try_from(value: PpaassPacketPayload) -> Result<Self, Self::Error> {
63        let result = bincode::serialize(&value)?;
64        Ok(result.into())
65    }
66}
67
68#[derive(Serialize, Deserialize, Debug, Constructor)]
69pub struct PpaassPacket {
70    packet_id: String,
71    user_token: String,
72    encryption: PpaassPacketPayloadEncryption,
73    payload: Bytes,
74}
75impl PpaassPacket {
76    pub fn packet_id(&self) -> &str {
77        &self.packet_id
78    }
79
80    pub fn user_token(&self) -> &str {
81        &self.user_token
82    }
83
84    pub fn encryption(&self) -> &PpaassPacketPayloadEncryption {
85        &self.encryption
86    }
87
88    pub fn payload(&self) -> &[u8] {
89        &self.payload
90    }
91}
92
93impl TryFrom<Bytes> for PpaassPacket {
94    type Error = ProtocolError;
95    fn try_from(value: Bytes) -> Result<Self, Self::Error> {
96        let result = bincode::deserialize::<PpaassPacket>(&value)?;
97        Ok(result)
98    }
99}
100
101impl TryFrom<PpaassPacket> for Bytes {
102    type Error = ProtocolError;
103    fn try_from(value: PpaassPacket) -> Result<Self, Self::Error> {
104        let result = bincode::serialize(&value)?;
105        Ok(result.into())
106    }
107}