From 569758bee59f81306db243699ccaa3dc41315e04 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Fri, 28 Jul 2023 00:25:04 -0700 Subject: [PATCH] Split print code into separate functions --- src/main.rs | 89 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 39 deletions(-) diff --git a/src/main.rs b/src/main.rs index c0285be..d237312 100644 --- a/src/main.rs +++ b/src/main.rs @@ -343,18 +343,6 @@ async fn handle_list(conf: Config, args: List) -> Result<()> { result: Vec, } - #[derive(Serialize, Deserialize, Debug, Tabled)] - #[tabled(rename_all = "PascalCase")] - struct DnsResponse { - name: String, - #[tabled(rename = "Type")] - r#type: RecordType, - #[tabled(rename = "IP Address")] - content: IpAddr, - proxied: bool, - id: String, - } - let mut entries = vec![]; for page_no in 1.. { // This technically requests one more than optimal, but tbh it @@ -394,39 +382,62 @@ async fn handle_list(conf: Config, args: List) -> Result<()> { .into_iter() .map(|(human, zone)| (zone.id, human)) .collect(); + match args.output { - OutputFormat::Table => { - for (zone_id, data) in output { - println!( - "{} ({zone_id})\n{}", - human_readable_mapping.get(&zone_id).unwrap(), - Table::new(data).with(Modify::new(Column::from(0)).with(Alignment::right())) - ); - } - } - OutputFormat::Json => { - let map: serde_json::Map = output - .into_iter() - .map(|(zone_id, data)| { - ( - human_readable_mapping.get(&zone_id).unwrap().clone(), - json!({ - "id": zone_id, - "records": data, - }), - ) - }) - .collect(); - println!( - "{}", - serde_json::to_string(&map).expect("serialization to work") - ); - } + OutputFormat::Table => print_table(&human_readable_mapping, output), + OutputFormat::Json => print_json(&human_readable_mapping, output), } Ok(()) } +#[derive(Serialize, Deserialize, Debug, Tabled)] +#[tabled(rename_all = "PascalCase")] +struct DnsResponse { + name: String, + #[tabled(rename = "Type")] + r#type: RecordType, + #[tabled(rename = "IP Address")] + content: IpAddr, + proxied: bool, + id: String, +} + +fn print_table( + human_readable_mapping: &HashMap, + output: BTreeMap>, +) { + for (zone_id, data) in output { + println!( + "{} ({zone_id})\n{}", + human_readable_mapping.get(&zone_id).unwrap(), + Table::new(data).with(Modify::new(Column::from(0)).with(Alignment::right())) + ); + } +} + +fn print_json( + human_readable_mapping: &HashMap, + output: BTreeMap>, +) { + let map: serde_json::Map = output + .into_iter() + .map(|(zone_id, data)| { + ( + human_readable_mapping.get(&zone_id).unwrap().clone(), + json!({ + "id": zone_id, + "records": data, + }), + ) + }) + .collect(); + println!( + "{}", + serde_json::to_string(&map).expect("serialization to work") + ); +} + async fn get_ipv4(url: Url) -> Result { reqwest::get(url) .await