mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
[Docs] Improve generic example (#79)
- Allow entering DNS names that can be resolved - Output a list of games if there is no game provided
This commit is contained in:
parent
ada60f2376
commit
a4df444c86
1 changed files with 23 additions and 9 deletions
|
|
@ -5,15 +5,18 @@ use gamedig::{
|
||||||
GAMES,
|
GAMES,
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::net::IpAddr;
|
use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
|
||||||
|
|
||||||
|
/// Make a query given the name of a game
|
||||||
fn generic_query(
|
fn generic_query(
|
||||||
game_name: &str,
|
game_name: &str,
|
||||||
addr: &IpAddr,
|
addr: &IpAddr,
|
||||||
port: Option<u16>,
|
port: Option<u16>,
|
||||||
timeout_settings: Option<TimeoutSettings>,
|
timeout_settings: Option<TimeoutSettings>,
|
||||||
) -> GDResult<Box<dyn CommonResponse>> {
|
) -> GDResult<Box<dyn CommonResponse>> {
|
||||||
let game = GAMES.get(game_name).expect("Game doesn't exist");
|
let game = GAMES
|
||||||
|
.get(game_name)
|
||||||
|
.expect("Game doesn't exist, run without arguments to see a list of games");
|
||||||
|
|
||||||
println!("Querying {:#?} with game {:#?}.", addr, game);
|
println!("Querying {:#?} with game {:#?}.", addr, game);
|
||||||
|
|
||||||
|
|
@ -29,14 +32,25 @@ fn generic_query(
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut args = std::env::args().skip(1);
|
let mut args = std::env::args().skip(1);
|
||||||
|
|
||||||
let game_name = args.next().expect("Must provide a game name");
|
// Handle arguments
|
||||||
let addr: IpAddr = args
|
if let Some(game_name) = args.next() {
|
||||||
.next()
|
// Use to_socket_addrs to resolve hostname to IP
|
||||||
.map(|s| s.parse().unwrap())
|
let addr: SocketAddr = args
|
||||||
.expect("Must provide address");
|
.next()
|
||||||
let port: Option<u16> = args.next().map(|s| s.parse().unwrap());
|
.map(|s| format!("{}:0", s).to_socket_addrs().unwrap())
|
||||||
|
.expect("Must provide address")
|
||||||
|
.next()
|
||||||
|
.expect("Could not lookup host");
|
||||||
|
let port: Option<u16> = args.next().map(|s| s.parse().unwrap());
|
||||||
|
|
||||||
generic_query(&game_name, &addr, port, None).unwrap();
|
generic_query(&game_name, &addr.ip(), port, None).unwrap();
|
||||||
|
} else {
|
||||||
|
// Without arguments print a list of games
|
||||||
|
|
||||||
|
for (name, game) in gamedig::games::GAMES.entries() {
|
||||||
|
println!("{}\t{}", name, game.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue