tetratto_core/model/
socket.rs

1use serde::{Serialize, Deserialize, de::DeserializeOwned};
2
3#[derive(Serialize, Deserialize, PartialEq, Eq)]
4pub enum CrudMessageType {
5    Create,
6    Delete,
7}
8
9#[derive(Serialize, Deserialize, PartialEq, Eq)]
10pub enum PacketType {
11    /// A regular check to ensure the connection is still alive.
12    Ping,
13    /// General text which can be ignored.
14    Text,
15    /// A CRUD operation.
16    Crud(CrudMessageType),
17    /// A text key which identifies the socket.
18    Key,
19}
20
21#[derive(Serialize, Deserialize, PartialEq, Eq)]
22pub enum SocketMethod {
23    /// Authentication and channel identification.
24    Headers,
25    /// A message was sent in the channel.
26    Message,
27    /// A message was deleted in the channel.
28    Delete,
29    /// Forward message from server to client. (Redis pubsub to ws)
30    Forward(PacketType),
31    /// A general packet from client to server. (ws to Redis pubsub)
32    Misc(PacketType),
33    /// A general packet from client to server. (ws to Redis pubsub)
34    Packet(PacketType),
35}
36
37#[derive(Serialize, Deserialize)]
38pub struct SocketMessage {
39    pub method: SocketMethod,
40    pub data: String,
41}
42
43impl SocketMessage {
44    pub fn data<T: DeserializeOwned>(&self) -> T {
45        serde_json::from_str(&self.data).unwrap()
46    }
47}
48
49/// [`PacketType::Text`]
50#[derive(Serialize, Deserialize, PartialEq, Eq)]
51pub struct TextMessage {
52    pub text: String,
53}