minechat_protocol/
protocol.rs

1use log::error;
2use miette::Diagnostic;
3use serde::{Deserialize, Serialize};
4use thiserror::Error;
5use tokio::io;
6
7/// Errors that can occur during the operation of the MineChat protocol.
8#[derive(Debug, Error, Diagnostic)]
9pub enum MineChatError {
10    /// I/O error. Contains the underlying error.
11    #[error("I/O error: {0}")]
12    #[diagnostic(code(minechat::io))]
13    Io(#[from] io::Error),
14
15    /// Serde error. Contains the underlying JSON error.
16    #[error("Serde error: {0}")]
17    #[diagnostic(code(minechat::serde))]
18    Serde(#[from] serde_json::Error),
19
20    /// Server not linked.
21    #[error("Server not linked")]
22    ServerNotLinked,
23
24    /// Configuration error.
25    #[error("Config error: {0}")]
26    #[diagnostic(code(minechat::config_error), help = "Check your configuration file")]
27    ConfigError(String),
28
29    /// Authentication failed.
30    #[error("Authentication failed: {0}")]
31    #[diagnostic(
32        code(minechat::auth_failed),
33        help = "Try logging in again with valid credentials"
34    )]
35    AuthFailed(String),
36
37    /// UUID error.
38    #[error("UUID error: {0}")]
39    #[diagnostic(code(minechat::uuid))]
40    Uuid(#[from] uuid::Error),
41
42    /// Disconnected.
43    #[error("Disconnected")]
44    #[diagnostic(
45        code(minechat::disconnected),
46        help = "If this is unexpected, try reconnecting"
47    )]
48    Disconnected,
49}
50
51/// The different types of messages that can be sent and received in the MineChat protocol.
52#[derive(Debug, Serialize, Deserialize)]
53#[serde(tag = "type")]
54pub enum MineChatMessage {
55    /// An authentication message, containing the client's UUID and link code.
56    #[serde(rename = "AUTH")]
57    Auth { payload: AuthPayload },
58
59    /// An acknowledgment of a successful authentication, containing the server's response.
60    #[serde(rename = "AUTH_ACK")]
61    AuthAck { payload: AuthAckPayload },
62
63    /// A chat message, containing the message text.
64    #[serde(rename = "CHAT")]
65    Chat { payload: ChatPayload },
66
67    /// A broadcast message, containing the message text and the sender's name.
68    #[serde(rename = "BROADCAST")]
69    Broadcast { payload: BroadcastPayload },
70
71    /// A disconnect message, containing the reason for the disconnection.
72    #[serde(rename = "DISCONNECT")]
73    Disconnect { payload: DisconnectPayload },
74}
75
76/// The payload for an authentication message.
77#[derive(Debug, Serialize, Deserialize)]
78pub struct AuthPayload {
79    /// The client's UUID.
80    pub client_uuid: String,
81    /// The link code used to authenticate with the server.
82    pub link_code: String,
83}
84
85/// The payload for an authentication acknowledgment message.
86#[derive(Debug, Serialize, Deserialize)]
87pub struct AuthAckPayload {
88    /// The status of the authentication (either "success" or "failure").
89    pub status: String,
90    /// A message describing the authentication status.
91    pub message: String,
92    /// The client's Minecraft UUID, if available.
93    pub minecraft_uuid: Option<String>,
94    /// The client's Minecraft username, if available.
95    pub username: Option<String>,
96}
97
98/// The payload for a chat message.
99#[derive(Debug, Serialize, Deserialize)]
100pub struct ChatPayload {
101    /// The text of the chat message.
102    pub message: String,
103}
104
105/// The payload for a broadcast message.
106#[derive(Debug, Serialize, Deserialize)]
107pub struct BroadcastPayload {
108    /// The name of the sender.
109    pub from: String,
110    /// The text of the broadcast message.
111    pub message: String,
112}
113
114/// The payload for a disconnect message.
115#[derive(Debug, Serialize, Deserialize)]
116pub struct DisconnectPayload {
117    /// The reason for the disconnection.
118    pub reason: String,
119}