Some minor improvements and documentation

This commit is contained in:
Vladislav Golub 2019-12-17 03:10:05 +03:00
parent 1e5d82a664
commit e3aa488520
2 changed files with 33 additions and 21 deletions

View File

@ -1,61 +1,71 @@
//! This crate implements Minecraft protocol.
//!
//! Information about protocol can be found at https://wiki.vg/Protocol.
use io::Error as IoError;
use mc_varint::{VarIntRead, VarIntWrite}; use mc_varint::{VarIntRead, VarIntWrite};
use serde_json::error::Error as JsonError;
use std::io; use std::io;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::string::FromUtf8Error; use std::string::FromUtf8Error;
pub mod status; pub mod status;
/// Current supported protocol version. /// Current supported protocol version.
pub const PROTOCOL_VERSION: usize = 498; pub const PROTOCOL_VERSION: usize = 498;
/// String maximum length.
const MAX_STRING_LENGTH: usize = 32_768;
/// Possible errors while encoding packet. /// Possible errors while encoding packet.
pub enum EncodeError { pub enum EncodeError {
/// String length can't be more than `MAX_STRING_LENGTH` value.
StringTooLong, StringTooLong,
IOError { IOError {
io_error: io::Error, io_error: IoError,
}, },
JsonError { JsonError {
json_error: serde_json::error::Error, json_error: JsonError,
}, },
} }
impl From<io::Error> for EncodeError { impl From<IoError> for EncodeError {
fn from(io_error: io::Error) -> Self { fn from(io_error: IoError) -> Self {
EncodeError::IOError { io_error } EncodeError::IOError { io_error }
} }
} }
impl From<serde_json::error::Error> for EncodeError { impl From<JsonError> for EncodeError {
fn from(json_error: serde_json::error::Error) -> Self { fn from(json_error: JsonError) -> Self {
EncodeError::JsonError { json_error } EncodeError::JsonError { json_error }
} }
} }
/// Possible errors while decoding packet. /// Possible errors while decoding packet.
pub enum DecodeError { pub enum DecodeError {
/// Packet was not recognized. Invalid data or wrong protocol version.
UnknownPacketType { UnknownPacketType {
type_id: u8, type_id: u8,
}, },
/// String length can't be more than `MAX_STRING_LENGTH` value.
StringTooLong, StringTooLong,
IOError { IOError {
io_error: io::Error, io_error: IoError,
}, },
JsonError { JsonError {
json_error: serde_json::error::Error, json_error: JsonError,
}, },
/// Byte array was not recognized as valid UTF-8 string.
Utf8Error { Utf8Error {
utf8_error: FromUtf8Error, utf8_error: FromUtf8Error,
}, },
} }
impl From<io::Error> for DecodeError { impl From<IoError> for DecodeError {
fn from(io_error: io::Error) -> Self { fn from(io_error: IoError) -> Self {
DecodeError::IOError { io_error } DecodeError::IOError { io_error }
} }
} }
impl From<serde_json::error::Error> for DecodeError { impl From<JsonError> for DecodeError {
fn from(json_error: serde_json::error::Error) -> Self { fn from(json_error: JsonError) -> Self {
DecodeError::JsonError { json_error } DecodeError::JsonError { json_error }
} }
} }
@ -74,10 +84,12 @@ trait Packet {
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError>; fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError>;
} }
/// Trait adds additional helper methods for `Read` to read protocol data.
trait PacketRead { trait PacketRead {
fn read_string(&mut self) -> Result<String, DecodeError>; fn read_string(&mut self) -> Result<String, DecodeError>;
} }
/// Trait adds additional helper methods for `Write` to write protocol data.
trait PacketWrite { trait PacketWrite {
fn write_string(&mut self, value: &str) -> Result<(), EncodeError>; fn write_string(&mut self, value: &str) -> Result<(), EncodeError>;
} }
@ -86,7 +98,7 @@ impl<R: Read> PacketRead for R {
fn read_string(&mut self) -> Result<String, DecodeError> { fn read_string(&mut self) -> Result<String, DecodeError> {
let length = self.read_var_u32()?; let length = self.read_var_u32()?;
if length > 32_767 { if length > MAX_STRING_LENGTH as u32 {
return Err(DecodeError::StringTooLong); return Err(DecodeError::StringTooLong);
} }
@ -99,7 +111,7 @@ impl<R: Read> PacketRead for R {
impl<W: Write> PacketWrite for W { impl<W: Write> PacketWrite for W {
fn write_string(&mut self, value: &str) -> Result<(), EncodeError> { fn write_string(&mut self, value: &str) -> Result<(), EncodeError> {
if value.len() > 32_767 { if value.len() > MAX_STRING_LENGTH {
return Err(EncodeError::StringTooLong); return Err(EncodeError::StringTooLong);
} }

View File

@ -17,15 +17,15 @@ pub enum StatusClientBoundPacket {
impl StatusServerBoundPacket { impl StatusServerBoundPacket {
pub fn get_type_id(&self) -> u8 { pub fn get_type_id(&self) -> u8 {
match self { match self {
StatusServerBoundPacket::StatusRequest => 0x0, StatusServerBoundPacket::StatusRequest => 0x00,
StatusServerBoundPacket::PingRequest(_) => 0x1, StatusServerBoundPacket::PingRequest(_) => 0x01,
} }
} }
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> { pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
match type_id { match type_id {
0x0 => Ok(StatusServerBoundPacket::StatusRequest), 0x00 => Ok(StatusServerBoundPacket::StatusRequest),
0x1 => { 0x01 => {
let ping_request = PingRequest::decode(reader)?; let ping_request = PingRequest::decode(reader)?;
Ok(StatusServerBoundPacket::PingRequest(ping_request)) Ok(StatusServerBoundPacket::PingRequest(ping_request))
@ -38,8 +38,8 @@ impl StatusServerBoundPacket {
impl StatusClientBoundPacket { impl StatusClientBoundPacket {
pub fn get_type_id(&self) -> u8 { pub fn get_type_id(&self) -> u8 {
match self { match self {
StatusClientBoundPacket::StatusResponse(_) => 0x0, StatusClientBoundPacket::StatusResponse(_) => 0x00,
StatusClientBoundPacket::PingResponse(_) => 0x1, StatusClientBoundPacket::PingResponse(_) => 0x01,
} }
} }
} }