diff options
author | Joey Sacchini <joey@sacchini.net> | 2020-10-14 18:45:24 -0400 |
---|---|---|
committer | Joey Sacchini <joey@sacchini.net> | 2020-10-14 18:45:24 -0400 |
commit | 6cbd1b14d7ddb0d2f89980b21f91a67194d8a346 (patch) | |
tree | 1bc1fcc6d957b61e55ddfd58a50db68309555440 | |
parent | d018930688c7efabc67a8257dbcb3258cfeeae93 (diff) | |
download | mcproto-rs-6cbd1b14d7ddb0d2f89980b21f91a67194d8a346.tar.gz mcproto-rs-6cbd1b14d7ddb0d2f89980b21f91a67194d8a346.tar.bz2 mcproto-rs-6cbd1b14d7ddb0d2f89980b21f91a67194d8a346.zip |
update raw packet to allow deserialization of body in a match
-rw-r--r-- | src/protocol.rs | 32 | ||||
-rw-r--r-- | src/v1_15_2.rs | 2 |
2 files changed, 28 insertions, 6 deletions
diff --git a/src/protocol.rs b/src/protocol.rs index 9068f34..7cd84e7 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -158,7 +158,7 @@ macro_rules! __protocol_body_def_helper { #[macro_export] macro_rules! define_protocol { - ($packett: ident, $rawpackett: ident, $directiont: ident, $statet: ident, $idt: ident, $idi: ident => { $($nam: ident, $id: literal, $state: ident, $direction: ident => $body: ident { $($fnam: ident: $ftyp: ty),* }),*}) => { + ($packett: ident, $rawpackett: ident, $rawdt: ident, $directiont: ident, $statet: ident, $idt: ident, $idi: ident => { $($nam: ident, $id: literal, $state: ident, $direction: ident => $body: ident { $($fnam: ident: $ftyp: ty),* }),*}) => { #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub struct $idi { pub id: $idt, @@ -189,7 +189,7 @@ macro_rules! define_protocol { $crate::as_item! { #[derive(Debug, PartialEq)] pub enum $rawpackett<'a> { - $($nam(&'a [u8])),*, + $($nam($rawdt<'a, $body>)),*, } } @@ -272,7 +272,10 @@ macro_rules! define_protocol { use crate::protocol::PacketErr::*; match (value.id.id, value.id.state, value.id.direction) { - $(($id, $state, $direction) => Ok($nam(value.data))),*, + $(($id, $state, $direction) => Ok($nam($rawdt { + data: value.data, + _typ: std::marker::PhantomData, + }))),*, other => Err(UnknownId(other.0)) } } @@ -292,7 +295,7 @@ macro_rules! define_protocol { use self::$rawpackett::*; match self { - $($nam(data) => data),* + $($nam(bod) => bod.data),* } } } @@ -317,7 +320,26 @@ macro_rules! define_protocol { use self::$rawpackett::*; match self { - $($nam(data) => data),* + $($nam(bod) => bod.data),* + } + } + } + + #[derive(PartialEq, Debug)] + pub struct $rawdt<'a, T> { + pub data: &'a [u8], + _typ: std::marker::PhantomData<T> + } + + impl<'a, T> $rawdt<'a, T> where T: crate::Deserialize { + pub fn into_deserialized(self) -> Result<T, crate::protocol::PacketErr> { + use crate::protocol::PacketErr::*; + + let Deserialized { value: body, data: rest } = T::mc_deserialize(self.data).map_err(DeserializeFailed)?; + if !rest.is_empty() { + Err(ExtraData(rest.to_vec())) + } else { + Ok(body) } } } diff --git a/src/v1_15_2.rs b/src/v1_15_2.rs index 83cd516..b6cf2c3 100644 --- a/src/v1_15_2.rs +++ b/src/v1_15_2.rs @@ -42,7 +42,7 @@ impl State { } } -define_protocol!(Packet578, RawPacket578, PacketDirection, State, i32, Id => { +define_protocol!(Packet578, RawPacket578, RawPacket578Body, PacketDirection, State, i32, Id => { // handshaking Handshake, 0x00, Handshaking, ServerBound => HandshakeSpec { version: VarInt, |