mirror of
https://github.com/lov3b/ecb-rates.git
synced 2025-02-22 18:00:11 +01:00
use table
This commit is contained in:
parent
3dd3b3f207
commit
d05a1433d3
@ -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
|
||||
|
45
src/main.rs
45
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<Vec<ExchangeRateResult>, Box<dyn Error>> {
|
||||
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::<Vec<_>>()
|
||||
.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::<Vec<_>>()
|
||||
.join("\n"),
|
||||
};
|
||||
|
||||
println!("{}", &output);
|
||||
|
Loading…
x
Reference in New Issue
Block a user