ppaass_protocol/message/
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
pub mod values;
use crate::error::ProtocolError;
use crate::message::values::address::UnifiedAddress;
use bytes::Bytes;
use derive_more::Constructor;
use serde_derive::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Encryption {
    Plain,
    Aes(Bytes),
}

impl TryFrom<Bytes> for Encryption {
    type Error = ProtocolError;
    fn try_from(value: Bytes) -> Result<Self, Self::Error> {
        let result = bincode::deserialize::<Encryption>(&value)?;
        Ok(result)
    }
}

impl TryFrom<Encryption> for Bytes {
    type Error = ProtocolError;
    fn try_from(value: Encryption) -> Result<Self, Self::Error> {
        let result = bincode::serialize(&value)?;
        Ok(result.into())
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub enum Payload {
    KeyExchange,
    Content {
        src_address: UnifiedAddress,
        dest_address: UnifiedAddress,
        data: Option<Bytes>,
    },
}

impl TryFrom<Bytes> for Payload {
    type Error = ProtocolError;
    fn try_from(value: Bytes) -> Result<Self, Self::Error> {
        let result = bincode::deserialize::<Payload>(&value)?;
        Ok(result)
    }
}

impl TryFrom<Payload> for Bytes {
    type Error = ProtocolError;
    fn try_from(value: Payload) -> Result<Self, Self::Error> {
        let result = bincode::serialize(&value)?;
        Ok(result.into())
    }
}

#[derive(Serialize, Deserialize, Debug, Constructor)]
pub struct Packet {
    packet_id: String,
    user_token: String,
    encryption: Encryption,
    payload: Bytes,
}

impl TryFrom<Bytes> for Packet {
    type Error = ProtocolError;
    fn try_from(value: Bytes) -> Result<Self, Self::Error> {
        let result = bincode::deserialize::<Packet>(&value)?;
        Ok(result)
    }
}

impl TryFrom<Packet> for Bytes {
    type Error = ProtocolError;
    fn try_from(value: Packet) -> Result<Self, Self::Error> {
        let result = bincode::serialize(&value)?;
        Ok(result.into())
    }
}