From 56aca74855cf59cd83ebf96a127bbd79911fd5d3 Mon Sep 17 00:00:00 2001 From: MegaBrutal Date: Sun, 19 Feb 2023 00:45:00 +0100 Subject: [PATCH] Preserve input values and pair them to results --- src/main.rs | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 89491c4..5540679 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,18 @@ impl HLQueryResult { } } +#[derive(Debug, Serialize)] +struct HLQuery { + input: String, + result: Result, HLQueryError> +} + +impl HLQuery { + fn new(input: String, result: Result, HLQueryError>) -> Self { + Self { input, result } + } +} + #[derive(Debug)] enum HLQueryError { IOError(std::io::Error), @@ -67,18 +79,22 @@ impl From for HLQueryError { fn main() { let client = A2SClient::new().unwrap(); - let query_results: Vec, HLQueryError>> = args().skip(1) - .map(|arg| arg.to_socket_addrs()) + let query_results: Vec = args().skip(1) + .map(|arg| { + let addresses = arg.to_socket_addrs(); + (arg, addresses) + }) .map(|lookup_result| match lookup_result { - Ok(iter_addr) => { - Ok(iter_addr.filter_map(|sa| match sa { + (input, Ok(iter_addr)) => { + (input, Ok(iter_addr.filter_map(|sa| match sa { SocketAddr::V4(sa4) => Some(sa4), _ => None - })) + }))) }, - Err(e) => Err(HLQueryError::IOError(e)) + (input, Err(e)) => (input, Err(HLQueryError::IOError(e))) }) - .map(|address_group| address_group.map(|addresses| addresses.map(|addr| HLQueryResult::new(&client, addr)).collect())) + .map(|(input, address_group)| HLQuery::new(input, address_group.map( + |addresses| addresses.map(|addr| HLQueryResult::new(&client, addr)).collect()))) .collect(); println!("{}\n", serde_json::to_string_pretty(&query_results).unwrap()); -- 2.34.1