From d05a1433d3eb2abfd9c871e7d98e2002da8e21a5 Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Sat, 4 Jan 2025 20:45:30 +0100 Subject: [PATCH] use table --- src/cli.rs | 2 +- src/main.rs | 45 +++++++++------------------------------------ 2 files changed, 10 insertions(+), 37 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 362f2b4..6147b6e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -14,7 +14,7 @@ pub struct Cli { pub command: FormatOption, /// Show the time in the output - #[arg(long = "display-time")] + #[arg(long = "display-time", default_value_t = true)] pub display_time: bool, /// Print currencies in a compact single line diff --git a/src/main.rs b/src/main.rs index 9a701d2..6af0cf5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use std::{borrow::BorrowMut, collections::HashMap, error::Error, process::ExitCo use ecb_rates::cli::{Cli, FormatOption}; use ecb_rates::models::ExchangeRateResult; use ecb_rates::parsing::parse; +use ecb_rates::table::Table; async fn get_and_parse(url: impl IntoUrl) -> Result, Box> { let client = Client::new(); @@ -64,42 +65,14 @@ async fn main() -> ExitCode { } .expect("Failed to parse content as JSON") } - FormatOption::Plain => { - struct StringCur<'a> { - time: &'a String, - cur: String, - } - - let separator = if cli.compact { ", " } else { "\n" }; - - let string_curred = parsed.iter().map(|entry| { - let s = entry - .rates - .iter() - .map(|(cur, rate)| format!("{}: {}", cur, rate)) - .collect::>() - .join(&separator); - - StringCur { - time: &entry.time, - cur: s, - } - }); - - let time_sep = if cli.compact { ": " } else { "\n" }; - let mut buf = String::new(); - for sc in string_curred { - if cli.display_time { - buf.push_str(&sc.time); - buf.push_str(time_sep); - } - buf.push_str(&sc.cur); - buf.push_str(&separator); - buf.push('\n'); - } - - buf - } + FormatOption::Plain => parsed + .into_iter() + .map(|x| { + let t: Table = x.into(); + format!("{}", t) + }) + .collect::>() + .join("\n"), }; println!("{}", &output);