4 use std
::net
::{SocketAddr
, SocketAddrV4
, ToSocketAddrs
};
6 use serde
::{Serialize
, Serializer
};
8 use crate::HLQueryError
::{IOError
,A2SError
};
11 #[command(name = "HLQuery")]
12 #[command(author = "MegaBrutal")]
14 #[command(about = "Query Half-Life servers", long_about = None)]
19 addresses
: Vec
<String
>
23 #[derive(Debug, Serialize)]
24 struct HLQueryResult
{
25 address
: SocketAddrV4
,
26 info
: Result
<a2s
::info
::Info
, HLQueryError
>,
27 rules
: Result
<Vec
<a2s
::rules
::Rule
>, HLQueryError
>,
28 players
: Result
<Vec
<a2s
::players
::Player
>, HLQueryError
>
32 fn new(a2s_client
: &A2SClient
, server
: SocketAddrV4
) -> Self {
35 info
: a2s_client
.in
fo
(server
).map_err(From
::from
),
36 rules
: a2s_client
.rules(server
).map_err(From
::from
),
37 players
: a2s_client
.players(server
).map_err(From
::from
)
42 #[derive(Debug, Serialize)]
45 result
: Result
<Vec
<HLQueryResult
>, HLQueryError
>
49 fn new
<S
: Into
<String
>>(input
: S
, result
: Result
<Vec
<HLQueryResult
>, HLQueryError
>) -> Self {
50 let input
= input
.into
();
51 Self { input
, result
}
57 IOError(std
::io
::Error
),
58 A2SError(a2s
::errors
::Error
)
61 impl Display
for HLQueryError
{
62 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
64 IOError(e
) => write
!(f
, "{:?}", e
),
65 A2SError(e
) => write
!(f
, "{:?}", e
)
70 impl Error
for HLQueryError
{}
72 impl Serialize
for HLQueryError
{
73 fn serialize
<S
>(&self, serializer
: S
) -> Result
<S
::Ok
, S
::Error
>
77 serializer
.serialize_str(&format
!("{self}"))
81 impl From
<std
::io
::Error
> for HLQueryError
{
82 fn from(e
: std
::io
::Error
) -> Self {
87 impl From
<a2s
::errors
::Error
> for HLQueryError
{
88 fn from(e
: a2s
::errors
::Error
) -> Self {
95 let cli
= Cli
::parse();
97 let client
= A2SClient
::new().unwrap
();
98 let query_results
: Vec
<HLQuery
> = cli
.addresses
.iter
()
100 let addresses
= arg
.to_socket_addrs();
103 .map(|lookup_result
| match lookup_result
{
104 (input
, Ok(iter_addr
)) => {
105 (input
, Ok(iter_addr
.filter
_map
(|sa
| match sa
{
106 SocketAddr
::V4(sa4
) => Some(sa4
),
110 (input
, Err(e
)) => (input
, Err(HLQueryError
::IOError(e
)))
112 .map(|(input
, address_group
)| HLQuery
::new(input
, address_group
.map(
113 |addresses
| addresses
.map(|addr
| HLQueryResult
::new(&client
, addr
)).collect())))
117 println
!("{}", serde_json
::to_string_pretty(&query_results
).unwrap
());
120 println
!("{:?}", query_results
);