use std::fmt::Display;
use std::error::Error;
use std::env::args;
-use std::net::{SocketAddr, ToSocketAddrs};
+use std::net::{SocketAddr, SocketAddrV4, ToSocketAddrs};
use serde::{Serialize, Serializer};
use a2s::A2SClient;
use crate::HLQueryError::A2SError;
+#[derive(Debug, Serialize)]
+struct HLQueryResult {
+ info: Result<a2s::info::Info, HLQueryError>,
+ rules: Result<Vec<a2s::rules::Rule>, HLQueryError>,
+ players: Result<Vec<a2s::players::Player>, HLQueryError>
+}
+
+impl HLQueryResult {
+ fn new(a2s_client: &A2SClient, server: SocketAddrV4) -> Self {
+ Self {
+ info: a2s_client.info(server).map_err(From::from),
+ rules: a2s_client.rules(server).map_err(From::from),
+ players: a2s_client.players(server).map_err(From::from)
+ }
+ }
+}
+
#[derive(Debug)]
enum HLQueryError {
A2SError(a2s::errors::Error)
println!("Querying address: {}", address);
- let result: Result<a2s::info::Info, HLQueryError> = client.info(address).map_err(From::from);
- println!("{}\n", serde_json::to_string_pretty(&result).unwrap());
-
- let result: Result<Vec<a2s::rules::Rule>, HLQueryError> = client.rules(address).map_err(From::from);
- println!("{}\n", serde_json::to_string_pretty(&result).unwrap());
-
- let result: Result<Vec<a2s::players::Player>, HLQueryError> = client.players(address).map_err(From::from);
+ let result = HLQueryResult::new(&client, address);
println!("{}\n", serde_json::to_string_pretty(&result).unwrap());
}