+use std::fmt;
+use std::fmt::Display;
+use std::error::Error;
use std::env::args;
use std::net::{SocketAddr, ToSocketAddrs};
+use serde::{Serialize, Serializer};
use a2s::A2SClient;
+use crate::HLQueryError::A2SError;
+
+#[derive(Debug)]
+enum HLQueryError {
+ A2SError(a2s::errors::Error)
+}
+
+impl Display for HLQueryError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self {
+ A2SError(e) => write!(f, "{}", e)
+ }
+ }
+}
+
+impl Error for HLQueryError {}
+
+impl Serialize for HLQueryError {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.serialize_str(&format!("{self}"))
+ }
+}
+
+impl From<a2s::errors::Error> for HLQueryError {
+ fn from(e: a2s::errors::Error) -> Self {
+ Self::A2SError(e)
+ }
+}
+
fn main() {
let client = A2SClient::new().unwrap();
println!("Querying address: {}", address);
- if let Ok(result) = client.info(address) {
- println!("{}\n", serde_json::to_string_pretty(&result).unwrap());
- }
+ let result: Result<a2s::info::Info, HLQueryError> = client.info(address).map_err(From::from);
+ println!("{}\n", serde_json::to_string_pretty(&result).unwrap());
- if let Ok(result) = client.rules(address) {
- 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());
- if let Ok(result) = client.players(address) {
- println!("{}\n", serde_json::to_string_pretty(&result).unwrap());
- }
+ let result: Result<Vec<a2s::players::Player>, HLQueryError> = client.players(address).map_err(From::from);
+ println!("{}\n", serde_json::to_string_pretty(&result).unwrap());
}
}