1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104

#[macro_export]
macro_rules! define_packet
{
    // Define a normal packet.
    ( $ty:ident { $( $field_name:ident : $field_ty:ty),+ }) => {
        #[derive(Clone, Debug, PartialEq)]
        pub struct $ty
        {
            $( pub $field_name : $field_ty ),+
        }

        impl $crate::Parcel for $ty
        {
            fn read(read: &mut ::std::io::Read) -> Result<Self, $crate::Error> {
                #[allow(unused_imports)]
                use $crate::Parcel;

                Ok($ty {
                    $( $field_name : $crate::Parcel::read(read)?, )+
                })
            }

            fn write(&self, write: &mut ::std::io::Write) -> Result<(), $crate::Error> {
                #[allow(unused_imports)]
                use $crate::Parcel;

                $( self.$field_name.write(write)?; )+

                Ok(())
            }
        }
    };

    // Define an empty packet.
    ( $ty:ident ) => {
        #[derive(Clone, Debug, PartialEq)]
        pub struct $ty;

        impl $crate::Parcel for $ty
        {
            fn read(_read: &mut ::std::io::Read) -> Result<Self, $crate::Error> {
                Ok($ty)
            }

            fn write(&self, _write: &mut ::std::io::Write) -> Result<(), $crate::Error> {
                Ok(())
            }
        }
    };
}

/// Defines a packet kind enum.
///
/// You can use any type that implements `Parcel` as the packet ID.
#[macro_export]
macro_rules! define_packet_kind
{
    ( $ty:ident : $id_ty:ty { $( $packet_id:expr => $packet_ty:ident ),+ } ) => {
        #[derive(Clone, Debug, PartialEq)]
        pub enum $ty
        {
            $( $packet_ty($packet_ty) ),+
        }

        impl $ty
        {
            /// Gets the ID of the packet.
            pub fn packet_id(&self) -> $id_ty {
                match *self {
                    $( $ty::$packet_ty(..) => $packet_id ),+
                }
            }
        }

        impl $crate::Parcel for $ty
        {
            fn read(read: &mut ::std::io::Read) -> Result<Self, $crate::Error> {
                let packet_id = <$id_ty as $crate::Parcel>::read(read)?;

                let packet = match packet_id {
                    $( $packet_id => $ty::$packet_ty(<$packet_ty as $crate::Parcel>::read(read)?), )+
                    _ => return Err($crate::ErrorKind::UnknownPacketId.into()),
                };

                Ok(packet)
            }

            fn write(&self, write: &mut ::std::io::Write) -> Result<(), $crate::Error> {
                #[allow(unused_imports)]
                use $crate::Parcel;

                self.packet_id().write(write)?;

                match *self {
                    $( $ty::$packet_ty(ref p) => <$packet_ty as $crate::Parcel>::write(p, write)? ),+
                }

                Ok(())
            }
        }
    }
}