minechat_protocol/
protocol.rs1use log::error;
2use miette::Diagnostic;
3use serde::{Deserialize, Serialize};
4use thiserror::Error;
5use tokio::io;
6
7#[derive(Debug, Error, Diagnostic)]
9pub enum MineChatError {
10 #[error("I/O error: {0}")]
12 #[diagnostic(code(minechat::io))]
13 Io(#[from] io::Error),
14
15 #[error("Serde error: {0}")]
17 #[diagnostic(code(minechat::serde))]
18 Serde(#[from] serde_json::Error),
19
20 #[error("Server not linked")]
22 ServerNotLinked,
23
24 #[error("Config error: {0}")]
26 #[diagnostic(code(minechat::config_error), help = "Check your configuration file")]
27 ConfigError(String),
28
29 #[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 #[error("UUID error: {0}")]
39 #[diagnostic(code(minechat::uuid))]
40 Uuid(#[from] uuid::Error),
41
42 #[error("Disconnected")]
44 #[diagnostic(
45 code(minechat::disconnected),
46 help = "If this is unexpected, try reconnecting"
47 )]
48 Disconnected,
49}
50
51#[derive(Debug, Serialize, Deserialize)]
53#[serde(tag = "type")]
54pub enum MineChatMessage {
55 #[serde(rename = "AUTH")]
57 Auth { payload: AuthPayload },
58
59 #[serde(rename = "AUTH_ACK")]
61 AuthAck { payload: AuthAckPayload },
62
63 #[serde(rename = "CHAT")]
65 Chat { payload: ChatPayload },
66
67 #[serde(rename = "BROADCAST")]
69 Broadcast { payload: BroadcastPayload },
70
71 #[serde(rename = "DISCONNECT")]
73 Disconnect { payload: DisconnectPayload },
74}
75
76#[derive(Debug, Serialize, Deserialize)]
78pub struct AuthPayload {
79 pub client_uuid: String,
81 pub link_code: String,
83}
84
85#[derive(Debug, Serialize, Deserialize)]
87pub struct AuthAckPayload {
88 pub status: String,
90 pub message: String,
92 pub minecraft_uuid: Option<String>,
94 pub username: Option<String>,
96}
97
98#[derive(Debug, Serialize, Deserialize)]
100pub struct ChatPayload {
101 pub message: String,
103}
104
105#[derive(Debug, Serialize, Deserialize)]
107pub struct BroadcastPayload {
108 pub from: String,
110 pub message: String,
112}
113
114#[derive(Debug, Serialize, Deserialize)]
116pub struct DisconnectPayload {
117 pub reason: String,
119}