Add unit variant derive (#13)
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
use crate::error::DecodeError;
|
||||
use byteorder::{BigEndian, ReadBytesExt};
|
||||
use nbt::CompoundTag;
|
||||
use num_traits::FromPrimitive;
|
||||
use std::io::Read;
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -19,8 +18,6 @@ pub trait DecoderReadExt {
|
||||
|
||||
fn read_byte_array(&mut self) -> Result<Vec<u8>, DecodeError>;
|
||||
|
||||
fn read_enum<T: FromPrimitive>(&mut self) -> Result<T, DecodeError>;
|
||||
|
||||
fn read_compound_tag(&mut self) -> Result<CompoundTag, DecodeError>;
|
||||
|
||||
fn read_var_i32(&mut self) -> Result<i32, DecodeError>;
|
||||
@@ -86,13 +83,6 @@ impl<R: Read> DecoderReadExt for R {
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
fn read_enum<T: FromPrimitive>(&mut self) -> Result<T, DecodeError> {
|
||||
let type_id = self.read_u8()?;
|
||||
let result = FromPrimitive::from_u8(type_id);
|
||||
|
||||
result.ok_or_else(|| DecodeError::UnknownEnumType { type_id })
|
||||
}
|
||||
|
||||
fn read_compound_tag(&mut self) -> Result<CompoundTag, DecodeError> {
|
||||
Ok(nbt::decode::read_compound_tag(self)?)
|
||||
}
|
||||
|
@@ -1,7 +1,6 @@
|
||||
use crate::error::EncodeError;
|
||||
use byteorder::{BigEndian, WriteBytesExt};
|
||||
use nbt::CompoundTag;
|
||||
use num_traits::ToPrimitive;
|
||||
use std::io::Write;
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -17,8 +16,6 @@ pub trait EncoderWriteExt {
|
||||
|
||||
fn write_byte_array(&mut self, value: &[u8]) -> Result<(), EncodeError>;
|
||||
|
||||
fn write_enum<T: ToPrimitive>(&mut self, value: &T) -> Result<(), EncodeError>;
|
||||
|
||||
fn write_compound_tag(&mut self, value: &CompoundTag) -> Result<(), EncodeError>;
|
||||
|
||||
fn write_var_i32(&mut self, value: i32) -> Result<(), EncodeError>;
|
||||
@@ -80,13 +77,6 @@ impl<W: Write> EncoderWriteExt for W {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_enum<T: ToPrimitive>(&mut self, value: &T) -> Result<(), EncodeError> {
|
||||
let type_value = ToPrimitive::to_u8(value).unwrap();
|
||||
self.write_u8(type_value)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_compound_tag(&mut self, value: &CompoundTag) -> Result<(), EncodeError> {
|
||||
nbt::encode::write_compound_tag(self, value.clone())?;
|
||||
|
||||
|
@@ -10,25 +10,6 @@ pub mod version;
|
||||
/// Protocol limits maximum string length.
|
||||
const STRING_MAX_LENGTH: u16 = 32_768;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! impl_enum_encoder_decoder (
|
||||
($ty: ident) => (
|
||||
impl crate::encoder::Encoder for $ty {
|
||||
fn encode<W: std::io::Write>(&self, writer: &mut W) -> Result<(), crate::error::EncodeError> {
|
||||
Ok(crate::encoder::EncoderWriteExt::write_enum(writer, self)?)
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::decoder::Decoder for $ty {
|
||||
type Output = Self;
|
||||
|
||||
fn decode<R: std::io::Read>(reader: &mut R) -> Result<Self::Output, crate::error::DecodeError> {
|
||||
Ok(crate::decoder::DecoderReadExt::read_enum(reader)?)
|
||||
}
|
||||
}
|
||||
);
|
||||
);
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! impl_json_encoder_decoder (
|
||||
($ty: ident) => (
|
||||
|
@@ -1,9 +1,6 @@
|
||||
use num_derive::{FromPrimitive, ToPrimitive};
|
||||
|
||||
use crate::data::chat::Message;
|
||||
use crate::decoder::Decoder;
|
||||
use crate::error::DecodeError;
|
||||
use crate::impl_enum_encoder_decoder;
|
||||
use byteorder::{ReadBytesExt, WriteBytesExt};
|
||||
use minecraft_protocol_derive::{Decoder, Encoder};
|
||||
use nbt::CompoundTag;
|
||||
@@ -113,15 +110,13 @@ pub struct ClientBoundChatMessage {
|
||||
pub position: MessagePosition,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, FromPrimitive, ToPrimitive)]
|
||||
#[derive(Encoder, Decoder, Debug, Eq, PartialEq)]
|
||||
pub enum MessagePosition {
|
||||
Chat,
|
||||
System,
|
||||
HotBar,
|
||||
}
|
||||
|
||||
impl_enum_encoder_decoder!(MessagePosition);
|
||||
|
||||
impl ClientBoundChatMessage {
|
||||
pub fn new(message: Message, position: MessagePosition) -> GameClientBoundPacket {
|
||||
let chat_message = ClientBoundChatMessage { message, position };
|
||||
@@ -143,7 +138,7 @@ pub struct JoinGame {
|
||||
pub reduced_debug_info: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, FromPrimitive, ToPrimitive)]
|
||||
#[derive(Encoder, Decoder, Debug, Eq, PartialEq)]
|
||||
pub enum GameMode {
|
||||
Survival = 0,
|
||||
Creative = 1,
|
||||
@@ -152,8 +147,6 @@ pub enum GameMode {
|
||||
Hardcore = 8,
|
||||
}
|
||||
|
||||
impl_enum_encoder_decoder!(GameMode);
|
||||
|
||||
impl JoinGame {
|
||||
pub fn new(
|
||||
entity_id: u32,
|
||||
@@ -284,7 +277,7 @@ pub enum BossBarAction {
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, FromPrimitive, ToPrimitive)]
|
||||
#[derive(Encoder, Decoder, Debug, PartialEq)]
|
||||
pub enum BossBarColor {
|
||||
Pink,
|
||||
Blue,
|
||||
@@ -295,9 +288,7 @@ pub enum BossBarColor {
|
||||
White,
|
||||
}
|
||||
|
||||
impl_enum_encoder_decoder!(BossBarColor);
|
||||
|
||||
#[derive(Debug, PartialEq, FromPrimitive, ToPrimitive)]
|
||||
#[derive(Encoder, Decoder, Debug, PartialEq)]
|
||||
pub enum BossBarDivision {
|
||||
None,
|
||||
Notches6,
|
||||
@@ -306,8 +297,6 @@ pub enum BossBarDivision {
|
||||
Notches20,
|
||||
}
|
||||
|
||||
impl_enum_encoder_decoder!(BossBarDivision);
|
||||
|
||||
impl BossBar {
|
||||
pub fn new(id: Uuid, action: BossBarAction) -> GameClientBoundPacket {
|
||||
let boss_bar = BossBar { id, action };
|
||||
@@ -479,7 +468,7 @@ mod tests {
|
||||
fn test_join_game_encode() {
|
||||
let join_game = JoinGame {
|
||||
entity_id: 27,
|
||||
game_mode: GameMode::Spectator,
|
||||
game_mode: GameMode::Hardcore,
|
||||
dimension: 23,
|
||||
max_players: 100,
|
||||
level_type: String::from("default"),
|
||||
@@ -503,7 +492,7 @@ mod tests {
|
||||
let join_game = JoinGame::decode(&mut cursor).unwrap();
|
||||
|
||||
assert_eq!(join_game.entity_id, 27);
|
||||
assert_eq!(join_game.game_mode, GameMode::Spectator);
|
||||
assert_eq!(join_game.game_mode, GameMode::Hardcore);
|
||||
assert_eq!(join_game.dimension, 23);
|
||||
assert_eq!(join_game.max_players, 100);
|
||||
assert_eq!(join_game.level_type, String::from("default"));
|
||||
|
Reference in New Issue
Block a user