tetratto/crates/core/src/model/socket.rs

54 lines
1.3 KiB
Rust
Raw Normal View History

2025-04-27 23:11:37 -04:00
use serde::{Serialize, Deserialize, de::DeserializeOwned};
2025-05-02 20:08:35 -04:00
#[derive(Serialize, Deserialize, PartialEq, Eq)]
pub enum CrudMessageType {
Create,
Delete,
}
#[derive(Serialize, Deserialize, PartialEq, Eq)]
pub enum PacketType {
/// A regular check to ensure the connection is still alive.
Ping,
/// General text which can be ignored.
Text,
2025-05-02 20:08:35 -04:00
/// A CRUD operation.
Crud(CrudMessageType),
/// A text key which identifies the socket.
Key,
}
2025-04-27 23:11:37 -04:00
#[derive(Serialize, Deserialize, PartialEq, Eq)]
pub enum SocketMethod {
/// Authentication and channel identification.
Headers,
/// A message was sent in the channel.
Message,
/// A message was deleted in the channel.
Delete,
/// Forward message from server to client. (Redis pubsub to ws)
Forward(PacketType),
/// A general packet from client to server. (ws to Redis pubsub)
Misc(PacketType),
2025-05-02 20:08:35 -04:00
/// A general packet from client to server. (ws to Redis pubsub)
Packet(PacketType),
2025-04-27 23:11:37 -04:00
}
#[derive(Serialize, Deserialize)]
pub struct SocketMessage {
pub method: SocketMethod,
pub data: String,
}
impl SocketMessage {
pub fn data<T: DeserializeOwned>(&self) -> T {
serde_json::from_str(&self.data).unwrap()
}
}
/// [`PacketType::Text`]
#[derive(Serialize, Deserialize, PartialEq, Eq)]
pub struct TextMessage {
pub text: String,
}