mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-05-06 15:27:28 +00:00
Replaced Result<T, GDError> with GDResult<T>
This commit is contained in:
parent
526aef9acc
commit
15e6ad5892
6 changed files with 25 additions and 25 deletions
|
|
@ -1,6 +1,9 @@
|
|||
use core::fmt;
|
||||
use std::fmt::Formatter;
|
||||
|
||||
/// Result of Type and GDError.
|
||||
pub type GDResult<T> = Result<T, GDError>;
|
||||
|
||||
/// GameDigError, every error you can encounter using the library.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum GDError {
|
||||
|
|
|
|||
|
|
@ -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<u16>) -> Result<Response, GDError> {
|
||||
pub fn query(address: &str, port: Option<u16>) -> GDResult<Response> {
|
||||
let valve_response = ValveProtocol::query(App::CSGO, address, match port {
|
||||
None => 27015,
|
||||
Some(port) => port
|
||||
|
|
|
|||
|
|
@ -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<u16>) -> Result<Response, GDError> {
|
||||
pub fn query(address: &str, port: Option<u16>) -> GDResult<Response> {
|
||||
let valve_response = ValveProtocol::query(App::TF2, address, match port {
|
||||
None => 27015,
|
||||
Some(port) => port
|
||||
|
|
|
|||
|
|
@ -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<u16>) -> Result<Response, GDError> {
|
||||
pub fn query(address: &str, port: Option<u16>) -> GDResult<Response> {
|
||||
let valve_response = ValveProtocol::query(App::TheShip, address, match port {
|
||||
None => 27015,
|
||||
Some(port) => port
|
||||
|
|
|
|||
|
|
@ -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<u16> for App {
|
||||
type Error = GDError;
|
||||
|
||||
fn try_from(value: u16) -> Result<Self, Self::Error> {
|
||||
fn try_from(value: u16) -> GDResult<Self> {
|
||||
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<Vec<u8>, GDError> {
|
||||
fn receive(&self, buffer_size: usize) -> GDResult<Vec<u8>> {
|
||||
let mut buffer: Vec<u8> = 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<Vec<u8>, GDError> {
|
||||
fn receive_truncated(&self, initial_packet: &[u8]) -> GDResult<Vec<u8>> {
|
||||
let count = initial_packet[8] - 1;
|
||||
let mut final_packet: Vec<u8> = initial_packet.to_vec().drain(17..).collect::<Vec<u8>>();
|
||||
|
||||
|
|
@ -187,7 +187,7 @@ impl ValveProtocol {
|
|||
}
|
||||
|
||||
/// Ask for a specific request only.
|
||||
pub fn get_request_data(&self, app: &App, kind: Request) -> Result<Vec<u8>, GDError> {
|
||||
pub fn get_request_data(&self, app: &App, kind: Request) -> GDResult<Vec<u8>> {
|
||||
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<ServerInfo, GDError> {
|
||||
pub fn get_server_info(&self, app: &App) -> GDResult<ServerInfo> {
|
||||
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<Vec<ServerPlayer>, GDError> {
|
||||
pub fn get_server_players(&self, app: &App) -> GDResult<Vec<ServerPlayer>> {
|
||||
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<Option<Vec<ServerRule>>, GDError> {
|
||||
pub fn get_server_rules(&self, app: &App) -> GDResult<Option<Vec<ServerRule>>> {
|
||||
if *app == App::CSGO { //cause csgo response here is broken after feb 21 2014
|
||||
return Ok(None);
|
||||
}
|
||||
|
|
|
|||
14
src/utils.rs
14
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<u8> {
|
||||
[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<u8, GDError> {
|
||||
pub fn get_u8(buf: &[u8], pos: &mut usize) -> GDResult<u8> {
|
||||
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<u16, GDError> {
|
||||
pub fn get_u16_le(buf: &[u8], pos: &mut usize) -> GDResult<u16> {
|
||||
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<u32, GDError> {
|
||||
pub fn get_u32_le(buf: &[u8], pos: &mut usize) -> GDResult<u32> {
|
||||
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<f32, GDError> {
|
||||
pub fn get_f32_le(buf: &[u8], pos: &mut usize) -> GDResult<f32> {
|
||||
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<u64, GDError> {
|
||||
pub fn get_u64_le(buf: &[u8], pos: &mut usize) -> GDResult<u64> {
|
||||
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<String, GDError> {
|
||||
pub fn get_string(buf: &[u8], pos: &mut usize) -> GDResult<String> {
|
||||
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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue