wsio_core/packet/
mod.rs

1use serde::{
2    Deserialize,
3    Serialize,
4};
5use serde_repr::{
6    Deserialize_repr,
7    Serialize_repr,
8};
9
10pub mod codecs;
11
12// Enums
13#[repr(u8)]
14#[derive(Clone, Debug, Deserialize_repr, Serialize_repr)]
15pub enum WsIoPacketType {
16    Disconnect = 0,
17    Event = 1,
18    Init = 2,
19    Ready = 3,
20}
21
22// Structs
23#[derive(Deserialize)]
24struct InnerPacket(WsIoPacketType, Option<String>, Option<Vec<u8>>);
25
26#[derive(Serialize)]
27struct InnerPacketRef<'a>(&'a WsIoPacketType, &'a Option<String>, &'a Option<Vec<u8>>);
28
29#[derive(Clone, Debug)]
30pub struct WsIoPacket {
31    pub data: Option<Vec<u8>>,
32    pub key: Option<String>,
33    pub r#type: WsIoPacketType,
34}
35
36impl WsIoPacket {
37    #[inline]
38    pub fn new(r#type: WsIoPacketType, key: Option<&str>, data: Option<Vec<u8>>) -> Self {
39        Self {
40            data,
41            key: key.map(|k| k.into()),
42            r#type,
43        }
44    }
45
46    // Protected methods
47    #[inline]
48    pub(self) fn from_inner(inner: InnerPacket) -> Self {
49        Self {
50            data: inner.2,
51            key: inner.1,
52            r#type: inner.0,
53        }
54    }
55
56    #[inline]
57    pub(self) fn to_inner_ref(&self) -> InnerPacketRef<'_> {
58        InnerPacketRef(&self.r#type, &self.key, &self.data)
59    }
60
61    // Public methods
62    #[inline]
63    pub fn new_disconnect() -> Self {
64        Self::new(WsIoPacketType::Disconnect, None, None)
65    }
66
67    #[inline]
68    pub fn new_event(event: &str, data: Option<Vec<u8>>) -> Self {
69        Self::new(WsIoPacketType::Event, Some(event), data)
70    }
71
72    #[inline]
73    pub fn new_init(data: Option<Vec<u8>>) -> Self {
74        Self::new(WsIoPacketType::Init, None, data)
75    }
76
77    #[inline]
78    pub fn new_ready() -> Self {
79        Self::new(WsIoPacketType::Ready, None, None)
80    }
81}