From 15e6ad5892bb73a7d236bebea3076024abff3dbf Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Thu, 20 Oct 2022 23:19:57 +0300 Subject: [PATCH] Replaced Result with GDResult --- src/errors.rs | 3 +++ src/games/csgo.rs | 5 ++--- src/games/tf2.rs | 5 ++--- src/games/the_ship.rs | 5 ++--- src/protocols/valve.rs | 18 +++++++++--------- src/utils.rs | 14 +++++++------- 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index 6e2210f..f4713d4 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,6 +1,9 @@ use core::fmt; use std::fmt::Formatter; +/// Result of Type and GDError. +pub type GDResult = Result; + /// GameDigError, every error you can encounter using the library. #[derive(Debug, Clone)] pub enum GDError { diff --git a/src/games/csgo.rs b/src/games/csgo.rs index 20d0416..d946e83 100644 --- a/src/games/csgo.rs +++ b/src/games/csgo.rs @@ -1,5 +1,4 @@ -use crate::errors::GDError; -use crate::valve; +use crate::{GDResult, valve}; use crate::valve::{ValveProtocol, App, GatheringSettings, ServerPlayer, Server}; #[derive(Debug)] @@ -69,7 +68,7 @@ impl Response { } } -pub fn query(address: &str, port: Option) -> Result { +pub fn query(address: &str, port: Option) -> GDResult { let valve_response = ValveProtocol::query(App::CSGO, address, match port { None => 27015, Some(port) => port diff --git a/src/games/tf2.rs b/src/games/tf2.rs index 528961f..f25a94c 100644 --- a/src/games/tf2.rs +++ b/src/games/tf2.rs @@ -1,5 +1,4 @@ -use crate::errors::GDError; -use crate::valve; +use crate::{GDResult, valve}; use crate::valve::{ValveProtocol, App, GatheringSettings, Server, ServerRule, ServerPlayer}; #[derive(Debug)] @@ -71,7 +70,7 @@ impl Response { } } -pub fn query(address: &str, port: Option) -> Result { +pub fn query(address: &str, port: Option) -> GDResult { let valve_response = ValveProtocol::query(App::TF2, address, match port { None => 27015, Some(port) => port diff --git a/src/games/the_ship.rs b/src/games/the_ship.rs index 0561930..6f27695 100644 --- a/src/games/the_ship.rs +++ b/src/games/the_ship.rs @@ -1,5 +1,4 @@ -use crate::errors::GDError; -use crate::valve; +use crate::{GDResult, valve}; use crate::valve::{ValveProtocol, App, GatheringSettings, ServerPlayer, Server, ServerRule}; #[derive(Debug)] @@ -83,7 +82,7 @@ impl Response { } } -pub fn query(address: &str, port: Option) -> Result { +pub fn query(address: &str, port: Option) -> GDResult { let valve_response = ValveProtocol::query(App::TheShip, address, match port { None => 27015, Some(port) => port diff --git a/src/protocols/valve.rs b/src/protocols/valve.rs index 6bcc344..82e1a74 100644 --- a/src/protocols/valve.rs +++ b/src/protocols/valve.rs @@ -1,5 +1,5 @@ use std::net::UdpSocket; -use crate::errors::GDError; +use crate::{GDError, GDResult}; use crate::utils::{buffer, complete_address, concat_u8_arrays}; /// The type of the server. @@ -132,7 +132,7 @@ pub enum App { impl TryFrom for App { type Error = GDError; - fn try_from(value: u16) -> Result { + fn try_from(value: u16) -> GDResult { match value { x if x == App::TF2 as u16 => Ok(App::TF2), x if x == App::CSGO as u16 => Ok(App::CSGO), @@ -163,18 +163,18 @@ impl ValveProtocol { } } - fn send(&self, data: &[u8]) -> Result<(), GDError> { + fn send(&self, data: &[u8]) -> GDResult<()> { self.socket.send_to(&data, &self.complete_address).map_err(|e| GDError::PacketSend(e.to_string()))?; Ok(()) } - fn receive(&self, buffer_size: usize) -> Result, GDError> { + fn receive(&self, buffer_size: usize) -> GDResult> { let mut buffer: Vec = vec![0; buffer_size]; let (amt, _) = self.socket.recv_from(&mut buffer.as_mut_slice()).map_err(|e| GDError::PacketReceive(e.to_string()))?; Ok(buffer[..amt].to_vec()) } - fn receive_truncated(&self, initial_packet: &[u8]) -> Result, GDError> { + fn receive_truncated(&self, initial_packet: &[u8]) -> GDResult> { let count = initial_packet[8] - 1; let mut final_packet: Vec = initial_packet.to_vec().drain(17..).collect::>(); @@ -187,7 +187,7 @@ impl ValveProtocol { } /// Ask for a specific request only. - pub fn get_request_data(&self, app: &App, kind: Request) -> Result, GDError> { + pub fn get_request_data(&self, app: &App, kind: Request) -> GDResult> { let info_initial_packet = vec![0xFF, 0xFF, 0xFF, 0xFF, 0x54, 0x53, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x20, 0x45, 0x6E, 0x67, 0x69, 0x6E, 0x65, 0x20, 0x51, 0x75, 0x65, 0x72, 0x79, 0x00]; let players_initial_packet = vec![0xFF, 0xFF, 0xFF, 0xFF, 0x55, 0xFF, 0xFF, 0xFF, 0xFF]; let rules_initial_packet = vec![0xFF, 0xFF, 0xFF, 0xFF, 0x56, 0xFF, 0xFF, 0xFF, 0xFF]; @@ -227,7 +227,7 @@ impl ValveProtocol { } /// Get the server information's. - pub fn get_server_info(&self, app: &App) -> Result { + pub fn get_server_info(&self, app: &App) -> GDResult { let buf = self.get_request_data(app, Request::INFO)?; let mut pos = 0; @@ -297,7 +297,7 @@ impl ValveProtocol { } /// Get the server player's. - pub fn get_server_players(&self, app: &App) -> Result, GDError> { + pub fn get_server_players(&self, app: &App) -> GDResult> { let buf = self.get_request_data(app, Request::PLAYERS)?; let mut pos = 0; @@ -325,7 +325,7 @@ impl ValveProtocol { } /// Get the server rules's. - pub fn get_server_rules(&self, app: &App) -> Result>, GDError> { + pub fn get_server_rules(&self, app: &App) -> GDResult>> { if *app == App::CSGO { //cause csgo response here is broken after feb 21 2014 return Ok(None); } diff --git a/src/utils.rs b/src/utils.rs index 6dd678d..cdce785 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,5 @@ use std::ops::Add; -use crate::GDError; +use crate::{GDResult, GDError}; pub fn concat_u8_arrays(first: &[u8], second: &[u8]) -> Vec { [first, second].concat() @@ -12,7 +12,7 @@ pub fn complete_address(address: &str, port: u16) -> String { pub mod buffer { use super::*; - pub fn get_u8(buf: &[u8], pos: &mut usize) -> Result { + pub fn get_u8(buf: &[u8], pos: &mut usize) -> GDResult { if buf.len() <= *pos { return Err(GDError::PacketUnderflow("Unexpectedly short packet.".to_string())); } @@ -22,7 +22,7 @@ pub mod buffer { Ok(value) } - pub fn get_u16_le(buf: &[u8], pos: &mut usize) -> Result { + pub fn get_u16_le(buf: &[u8], pos: &mut usize) -> GDResult { if buf.len() <= *pos + 1 { return Err(GDError::PacketUnderflow("Unexpectedly short packet.".to_string())); } @@ -32,7 +32,7 @@ pub mod buffer { Ok(value) } - pub fn get_u32_le(buf: &[u8], pos: &mut usize) -> Result { + pub fn get_u32_le(buf: &[u8], pos: &mut usize) -> GDResult { if buf.len() <= *pos + 3 { return Err(GDError::PacketUnderflow("Unexpectedly short packet.".to_string())); } @@ -42,7 +42,7 @@ pub mod buffer { Ok(value) } - pub fn get_f32_le(buf: &[u8], pos: &mut usize) -> Result { + pub fn get_f32_le(buf: &[u8], pos: &mut usize) -> GDResult { if buf.len() <= *pos + 3 { return Err(GDError::PacketUnderflow("Unexpectedly short packet.".to_string())); } @@ -52,7 +52,7 @@ pub mod buffer { Ok(value) } - pub fn get_u64_le(buf: &[u8], pos: &mut usize) -> Result { + pub fn get_u64_le(buf: &[u8], pos: &mut usize) -> GDResult { if buf.len() <= *pos + 7 { return Err(GDError::PacketUnderflow("Unexpectedly short packet.".to_string())); } @@ -62,7 +62,7 @@ pub mod buffer { Ok(value) } - pub fn get_string(buf: &[u8], pos: &mut usize) -> Result { + pub fn get_string(buf: &[u8], pos: &mut usize) -> GDResult { let sub_buf = &buf[*pos..]; let first_null_position = sub_buf.iter().position(|&x| x == 0).ok_or(GDError::PacketBad("Unexpectedly formatted packet.".to_string()))?; let value = std::str::from_utf8(&sub_buf[..first_null_position]).unwrap().to_string();