use std::fmt::Display; use axum::body::to_bytes; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Message { pub from: String, pub message_kind: MessageKind, pub content: String, } #[derive(Debug, Clone, Serialize, Deserialize)] pub enum MessageKind { Test, Move, CreatePlayer, CreateLobby, } impl Message { pub fn new(from: &str, message_kind: MessageKind, content: impl Into) -> anyhow::Result { let from = from.into(); let content = content.into(); Ok(Self { from, message_kind, content, }) } pub fn encode(&self) -> anyhow::Result { let message_json: String = serde_json::to_string(self)?; Ok(message_json) } pub fn decode(buffer: &[u8]) -> anyhow::Result { let message: Message = serde_json::from_slice(buffer)?; Ok(message) } } impl Display for Message { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { todo!() } }