[Generic] Add struct for all extra request settings (#93)

* [Generic] Add struct for all extra request settings

Adds a new struct `ExtraRequestSettings` that contains all possible
extra settings for all protocols, and can be implicitly converted with
`.into()` into each protocol's extra settings type.

This is then used in a new query method
`query_with_timeout_and_extra_settings()` that passes the object on to
the selected protocol.

This also updates the generic example to set some of these generic
settings so that it can be used for certain queries like
"mc.hypixel.net".

* [Minecraft] Add `request_settings` parameter to auto query

This allows generic queries to pass through request settings when using
the `mc` game so that servers like `mc.hypixel.net` will still work when
using auto query.

* Fix generic examples tests (and enable example tests in pre-commit)
This commit is contained in:
Tom 2023-09-04 22:13:12 +00:00 committed by GitHub
parent 76a3ac2f78
commit 6c1fdb1159
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 175 additions and 20 deletions

View file

@ -1,6 +1,6 @@
use gamedig::{
protocols::types::{CommonResponse, TimeoutSettings},
query_with_timeout,
protocols::types::{CommonResponse, ExtraRequestSettings, TimeoutSettings},
query_with_timeout_and_extra_settings,
GDResult,
GAMES,
};
@ -13,6 +13,7 @@ fn generic_query(
addr: &IpAddr,
port: Option<u16>,
timeout_settings: Option<TimeoutSettings>,
extra_settings: Option<ExtraRequestSettings>,
) -> GDResult<Box<dyn CommonResponse>> {
let game = GAMES
.get(game_name)
@ -20,7 +21,7 @@ fn generic_query(
println!("Querying {:#?} with game {:#?}.", addr, game);
let response = query_with_timeout(game, addr, port, timeout_settings)?;
let response = query_with_timeout_and_extra_settings(game, addr, port, timeout_settings, extra_settings)?;
println!("Response: {:#?}", response.as_json());
let common = response.as_original();
@ -34,16 +35,22 @@ fn main() {
// Handle arguments
if let Some(game_name) = args.next() {
let hostname = args.next().expect("Must provide an address");
// Use to_socket_addrs to resolve hostname to IP
let addr: SocketAddr = args
.next()
.map(|s| format!("{}:0", s).to_socket_addrs().unwrap())
.expect("Must provide address")
let addr: SocketAddr = format!("{}:0", hostname)
.to_socket_addrs()
.unwrap()
.next()
.expect("Could not lookup host");
let port: Option<u16> = args.next().map(|s| s.parse().unwrap());
generic_query(&game_name, &addr.ip(), port, None).unwrap();
let extra_settings = ExtraRequestSettings::default()
.set_hostname(hostname.to_string())
.set_gather_rules(true)
.set_gather_players(true)
.set_check_app_id(false);
generic_query(&game_name, &addr.ip(), port, None, Some(extra_settings)).unwrap();
} else {
// Without arguments print a list of games
@ -68,7 +75,7 @@ mod test {
fn test_game(game_name: &str) {
let timeout_settings =
Some(TimeoutSettings::new(Some(Duration::from_nanos(1)), Some(Duration::from_nanos(1))).unwrap());
assert!(generic_query(game_name, &ADDR, None, timeout_settings).is_err());
assert!(generic_query(game_name, &ADDR, None, timeout_settings, None).is_err());
}
#[test]