5 use std
::net
::{SocketAddr
, SocketAddrV4
, ToSocketAddrs
};
6 use serde
::{Serialize
, Serializer
};
8 use crate::HLQueryError
::{IOError
,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
)
29 #[derive(Debug, Serialize)]
32 result
: Result
<Vec
<HLQueryResult
>, HLQueryError
>
36 fn new(input
: String
, result
: Result
<Vec
<HLQueryResult
>, HLQueryError
>) -> Self {
37 Self { input
, result
}
43 IOError(std
::io
::Error
),
44 A2SError(a2s
::errors
::Error
)
47 impl Display
for HLQueryError
{
48 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
50 IOError(e
) => write
!(f
, "{:?}", e
),
51 A2SError(e
) => write
!(f
, "{:?}", e
)
56 impl Error
for HLQueryError
{}
58 impl Serialize
for HLQueryError
{
59 fn serialize
<S
>(&self, serializer
: S
) -> Result
<S
::Ok
, S
::Error
>
63 serializer
.serialize_str(&format
!("{self}"))
67 impl From
<std
::io
::Error
> for HLQueryError
{
68 fn from(e
: std
::io
::Error
) -> Self {
73 impl From
<a2s
::errors
::Error
> for HLQueryError
{
74 fn from(e
: a2s
::errors
::Error
) -> Self {
81 let client
= A2SClient
::new().unwrap
();
82 let query_results
: Vec
<HLQuery
> = args().skip(1)
84 let addresses
= arg
.to_socket_addrs();
87 .map(|lookup_result
| match lookup_result
{
88 (input
, Ok(iter_addr
)) => {
89 (input
, Ok(iter_addr
.filter
_map
(|sa
| match sa
{
90 SocketAddr
::V4(sa4
) => Some(sa4
),
94 (input
, Err(e
)) => (input
, Err(HLQueryError
::IOError(e
)))
96 .map(|(input
, address_group
)| HLQuery
::new(input
, address_group
.map(
97 |addresses
| addresses
.map(|addr
| HLQueryResult
::new(&client
, addr
)).collect())))
100 println
!("{}\n", serde_json
::to_string_pretty(&query_results
).unwrap
());