rust-gamedig/crates/lib/src/protocols/gamespy/mod.rs
Tom 7510fe3de0
Various improvements for the CLI (#159)
* cli: Do DNS lookup if host is not an IP address

* cli: Add option to output as JSON

* cli: Pass hostname to ExtraRequestSettings if it isn't an IP

* cli: Add help docs to all arguments

* cli: Add options for all extra request settings

* cli: Use a CLI only error for DNS

* cli: Add option to set timeout settings

* docs: Update CHANGELOG

* cli: Add default values to TimeoutSettings

* cli: Refactor finding game definition into its own function

Co-Authored-By: Cain <75994858+cainthebest@users.noreply.github.com>

* cli: Refactor IP resolution into its own set of functions

Co-Authored-By: Cain <75994858+cainthebest@users.noreply.github.com>

* cli: Refactor output formatting into its own functions

Co-Authored-By: Cain <75994858+cainthebest@users.noreply.github.com>

* cli: Improve doc comments for CLI args and derive Debug

Co-Authored-By: Cain <75994858+cainthebest@users.noreply.github.com>

* protocols: Derive Serialize for versioned generic responses

This allows for serializing the output of as_original(). We cannot also
derive Deserialize here because the enums use references to the inner
types, which is unavoidable in the current implementation because
as_original() takes a reference to self.

* cli: Add the output mode options

This allows selected whether to use CommonResponse or the original
response struct when outputting.

* cli: Fix ExtraRequestSettings docs showing up in help output

* cli: Add help headings for timeouts and extra request settings

---------

Co-authored-by: Cain <75994858+cainthebest@users.noreply.github.com>
2023-11-26 22:59:59 +00:00

92 lines
2.9 KiB
Rust

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
pub(crate) mod common;
/// The implementations.
pub mod protocols;
pub use protocols::*;
/// Versions of the gamespy protocol
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GameSpyVersion {
One,
Two,
Three,
}
/// Versioned response type
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VersionedResponse<'a> {
One(&'a one::Response),
Two(&'a two::Response),
Three(&'a three::Response),
}
/// Versioned player type
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VersionedPlayer<'a> {
One(&'a one::Player),
Two(&'a two::Player),
Three(&'a three::Player),
}
/// Generate a module containing a query function for a gamespy game.
///
/// * `mod_name` - The name to be given to the game module (see ID naming
/// conventions in CONTRIBUTING.md).
/// * `pretty_name` - The full name of the game, will be used as the
/// documentation for the created module.
/// * `gamespy_ver`, `default_port` - Passed through to [game_query_fn].
#[cfg(feature = "games")]
macro_rules! game_query_mod {
($mod_name: ident, $pretty_name: expr, $gamespy_ver: ident, $default_port: literal) => {
#[doc = $pretty_name]
pub mod $mod_name {
crate::protocols::gamespy::game_query_fn!($gamespy_ver, $default_port);
}
};
}
#[cfg(feature = "games")]
pub(crate) use game_query_mod;
// Allow generating doc comments:
// https://users.rust-lang.org/t/macros-filling-text-in-comments/20473
/// Generate a query function for a gamespy game.
///
/// * `gamespy_ver` - The name of the [module](crate::protocols::gamespy) for
/// the gamespy version the game uses.
/// * `default_port` - The default port the game uses.
///
/// ```rust,ignore
/// use crate::protocols::gamespy::game_query_fn;
/// game_query_fn!(one, 7778);
/// ```
#[cfg(feature = "games")]
macro_rules! game_query_fn {
($gamespy_ver: ident, $default_port: literal) => {
crate::protocols::gamespy::game_query_fn! {@gen $gamespy_ver, $default_port, concat!(
"Make a gamespy ", stringify!($gamespy_ver), " query with default timeout settings.\n\n",
"If port is `None`, then the default port (", stringify!($default_port), ") will be used.")}
};
(@gen $gamespy_ver: ident, $default_port: literal, $doc: expr) => {
#[doc = $doc]
pub fn query(
address: &std::net::IpAddr,
port: Option<u16>,
) -> crate::GDResult<crate::protocols::gamespy::$gamespy_ver::Response> {
crate::protocols::gamespy::$gamespy_ver::query(
&std::net::SocketAddr::new(*address, port.unwrap_or($default_port)),
None,
)
}
};
}
#[cfg(feature = "games")]
pub(crate) use game_query_fn;