ppaass_protocol_v2/
lib.rs

1mod business_obj;
2pub use business_obj::*;
3use bytes::Bytes;
4use derive_more::{Constructor, Display};
5use serde::{Deserialize, Serialize};
6use std::net::SocketAddr;
7#[derive(Debug, Serialize, Deserialize, Display, Clone)]
8pub enum PpaassProtocolUnifyAddress {
9    #[display("{:?}", _0)]
10    Socket(SocketAddr),
11    #[display("{}:{}", _0, _1)]
12    Domain(String, u16),
13}
14
15impl From<SocketAddr> for PpaassProtocolUnifyAddress {
16    fn from(value: SocketAddr) -> Self {
17        Self::Socket(value)
18    }
19}
20
21impl From<(String, u16)> for PpaassProtocolUnifyAddress {
22    fn from(value: (String, u16)) -> Self {
23        Self::Domain(value.0, value.1)
24    }
25}
26
27#[derive(Debug, Serialize, Deserialize, Display)]
28pub enum PpaassProtocolPayloadEncryption {
29    #[display("{:?}", _0)]
30    Aes(Bytes),
31}
32
33#[derive(Debug, Serialize, Deserialize, Constructor)]
34pub struct PpaassProtocolPacket {
35    magic_flag: [u8; 8],
36    payload_length: usize,
37    payload: Bytes,
38}
39
40impl PpaassProtocolPacket {
41    pub fn magic_flag(&self) -> &[u8] {
42        &self.magic_flag
43    }
44
45    pub fn payload_length(&self) -> usize {
46        self.payload_length
47    }
48
49    pub fn payload(&self) -> &[u8] {
50        &self.payload
51    }
52}
53
54#[derive(Debug, Serialize, Deserialize, Display, Constructor)]
55#[display(
56    r#"{{
57    user_token: {user_token},
58    encryption: {encryption},
59    business_obj: {business_obj:?},
60    }}"#
61)]
62pub struct PpaassProtocolPayload {
63    user_token: String,
64    encryption: PpaassProtocolPayloadEncryption,
65    business_obj: Bytes,
66}
67
68impl PpaassProtocolPayload {
69    pub fn user_token(&self) -> &str {
70        &self.user_token
71    }
72
73    pub fn encryption(&self) -> &PpaassProtocolPayloadEncryption {
74        &self.encryption
75    }
76
77    pub fn business_obj(&self) -> &[u8] {
78        &self.business_obj
79    }
80}