From ba54f4cf570f054cadbfb354a1f7f5a3d4816667 Mon Sep 17 00:00:00 2001 From: EliasSchriefer Date: Fri, 17 Sep 2021 18:54:43 +0200 Subject: [PATCH] Add a few imports --- src/lib.rs | 58 ++++++++++++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e4dc09c..ac3faff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,29 +19,23 @@ use std::{ collections::HashMap, - io, - time::Duration + error::Error, + io::{ + ErrorKind, + self, + }, + time::Duration, + fmt::{ + Display, + Formatter, + self, + } }; use tokio::{ net::{ ToSocketAddrs, UdpSocket }, time::timeout as tokioTimeout, }; -/// Bitmask to validate a session ID -/// -/// Usually you don't need this. -/// -/// # Example -/// ``` -/// use mcquery::SESSION_ID_MASK; -/// -/// # #[allow(unused_variables)] -/// # fn main() { -/// let session_id = 0x12345678_u32; -/// let valid_session_id = session_id & SESSION_ID_MASK; -/// # } -/// ``` -pub const SESSION_ID_MASK: u32 = 0x0F0F0F0F; const REQUEST_HEADER: [u8; 2] = [0xFE, 0xFD]; fn gen_session_id() -> u32 { @@ -61,10 +55,10 @@ fn gen_session_id() -> u32 { #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub struct MaybeIncompleteDataError(usize); -impl std::error::Error for MaybeIncompleteDataError {} +impl Error for MaybeIncompleteDataError {} -impl core::fmt::Display for MaybeIncompleteDataError { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { +impl Display for MaybeIncompleteDataError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "Buffer filled up") } } @@ -116,12 +110,12 @@ impl HandshakeRequest { .into_iter() .take_while(|x| x != &0x00) .collect() - ).map_err(|_| io::ErrorKind::InvalidData)? + ).map_err(|_| ErrorKind::InvalidData)? .parse() - .map_err(|_| io::ErrorKind::InvalidData)?; + .map_err(|_| ErrorKind::InvalidData)?; if res_packet_type != Self::PACKET_TYPE as u8 || res_session_id != self.session_id { - Err(io::ErrorKind::InvalidData.into()) + Err(ErrorKind::InvalidData.into()) } else { Ok(HandshakeResponse { session_id: res_session_id, @@ -181,7 +175,7 @@ impl BasicQueryRequest { // Receiving let bytes_read = tokioTimeout(timeout, socket.recv(buffer)).await??; if bytes_read == buffer.len() { - return Err(io::Error::new(io::ErrorKind::Other, MaybeIncompleteDataError(bytes_read))); + return Err(io::Error::new(ErrorKind::Other, MaybeIncompleteDataError(bytes_read))); } // Parsing @@ -230,10 +224,10 @@ impl BasicQueryRequest { }; // Number of players - let res_num_players = res_num_players.parse().map_err(|_| io::ErrorKind::InvalidData)?; + let res_num_players = res_num_players.parse().map_err(|_| ErrorKind::InvalidData)?; // Max number of players - let res_max_players = res_max_players.parse().map_err(|_| io::ErrorKind::InvalidData)?; + let res_max_players = res_max_players.parse().map_err(|_| ErrorKind::InvalidData)?; // Host port let mut res_port = [0; 2]; @@ -243,10 +237,10 @@ impl BasicQueryRequest { let res_port = u16::from_le_bytes(res_port); // Host IP - let res_ip = String::from_utf8(res_address).map_err(|_| io::ErrorKind::InvalidData)?; + let res_ip = String::from_utf8(res_address).map_err(|_| ErrorKind::InvalidData)?; if res_packet_type != Self::PACKET_TYPE as u8 || res_session_id != self.session_id { - Err(io::ErrorKind::InvalidData.into()) + Err(ErrorKind::InvalidData.into()) } else { Ok(BasicQueryResponse { session_id: res_session_id, @@ -308,7 +302,7 @@ impl FullQueryRequest { // Receiving let bytes_read = tokioTimeout(timeout, socket.recv(buffer)).await??; if bytes_read == buffer.len() { - return Err(io::Error::new(io::ErrorKind::Other, MaybeIncompleteDataError(bytes_read))); + return Err(io::Error::new(ErrorKind::Other, MaybeIncompleteDataError(bytes_read))); } // Parsing @@ -351,7 +345,7 @@ impl FullQueryRequest { .collect() ); } - res_kv.remove("splitnum").ok_or(io::ErrorKind::InvalidData)?; + res_kv.remove("splitnum").ok_or(ErrorKind::InvalidData)?; // Plugins and server software let ( @@ -380,7 +374,7 @@ impl FullQueryRequest { { let player_header = "\x01player_\0\0"; if !response_buffer.starts_with(player_header.as_bytes()) { - return Err(io::ErrorKind::InvalidData.into()); + return Err(ErrorKind::InvalidData.into()); } else { response_buffer.drain(0..player_header.len()); } @@ -404,7 +398,7 @@ impl FullQueryRequest { } if res_packet_type != Self::PACKET_TYPE as u8 || res_session_id != self.session_id { - Err(io::ErrorKind::InvalidData.into()) + Err(ErrorKind::InvalidData.into()) } else { Ok(FullQueryResponse { session_id: res_session_id,