c62bc7f78f71e3bd830e57ec0260d8d2e4646dca
5 use std
::net
::{SocketAddr
, SocketAddrV4
, ToSocketAddrs
};
6 use serde
::{Serialize
, Serializer
};
8 use crate::HLQueryError
::A2SError
;
10 #[derive(Debug, Serialize)]
11 struct HLQueryResult
{
12 address
: SocketAddrV4
,
13 info
: Result
<a2s
::info
::Info
, HLQueryError
>,
14 rules
: Result
<Vec
<a2s
::rules
::Rule
>, HLQueryError
>,
15 players
: Result
<Vec
<a2s
::players
::Player
>, HLQueryError
>
19 fn new(a2s_client
: &A2SClient
, server
: SocketAddrV4
) -> Self {
22 info
: a2s_client
.in
fo
(server
).map_err(From
::from
),
23 rules
: a2s_client
.rules(server
).map_err(From
::from
),
24 players
: a2s_client
.players(server
).map_err(From
::from
)
31 A2SError(a2s
::errors
::Error
)
34 impl Display
for HLQueryError
{
35 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
37 A2SError(e
) => write
!(f
, "{}", e
)
42 impl Error
for HLQueryError
{}
44 impl Serialize
for HLQueryError
{
45 fn serialize
<S
>(&self, serializer
: S
) -> Result
<S
::Ok
, S
::Error
>
49 serializer
.serialize_str(&format
!("{self}"))
53 impl From
<a2s
::errors
::Error
> for HLQueryError
{
54 fn from(e
: a2s
::errors
::Error
) -> Self {
61 let client
= A2SClient
::new().unwrap
();
62 let addresses
= args().skip(1)
63 .map(|arg
| arg
.to_socket_addrs())
64 .map(|lookup_result
| match lookup_result
{
66 Ok(iter_addr
.flat
_map
(|sa
| match sa
{
67 SocketAddr
::V4(sa4
) => Some(sa4
),
74 for address
in addresses
{
76 println
!("Querying address: {:?}", address
);
78 let result
= HLQueryResult
::new(&client
, address
.unwrap
().next().unwrap
());
79 println
!("{}\n", serde_json
::to_string_pretty(&result
).unwrap
());