Preserve input values and pair them to results
authorMegaBrutal <megabrutal+github@megabrutal.com>
Sat, 18 Feb 2023 23:45:00 +0000 (00:45 +0100)
committerMegaBrutal <megabrutal+github@megabrutal.com>
Sat, 18 Feb 2023 23:45:00 +0000 (00:45 +0100)
src/main.rs

index 89491c46f78fb9cb78765d1c4b659fdcbf4840f2..55406793e1eb812b171260dc5ee2d01cefdaa431 100644 (file)
@@ -26,6 +26,18 @@ impl HLQueryResult {
     }
 }
 
+#[derive(Debug, Serialize)]
+struct HLQuery {
+    input:      String,
+    result:     Result<Vec<HLQueryResult>, HLQueryError>
+}
+
+impl HLQuery {
+    fn new(input: String, result: Result<Vec<HLQueryResult>, HLQueryError>) -> Self {
+        Self { input, result }
+    }
+}
+
 #[derive(Debug)]
 enum HLQueryError {
     IOError(std::io::Error),
@@ -67,18 +79,22 @@ impl From<a2s::errors::Error> for HLQueryError {
 
 fn main() {
     let client = A2SClient::new().unwrap();
-    let query_results: Vec<Result<Vec<HLQueryResult>, HLQueryError>> = args().skip(1)
-        .map(|arg| arg.to_socket_addrs())
+    let query_results: Vec<HLQuery> = 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());