aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoey Sacchini <joey@sacchini.net>2020-12-07 17:57:14 -0500
committerJoey Sacchini <joey@sacchini.net>2020-12-07 17:57:14 -0500
commit2ce7b7b52b3ec2dd8a4b7aa08c9b341f31fbf745 (patch)
tree6df624815efe9f78a9814d22e5e578ae3d3fe6ef
parent171a93a33c7cc282a1726851eebb69ad4fffe5fc (diff)
downloadmcproto-rs-2ce7b7b52b3ec2dd8a4b7aa08c9b341f31fbf745.tar.gz
mcproto-rs-2ce7b7b52b3ec2dd8a4b7aa08c9b341f31fbf745.tar.bz2
mcproto-rs-2ce7b7b52b3ec2dd8a4b7aa08c9b341f31fbf745.zip
packet kind enum
-rw-r--r--Cargo.lock36
-rw-r--r--src/protocol.rs105
-rw-r--r--src/v1_15_2.rs2
-rw-r--r--src/v1_16_3.rs2
4 files changed, 102 insertions, 43 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 992e4cd..0f2769c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -25,21 +25,27 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
name = "crc32fast"
-version = "1.2.0"
+version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1"
+checksum = "81156fece84ab6a9f2afdb109ce3ae577e42b1228441eded99bd77f627953b1a"
dependencies = [
- "cfg-if",
+ "cfg-if 1.0.0",
]
[[package]]
name = "flate2"
-version = "1.0.18"
+version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "da80be589a72651dcda34d8b35bcdc9b7254ad06325611074d9cc0fbb19f60ee"
+checksum = "7411863d55df97a419aa64cb4d2f167103ea9d767e2c54a1868b7ac3f6b47129"
dependencies = [
- "cfg-if",
+ "cfg-if 1.0.0",
"crc32fast",
"libc",
"miniz_oxide",
@@ -51,7 +57,7 @@ version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6"
dependencies = [
- "cfg-if",
+ "cfg-if 0.1.10",
"libc",
"wasi",
]
@@ -64,9 +70,9 @@ checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
[[package]]
name = "libc"
-version = "0.2.79"
+version = "0.2.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2448f6066e80e3bfc792e9c98bf705b4b0fc6e8ef5b43e5889aff0eaa9c58743"
+checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614"
[[package]]
name = "mcproto-rs"
@@ -91,9 +97,9 @@ dependencies = [
[[package]]
name = "ppv-lite86"
-version = "0.2.9"
+version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c36fa947111f5c62a733b652544dd0016a43ce89619538a8ef92724a6f501a20"
+checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]]
name = "proc-macro2"
@@ -182,9 +188,9 @@ dependencies = [
[[package]]
name = "serde_json"
-version = "1.0.59"
+version = "1.0.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95"
+checksum = "1500e84d27fe482ed1dc791a56eddc2f230046a040fa908c08bda1d9fb615779"
dependencies = [
"itoa",
"ryu",
@@ -193,9 +199,9 @@ dependencies = [
[[package]]
name = "syn"
-version = "1.0.45"
+version = "1.0.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ea9c5432ff16d6152371f808fb5a871cd67368171b09bb21b43df8e4a47a3556"
+checksum = "8833e20724c24de12bbaba5ad230ea61c3eafb05b881c7c9d3cfe8638b187e68"
dependencies = [
"proc-macro2",
"quote",
diff --git a/src/protocol.rs b/src/protocol.rs
index dbdbc6b..8f155fc 100644
--- a/src/protocol.rs
+++ b/src/protocol.rs
@@ -87,6 +87,15 @@ pub struct ProtocolPacketField {
pub kind: String,
}
+pub trait HasPacketKind {
+ type Kind: PacketKind;
+
+ fn kind(&self) -> Self::Kind;
+}
+
+pub trait PacketKind: HasPacketId + Clone + Copy + PartialEq + Eq {
+}
+
pub trait HasPacketId {
fn id(&self) -> Id;
@@ -237,7 +246,7 @@ macro_rules! proto_struct {
#[macro_export]
macro_rules! define_protocol {
- ($version: literal, $packett: ident, $rawpackett: ident, $rawdt: ident => {
+ ($version: literal, $packett: ident, $rawpackett: ident, $rawdt: ident, $kindt: ident => {
$($nam: ident, $id: literal, $state: ident, $direction: ident => $body: ident {
$($fnam: ident: $ftyp: ty),* }),*
}
@@ -256,19 +265,30 @@ macro_rules! define_protocol {
}
}
+ $crate::as_item! {
+ #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
+ pub enum $kindt {
+ $($nam),*,
+ }
+ }
+
+ impl crate::protocol::HasPacketKind for $packett {
+ type Kind = $kindt;
+
+ fn kind(&self) -> Self::Kind {
+ match self {
+ $($packett::$nam(_) => $kindt::$nam),*,
+ }
+ }
+ }
+
impl crate::protocol::HasPacketId for $packett {
fn version() -> crate::types::VarInt {
crate::types::VarInt($version)
}
fn id(&self) -> crate::protocol::Id {
- use self::$packett::*;
- use crate::protocol::State::*;
- use crate::protocol::PacketDirection::*;
-
- match self {
- $($nam(_) => ($id, $state, $direction)),*
- }.into()
+ crate::protocol::HasPacketKind::kind(self).id()
}
}
@@ -306,15 +326,19 @@ macro_rules! define_protocol {
}
}
- impl<'a> crate::protocol::HasPacketId for $rawpackett<'a> {
- fn id(&self) -> crate::protocol::Id {
- use self::$rawpackett::*;
- use crate::protocol::State::*;
- use crate::protocol::PacketDirection::*;
+ impl<'a> crate::protocol::HasPacketKind for $rawpackett<'a> {
+ type Kind = $kindt;
+ fn kind(&self) -> Self::Kind {
match self {
- $($nam(_) => ($id, $state, $direction)),*
- }.into()
+ $($rawpackett::$nam(_) => $kindt::$nam),*,
+ }
+ }
+ }
+
+ impl<'a> crate::protocol::HasPacketId for $rawpackett<'a> {
+ fn id(&self) -> crate::protocol::Id {
+ crate::protocol::HasPacketKind::kind(self).id()
}
fn version() -> crate::types::VarInt {
@@ -327,17 +351,10 @@ macro_rules! define_protocol {
type Packet = $packett;
fn create(id: crate::protocol::Id, data: &'a[u8]) -> Result<Self, crate::protocol::PacketErr> {
- use self::$rawpackett::*;
- use crate::protocol::State::*;
- use crate::protocol::PacketDirection::*;
- use crate::protocol::PacketErr::UnknownId;
-
- match (id.id, id.state, id.direction) {
- $(($id, $state, $direction) => Ok($nam($rawdt{
- data,
- _typ: core::marker::PhantomData,
- }))),*,
- other => Err(UnknownId(other.into()))
+ if let Some(kind) = $kindt::from_id(id) {
+ Ok(kind.with_body_data(data))
+ } else {
+ Err(crate::protocol::PacketErr::UnknownId(id))
}
}
@@ -386,6 +403,42 @@ macro_rules! define_protocol {
}
}
+ impl crate::protocol::HasPacketId for $kindt {
+ fn id(&self) -> crate::protocol::Id {
+ use self::$kindt::*;
+ use crate::protocol::State::*;
+ use crate::protocol::PacketDirection::*;
+
+ match self {
+ $($nam => ($id, $state, $direction)),*
+ }.into()
+ }
+
+ fn version() -> crate::types::VarInt {
+ crate::types::VarInt($version)
+ }
+ }
+
+ impl $kindt {
+ pub fn from_id(id: crate::protocol::Id) -> Option<Self> {
+ match (id.id, id.state, id.direction) {
+ $(($id, crate::protocol::State::$state, crate::protocol::PacketDirection::$direction) => Some($kindt::$nam)),*,
+ _ => None
+ }
+ }
+
+ pub fn with_body_data<'a>(self, data: &'a [u8]) -> $rawpackett<'a> {
+ match self {
+ $($kindt::$nam => $rawpackett::$nam($rawdt{
+ data,
+ _typ: core::marker::PhantomData,
+ })),*,
+ }
+ }
+ }
+
+ impl crate::protocol::PacketKind for $kindt {}
+
$($crate::proto_struct!($body { $($fnam: $ftyp),* });)*
};
}
diff --git a/src/v1_15_2.rs b/src/v1_15_2.rs
index 402d853..fae1cb8 100644
--- a/src/v1_15_2.rs
+++ b/src/v1_15_2.rs
@@ -6,7 +6,7 @@ use fmt::Debug;
#[cfg(all(test, feature = "std"))]
use crate::protocol::TestRandom;
-define_protocol!(578, Packet578, RawPacket578, RawPacket578Body => {
+define_protocol!(578, Packet578, RawPacket578, RawPacket578Body, Packet578Kind => {
// handshaking
Handshake, 0x00, Handshaking, ServerBound => HandshakeSpec {
version: VarInt,
diff --git a/src/v1_16_3.rs b/src/v1_16_3.rs
index 7c7b4d8..b9743d4 100644
--- a/src/v1_16_3.rs
+++ b/src/v1_16_3.rs
@@ -6,7 +6,7 @@ use fmt::Debug;
#[cfg(all(test, feature = "std"))]
use crate::protocol::TestRandom;
-define_protocol!(753, Packet753, RawPacket753, RawPacket753Body => {
+define_protocol!(753, Packet753, RawPacket753, RawPacket753Body, Packet753Kind => {
// handshaking
Handshake, 0x00, Handshaking, ServerBound => HandshakeSpec {
version: VarInt,