1macro_rules! packable_enum {
2 (
3 $(#[$attrs:meta])*
4 $vis:vis enum $name:ident : $ty:ty {
5 $(
6 $(#[$vattrs:meta])*
7 $vvis:vis $vname:ident $( = $vval:expr )?,
8 )*
9 }
10 ) => {
11 $(#[$attrs])*
12 $vis enum $name {
13 $(
14 $(#[$vattrs])*
15 $vvis $vname $( = $vval )?,
16 )*
17 }
18
19 impl $crate::packet::pack::Pack for $name {
20 fn pack<W>(self, write: &mut W) -> $crate::packet::pack::Result<()> where W: std::io::Write {
21 <$ty as $crate::packet::pack::Pack>::pack(self as $ty, write)
22 }
23 }
24
25 impl $crate::packet::pack::Unpack for $name {
26 fn unpack<R>(read: &mut R) -> $crate::packet::pack::Result<Self> where R: std::io::Read {
27 #![allow(non_upper_case_globals)]
28 $( const $vname: $ty = $name::$vname as $ty; )*
29 Ok(match <$ty as $crate::packet::pack::Unpack>::unpack(read)? {
30 $( $vname => Self::$vname, )*
31 unknown => return Err($crate::packet::pack::Error::Unexpected(format!("{:X?}", unknown))),
32 })
33 }
34 }
35 }
36}
37
38macro_rules! packable_newtype {
39 (
40 $(#[$attrs:meta])*
41 $vis:vis struct $name:ident ( $ty:ty );
42 ) => {
43 $(#[$attrs])*
44 $vis struct $name ( $ty );
45
46 impl $crate::packet::pack::Pack for $name {
47 fn pack<W>(self, write: &mut W) -> $crate::packet::pack::Result<()> where W: std::io::Write {
48 self.0.pack(write)
49 }
50 }
51
52 impl $crate::packet::pack::Unpack for $name {
53 fn unpack<R>(read: &mut R) -> $crate::packet::pack::Result<Self> where R: std::io::Read {
54 Ok(Self($crate::packet::pack::Unpack::unpack(read)?))
55 }
56 }
57 }
58}
59
60macro_rules! packable_struct {
61 (
62 $(#[$attrs:meta])*
63 $vis:vis struct $name:ident {
64 $(
65 $(#[$fattrs:meta])*
66 $fvis:vis $fname:ident : $fty:ty,
67 )*
68 }
69 ) => {
70 $(#[$attrs])*
71 pub struct $name {
72 $(
73 $(#[$fattrs])*
74 $fvis $fname : $fty,
75 )*
76 }
77
78 impl $crate::packet::pack::Pack for $name {
79 fn pack<W>(self, #[allow(unused_variables)]write: &mut W) -> $crate::packet::pack::Result<()> where W: std::io::Write {
80 $( self.$fname.pack(write)?; )*
81 Ok(())
82 }
83 }
84
85 impl $crate::packet::pack::Unpack for $name {
86 fn unpack<R>(#[allow(unused_variables)]read: &mut R) -> $crate::packet::pack::Result<Self> where R: std::io::Read {
87 Ok(Self {
88 $( $fname : $crate::packet::pack::Unpack::unpack(read)?, )*
89 })
90 }
91 }
92 }
93}