Some minor improvements and documentation
This commit is contained in:
parent
1e5d82a664
commit
e3aa488520
42
src/lib.rs
42
src/lib.rs
@ -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 serde_json::error::Error as JsonError;
|
||||
use std::io;
|
||||
use std::io::{Read, Write};
|
||||
use std::string::FromUtf8Error;
|
||||
|
||||
pub mod status;
|
||||
|
||||
/// Current supported protocol version.
|
||||
pub const PROTOCOL_VERSION: usize = 498;
|
||||
/// String maximum length.
|
||||
const MAX_STRING_LENGTH: usize = 32_768;
|
||||
|
||||
/// Possible errors while encoding packet.
|
||||
pub enum EncodeError {
|
||||
/// String length can't be more than `MAX_STRING_LENGTH` value.
|
||||
StringTooLong,
|
||||
IOError {
|
||||
io_error: io::Error,
|
||||
io_error: IoError,
|
||||
},
|
||||
JsonError {
|
||||
json_error: serde_json::error::Error,
|
||||
json_error: JsonError,
|
||||
},
|
||||
}
|
||||
|
||||
impl From<io::Error> for EncodeError {
|
||||
fn from(io_error: io::Error) -> Self {
|
||||
impl From<IoError> for EncodeError {
|
||||
fn from(io_error: IoError) -> Self {
|
||||
EncodeError::IOError { io_error }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<serde_json::error::Error> for EncodeError {
|
||||
fn from(json_error: serde_json::error::Error) -> Self {
|
||||
impl From<JsonError> for EncodeError {
|
||||
fn from(json_error: JsonError) -> Self {
|
||||
EncodeError::JsonError { json_error }
|
||||
}
|
||||
}
|
||||
|
||||
/// Possible errors while decoding packet.
|
||||
pub enum DecodeError {
|
||||
/// Packet was not recognized. Invalid data or wrong protocol version.
|
||||
UnknownPacketType {
|
||||
type_id: u8,
|
||||
},
|
||||
/// String length can't be more than `MAX_STRING_LENGTH` value.
|
||||
StringTooLong,
|
||||
IOError {
|
||||
io_error: io::Error,
|
||||
io_error: IoError,
|
||||
},
|
||||
JsonError {
|
||||
json_error: serde_json::error::Error,
|
||||
json_error: JsonError,
|
||||
},
|
||||
/// Byte array was not recognized as valid UTF-8 string.
|
||||
Utf8Error {
|
||||
utf8_error: FromUtf8Error,
|
||||
},
|
||||
}
|
||||
|
||||
impl From<io::Error> for DecodeError {
|
||||
fn from(io_error: io::Error) -> Self {
|
||||
impl From<IoError> for DecodeError {
|
||||
fn from(io_error: IoError) -> Self {
|
||||
DecodeError::IOError { io_error }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<serde_json::error::Error> for DecodeError {
|
||||
fn from(json_error: serde_json::error::Error) -> Self {
|
||||
impl From<JsonError> for DecodeError {
|
||||
fn from(json_error: JsonError) -> Self {
|
||||
DecodeError::JsonError { json_error }
|
||||
}
|
||||
}
|
||||
@ -74,10 +84,12 @@ trait Packet {
|
||||
fn decode<R: Read>(reader: &mut R) -> Result<Self::Output, DecodeError>;
|
||||
}
|
||||
|
||||
/// Trait adds additional helper methods for `Read` to read protocol data.
|
||||
trait PacketRead {
|
||||
fn read_string(&mut self) -> Result<String, DecodeError>;
|
||||
}
|
||||
|
||||
/// Trait adds additional helper methods for `Write` to write protocol data.
|
||||
trait PacketWrite {
|
||||
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> {
|
||||
let length = self.read_var_u32()?;
|
||||
|
||||
if length > 32_767 {
|
||||
if length > MAX_STRING_LENGTH as u32 {
|
||||
return Err(DecodeError::StringTooLong);
|
||||
}
|
||||
|
||||
@ -99,7 +111,7 @@ impl<R: Read> PacketRead for R {
|
||||
|
||||
impl<W: Write> PacketWrite for W {
|
||||
fn write_string(&mut self, value: &str) -> Result<(), EncodeError> {
|
||||
if value.len() > 32_767 {
|
||||
if value.len() > MAX_STRING_LENGTH {
|
||||
return Err(EncodeError::StringTooLong);
|
||||
}
|
||||
|
||||
|
@ -17,15 +17,15 @@ pub enum StatusClientBoundPacket {
|
||||
impl StatusServerBoundPacket {
|
||||
pub fn get_type_id(&self) -> u8 {
|
||||
match self {
|
||||
StatusServerBoundPacket::StatusRequest => 0x0,
|
||||
StatusServerBoundPacket::PingRequest(_) => 0x1,
|
||||
StatusServerBoundPacket::StatusRequest => 0x00,
|
||||
StatusServerBoundPacket::PingRequest(_) => 0x01,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decode<R: Read>(type_id: u8, reader: &mut R) -> Result<Self, DecodeError> {
|
||||
match type_id {
|
||||
0x0 => Ok(StatusServerBoundPacket::StatusRequest),
|
||||
0x1 => {
|
||||
0x00 => Ok(StatusServerBoundPacket::StatusRequest),
|
||||
0x01 => {
|
||||
let ping_request = PingRequest::decode(reader)?;
|
||||
|
||||
Ok(StatusServerBoundPacket::PingRequest(ping_request))
|
||||
@ -38,8 +38,8 @@ impl StatusServerBoundPacket {
|
||||
impl StatusClientBoundPacket {
|
||||
pub fn get_type_id(&self) -> u8 {
|
||||
match self {
|
||||
StatusClientBoundPacket::StatusResponse(_) => 0x0,
|
||||
StatusClientBoundPacket::PingResponse(_) => 0x1,
|
||||
StatusClientBoundPacket::StatusResponse(_) => 0x00,
|
||||
StatusClientBoundPacket::PingResponse(_) => 0x01,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user