From: MegaBrutal Date: Sat, 18 Feb 2023 21:00:00 +0000 (+0100) Subject: Collect all query results into one Vec X-Git-Tag: v0.2.0~4 X-Git-Url: http://git.megabrutal.com/?p=hlquery.git;a=commitdiff_plain;h=310dd8af0c4ebf79d64ad3e12d714cc0fde9aa9e Collect all query results into one Vec --- diff --git a/src/main.rs b/src/main.rs index c62bc7f..90d51c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use std::env::args; use std::net::{SocketAddr, SocketAddrV4, ToSocketAddrs}; use serde::{Serialize, Serializer}; use a2s::A2SClient; -use crate::HLQueryError::A2SError; +use crate::HLQueryError::{IOError,A2SError}; #[derive(Debug, Serialize)] struct HLQueryResult { @@ -28,12 +28,14 @@ impl HLQueryResult { #[derive(Debug)] enum HLQueryError { + IOError(std::io::Error), A2SError(a2s::errors::Error) } impl Display for HLQueryError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { + IOError(e) => write!(f, "{}", e), A2SError(e) => write!(f, "{}", e) } } @@ -50,6 +52,12 @@ impl Serialize for HLQueryError { } } +impl From for HLQueryError { + fn from(e: std::io::Error) -> Self { + Self::IOError(e) + } +} + impl From for HLQueryError { fn from(e: a2s::errors::Error) -> Self { Self::A2SError(e) @@ -59,24 +67,22 @@ impl From for HLQueryError { fn main() { let client = A2SClient::new().unwrap(); - let addresses = args().skip(1) + let query_results: Vec, HLQueryError>> = args().skip(1) .map(|arg| arg.to_socket_addrs()) .map(|lookup_result| match lookup_result { Ok(iter_addr) => { - Ok(iter_addr.flat_map(|sa| match sa { + Ok(iter_addr.filter_map(|sa| match sa { SocketAddr::V4(sa4) => Some(sa4), _ => None })) }, + Err(e) => Err(HLQueryError::IOError(e)) + }) + .map(|addresses| match addresses { + Ok(iter_addr) => Ok(iter_addr.map(|addr| HLQueryResult::new(&client, addr)).collect()), Err(e) => Err(e) - }); - - for address in addresses { + }) + .collect(); - println!("Querying address: {:?}", address); - - let result = HLQueryResult::new(&client, address.unwrap().next().unwrap()); - println!("{}\n", serde_json::to_string_pretty(&result).unwrap()); - - } + println!("{}\n", serde_json::to_string_pretty(&query_results).unwrap()); }