mirror of
https://github.com/lov3b/ecb-rates.git
synced 2025-02-23 10:20:08 +01:00
use table
This commit is contained in:
parent
3dd3b3f207
commit
d05a1433d3
@ -14,7 +14,7 @@ pub struct Cli {
|
|||||||
pub command: FormatOption,
|
pub command: FormatOption,
|
||||||
|
|
||||||
/// Show the time in the output
|
/// Show the time in the output
|
||||||
#[arg(long = "display-time")]
|
#[arg(long = "display-time", default_value_t = true)]
|
||||||
pub display_time: bool,
|
pub display_time: bool,
|
||||||
|
|
||||||
/// Print currencies in a compact single line
|
/// Print currencies in a compact single line
|
||||||
|
43
src/main.rs
43
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::cli::{Cli, FormatOption};
|
||||||
use ecb_rates::models::ExchangeRateResult;
|
use ecb_rates::models::ExchangeRateResult;
|
||||||
use ecb_rates::parsing::parse;
|
use ecb_rates::parsing::parse;
|
||||||
|
use ecb_rates::table::Table;
|
||||||
|
|
||||||
async fn get_and_parse(url: impl IntoUrl) -> Result<Vec<ExchangeRateResult>, Box<dyn Error>> {
|
async fn get_and_parse(url: impl IntoUrl) -> Result<Vec<ExchangeRateResult>, Box<dyn Error>> {
|
||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
@ -64,42 +65,14 @@ async fn main() -> ExitCode {
|
|||||||
}
|
}
|
||||||
.expect("Failed to parse content as JSON")
|
.expect("Failed to parse content as JSON")
|
||||||
}
|
}
|
||||||
FormatOption::Plain => {
|
FormatOption::Plain => parsed
|
||||||
struct StringCur<'a> {
|
.into_iter()
|
||||||
time: &'a String,
|
.map(|x| {
|
||||||
cur: String,
|
let t: Table = x.into();
|
||||||
}
|
format!("{}", t)
|
||||||
|
})
|
||||||
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<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(&separator);
|
.join("\n"),
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
println!("{}", &output);
|
println!("{}", &output);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user