mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
[Crate] Add better documentation for generic game implementation (#49)
This commit is contained in:
parent
08e00c64e4
commit
4b081371f4
6 changed files with 28 additions and 2 deletions
|
|
@ -116,11 +116,15 @@ use crate::protocols::{self, Protocol};
|
||||||
use crate::GDResult;
|
use crate::GDResult;
|
||||||
use std::net::{IpAddr, SocketAddr};
|
use std::net::{IpAddr, SocketAddr};
|
||||||
|
|
||||||
|
/// Definition of a game
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Game {
|
pub struct Game {
|
||||||
|
/// Full name of the game
|
||||||
pub name: &'static str,
|
pub name: &'static str,
|
||||||
|
/// Default port used by game
|
||||||
pub default_port: u16,
|
pub default_port: u16,
|
||||||
|
/// The protocol the game's query uses
|
||||||
pub protocol: Protocol,
|
pub protocol: Protocol,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -130,6 +134,7 @@ mod definitions;
|
||||||
#[cfg(feature = "game_defs")]
|
#[cfg(feature = "game_defs")]
|
||||||
pub use definitions::GAMES;
|
pub use definitions::GAMES;
|
||||||
|
|
||||||
|
/// Make a query given a game definition
|
||||||
pub fn query(game: &Game, address: &IpAddr, port: Option<u16>) -> GDResult<protocols::GenericResponse> {
|
pub fn query(game: &Game, address: &IpAddr, port: Option<u16>) -> GDResult<protocols::GenericResponse> {
|
||||||
let socket_addr = SocketAddr::new(*address, port.unwrap_or(game.default_port));
|
let socket_addr = SocketAddr::new(*address, port.unwrap_or(game.default_port));
|
||||||
Ok(match &game.protocol {
|
Ok(match &game.protocol {
|
||||||
|
|
|
||||||
19
src/lib.rs
19
src/lib.rs
|
|
@ -2,6 +2,7 @@
|
||||||
//!
|
//!
|
||||||
//! # Usage example:
|
//! # Usage example:
|
||||||
//!
|
//!
|
||||||
|
//! ## For a specific game
|
||||||
//! ```
|
//! ```
|
||||||
//! use gamedig::games::tf2;
|
//! use gamedig::games::tf2;
|
||||||
//!
|
//!
|
||||||
|
|
@ -12,12 +13,26 @@
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
|
//! ## Using a game definition
|
||||||
|
//! ```
|
||||||
|
//! use gamedig::games::{GAMES, query};
|
||||||
|
//!
|
||||||
|
//! let game = GAMES.get("tf2").unwrap(); // Get a game definition, the full list can be found in src/games/mod.rs
|
||||||
|
//! let response = query(game, &"127.0.0.1".parse().unwrap(), None); // None will use the default port
|
||||||
|
//! match response {
|
||||||
|
//! Err(error) => println!("Couldn't query, error: {}", error),
|
||||||
|
//! Ok(r) => println!("{:#?}", r),
|
||||||
|
//! }
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
//! # Crate features:
|
//! # Crate features:
|
||||||
//! Enabled by default: None
|
//! Enabled by default: `game_defs`
|
||||||
//!
|
//!
|
||||||
//! `serde` - enables json serialization/deserialization for all response types.
|
//! `serde` - enables json serialization/deserialization for all response types.
|
||||||
//! <br> `no_games` - disables the included games support. <br>
|
//! <br> `no_games` - disables the included games support. <br>
|
||||||
//! `no_services` - disables the included services support.
|
//! `no_services` - disables the included services support. <br>
|
||||||
|
//! `game_defs` - Include game definitions for programmatic access (enabled by
|
||||||
|
//! default).
|
||||||
|
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
#[cfg(not(feature = "no_games"))]
|
#[cfg(not(feature = "no_games"))]
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ pub mod protocols;
|
||||||
|
|
||||||
pub use protocols::*;
|
pub use protocols::*;
|
||||||
|
|
||||||
|
/// Versions of the gamespy protocol
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum GameSpyVersion {
|
pub enum GameSpyVersion {
|
||||||
|
|
@ -15,6 +16,7 @@ pub enum GameSpyVersion {
|
||||||
Three,
|
Three,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Enum of versions and their ExtraResponse data
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum VersionedExtraResponse {
|
pub enum VersionedExtraResponse {
|
||||||
|
|
|
||||||
|
|
@ -78,6 +78,7 @@ pub struct JavaResponse {
|
||||||
pub server_type: Server,
|
pub server_type: Server,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Non-generic java response
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||||
pub struct JavaExtraResponse {
|
pub struct JavaExtraResponse {
|
||||||
|
|
@ -146,6 +147,7 @@ pub struct BedrockResponse {
|
||||||
pub server_type: Server,
|
pub server_type: Server,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Non-generic bedrock response
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||||
pub struct BedrockExtraResponse {
|
pub struct BedrockExtraResponse {
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ pub struct Response<P> {
|
||||||
pub unused_entries: HashMap<String, String>,
|
pub unused_entries: HashMap<String, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Non-generic quake response
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct ExtraResponse {
|
pub struct ExtraResponse {
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,7 @@ pub struct Response {
|
||||||
pub rules: Option<HashMap<String, String>>,
|
pub rules: Option<HashMap<String, String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Non-generic valve response
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct ExtraResponse {
|
pub struct ExtraResponse {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue