cubic_protocol/
packet_node.rs1#[macro_export]
2macro_rules! packet_node {
3 ($(#[$meta:meta])* $node_ident: ident => [
4 $($packet_ident:ident$(<$($packet_generic:ty$(,)*)*>)*$(,)*)*
5 ]) => {
6 $(#[$meta])*
7 pub enum $node_ident {
8 $($packet_ident($packet_ident<$($($packet_generic,)*)*>),)*
9 }
10
11 $(impl From<$packet_ident<$($($packet_generic,)*)*>> for $node_ident {
12 fn from(packet: $packet_ident<$($($packet_generic,)*)*>) -> Self {
13 Self::$packet_ident(packet)
14 }
15 })*
16
17 #[async_trait::async_trait]
18 impl $crate::packet::PacketWritable for $node_ident {
19 async fn write(self, output: &mut impl $crate::packet::OutputPacketBytes) ->
20 $crate::packet::PacketWritableResult {
21 Ok(match self {
22 $(Self::$packet_ident(packet) => packet.write(output).await?,)*
23 }.into())
24 }
25 }
26
27 #[async_trait::async_trait]
28 impl $crate::packet::PacketReadable for $node_ident {
29 async fn read(input: &mut impl $crate::packet::InputPacketBytes) ->
30 $crate::packet::PacketReadableResult<Self> {
31 let packet_id = VarInt::read(input).await?.0;
32 Ok(match packet_id {
33 $($packet_ident::<$($($packet_generic,)*)*>::ID => Self::$packet_ident(
34 $packet_ident::<$($($packet_generic,)*)*>::read(input).await?),
35 )*
36 _ => return Err(
37 $crate::packet::PacketReadableError::Custom($crate::packet::CustomError::StaticStr("Bad packet id"))
38 )
39 })
40 }
41 }
42 };
43}