kojacoord_protocol/
codec.rs1use bytes::{Buf, BufMut, Bytes, BytesMut};
2
3use crate::error::ProtocolError;
4
5pub const MAX_PACKET_SIZE: usize = 1 << 25;
6
7pub const MAX_STRING_LENGTH: usize = 32767;
8
9pub trait Encode {
10 fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError>;
11}
12
13pub trait Decode: Sized {
14 fn decode(src: &mut Bytes) -> Result<Self, ProtocolError>;
15}
16
17pub trait PacketId {
18 fn packet_id(protocol_version: u32) -> u8;
19}
20
21impl Encode for bool {
22 fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
23 dst.put_u8(*self as u8);
24 Ok(())
25 }
26}
27
28impl Decode for bool {
29 fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
30 if src.is_empty() {
31 return Err(ProtocolError::UnexpectedEof);
32 }
33 Ok(src.get_u8() != 0)
34 }
35}
36
37impl Encode for u8 {
38 fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
39 dst.put_u8(*self);
40 Ok(())
41 }
42}
43
44impl Decode for u8 {
45 fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
46 if src.is_empty() {
47 return Err(ProtocolError::UnexpectedEof);
48 }
49 Ok(src.get_u8())
50 }
51}
52
53impl Encode for i8 {
54 fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
55 dst.put_i8(*self);
56 Ok(())
57 }
58}
59
60impl Decode for i8 {
61 fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
62 if src.is_empty() {
63 return Err(ProtocolError::UnexpectedEof);
64 }
65 Ok(src.get_i8())
66 }
67}
68
69impl Encode for i16 {
70 fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
71 dst.put_i16(*self);
72 Ok(())
73 }
74}
75
76impl Decode for i16 {
77 fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
78 if src.remaining() < 2 {
79 return Err(ProtocolError::UnexpectedEof);
80 }
81 Ok(src.get_i16())
82 }
83}
84
85impl Encode for u16 {
86 fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
87 dst.put_u16(*self);
88 Ok(())
89 }
90}
91
92impl Decode for u16 {
93 fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
94 if src.remaining() < 2 {
95 return Err(ProtocolError::UnexpectedEof);
96 }
97 Ok(src.get_u16())
98 }
99}
100
101impl Encode for i32 {
102 fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
103 dst.put_i32(*self);
104 Ok(())
105 }
106}
107
108impl Decode for i32 {
109 fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
110 if src.remaining() < 4 {
111 return Err(ProtocolError::UnexpectedEof);
112 }
113 Ok(src.get_i32())
114 }
115}
116
117impl Encode for u32 {
118 fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
119 dst.put_u32(*self);
120 Ok(())
121 }
122}
123
124impl Decode for u32 {
125 fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
126 if src.remaining() < 4 {
127 return Err(ProtocolError::UnexpectedEof);
128 }
129 Ok(src.get_u32())
130 }
131}
132
133impl Encode for i64 {
134 fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
135 dst.put_i64(*self);
136 Ok(())
137 }
138}
139
140impl Decode for i64 {
141 fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
142 if src.remaining() < 8 {
143 return Err(ProtocolError::UnexpectedEof);
144 }
145 Ok(src.get_i64())
146 }
147}
148
149impl Encode for u64 {
150 fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
151 dst.put_u64(*self);
152 Ok(())
153 }
154}
155
156impl Decode for u64 {
157 fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
158 if src.remaining() < 8 {
159 return Err(ProtocolError::UnexpectedEof);
160 }
161 Ok(src.get_u64())
162 }
163}
164
165impl Encode for f32 {
166 fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
167 dst.put_f32(*self);
168 Ok(())
169 }
170}
171
172impl Decode for f32 {
173 fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
174 if src.remaining() < 4 {
175 return Err(ProtocolError::UnexpectedEof);
176 }
177 Ok(src.get_f32())
178 }
179}
180
181impl Encode for f64 {
182 fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
183 dst.put_f64(*self);
184 Ok(())
185 }
186}
187
188impl Decode for f64 {
189 fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
190 if src.remaining() < 8 {
191 return Err(ProtocolError::UnexpectedEof);
192 }
193 Ok(src.get_f64())
194 }
195}
196
197impl Encode for String {
198 fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
199 let bytes = self.as_bytes();
200 if bytes.len() > MAX_STRING_LENGTH * 3 {
201 return Err(ProtocolError::StringTooLong(
202 bytes.len(),
203 MAX_STRING_LENGTH * 3,
204 ));
205 }
206 crate::types::VarInt(bytes.len() as i32).encode(dst)?;
207 dst.put_slice(bytes);
208 Ok(())
209 }
210}
211
212impl Decode for String {
213 fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
214 let len = crate::types::VarInt::decode(src)?.0 as usize;
215 if len > MAX_STRING_LENGTH * 3 {
216 return Err(ProtocolError::StringTooLong(len, MAX_STRING_LENGTH * 3));
217 }
218 if src.remaining() < len {
219 return Err(ProtocolError::UnexpectedEof);
220 }
221 let bytes = src.copy_to_bytes(len);
222 Ok(String::from_utf8(bytes.to_vec())?)
223 }
224}
225
226pub fn encode_byte_array(data: &[u8], dst: &mut BytesMut) -> Result<(), ProtocolError> {
227 crate::types::VarInt(data.len() as i32).encode(dst)?;
228 dst.put_slice(data);
229 Ok(())
230}
231
232pub fn decode_byte_array(src: &mut Bytes) -> Result<Vec<u8>, ProtocolError> {
233 let len = crate::types::VarInt::decode(src)?.0 as usize;
234 if src.remaining() < len {
235 return Err(ProtocolError::UnexpectedEof);
236 }
237 Ok(src.copy_to_bytes(len).to_vec())
238}
239
240impl Encode for uuid::Uuid {
241 fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
242 dst.put_slice(self.as_bytes());
243 Ok(())
244 }
245}
246
247impl Decode for uuid::Uuid {
248 fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
249 if src.remaining() < 16 {
250 return Err(ProtocolError::UnexpectedEof);
251 }
252 let mut buf = [0u8; 16];
253 src.copy_to_slice(&mut buf);
254 Ok(uuid::Uuid::from_bytes(buf))
255 }
256}
257
258impl<T: Encode> Encode for Vec<T> {
259 fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
260 crate::types::VarInt(self.len() as i32).encode(dst)?;
261 for item in self {
262 item.encode(dst)?;
263 }
264 Ok(())
265 }
266}
267
268impl<T: Decode> Decode for Vec<T> {
269 fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
270 let len = crate::types::VarInt::decode(src)?.0 as usize;
271 let mut out = Vec::with_capacity(len.min(4096));
272 for _ in 0..len {
273 out.push(T::decode(src)?);
274 }
275 Ok(out)
276 }
277}
278
279impl<T: Encode> Encode for Option<T> {
280 fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
281 match self {
282 Some(v) => {
283 true.encode(dst)?;
284 v.encode(dst)
285 },
286 None => false.encode(dst),
287 }
288 }
289}
290
291impl<T: Decode> Decode for Option<T> {
292 fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
293 let present = bool::decode(src)?;
294 if present {
295 Ok(Some(T::decode(src)?))
296 } else {
297 Ok(None)
298 }
299 }
300}