a bare-bones limbo server in rust (mirror of https://github.com/xoogware/crawlspace)
1/*
2 * Copyright (c) 2024 Andrew Brower.
3 * This file is part of Crawlspace.
4 *
5 * Crawlspace is free software: you can redistribute it and/or
6 * modify it under the terms of the GNU Affero General Public
7 * License as published by the Free Software Foundation, either
8 * version 3 of the License, or (at your option) any later version.
9 *
10 * Crawlspace is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public
16 * License along with Crawlspace. If not, see
17 * <https://www.gnu.org/licenses/>.
18 */
19
20#[allow(unused_imports)]
21pub mod datatypes {
22 mod impls;
23 mod position;
24 mod slot;
25 mod string;
26 mod text_component;
27 mod variable;
28
29 pub use impls::*;
30 pub use position::*;
31 pub use slot::*;
32 pub use string::*;
33 pub use text_component::*;
34 pub use variable::*;
35}
36
37#[derive(Debug, Clone, Copy, PartialEq)]
38pub enum ConnectionState {
39 Handshake,
40 Play,
41 Status,
42 Login,
43}
44
45#[derive(thiserror::Error, Debug)]
46pub enum ErrorKind {
47 #[error("Not connected to a client")]
48 Disconnected,
49 #[error("IO error")]
50 Io(#[from] std::io::Error),
51 #[error("Invalid data: {0}")]
52 InvalidData(String),
53 #[error("Timed out")]
54 Timeout,
55}
56
57pub type Result<T> = std::result::Result<T, ErrorKind>;
58
59pub trait Read {
60 fn read(reader: &mut impl std::io::Read) -> Result<Self>
61 where
62 Self: Sized;
63}
64
65pub trait Write {
66 fn write(&self, writer: &mut impl std::io::Write) -> Result<()>;
67}
68
69#[derive(Clone, Debug)]
70pub enum PacketId {
71 String(&'static str),
72 Numeric(i32),
73}
74
75impl PartialEq<i32> for PacketId {
76 fn eq(&self, other: &i32) -> bool {
77 match self {
78 Self::Numeric(i) => i == other,
79 _ => false,
80 }
81 }
82}
83
84impl PartialEq<&'static str> for PacketId {
85 fn eq(&self, other: &&'static str) -> bool {
86 match self {
87 Self::String(s) => s == other,
88 _ => false,
89 }
90 }
91}
92
93pub trait Packet {
94 fn packet_id() -> PacketId
95 where
96 Self: Sized;
97 fn packet_state() -> ConnectionState
98 where
99 Self: Sized;
100}
101
102pub trait ServerboundPacket: Packet + Read {}
103impl<T> ServerboundPacket for T where T: Packet + Read {}
104
105pub trait ClientboundPacket: Packet + Write {}
106impl<T> ClientboundPacket for T where T: Packet + Write {}
107
108pub trait Protocol {
109 fn handshake_player(&mut self);
110 // fn login_player(&self);
111}