mirror of
https://github.com/lov3b/ecb-rates.git
synced 2025-02-22 18:00:11 +01:00
Perspective
This commit is contained in:
parent
e20b7d22a5
commit
19646e0001
13
src/main.rs
13
src/main.rs
@ -1,13 +1,14 @@
|
||||
use clap::Parser as _;
|
||||
use core::error;
|
||||
use ecb_rates::cache::{Cache, CacheLine};
|
||||
use reqwest::{Client, IntoUrl};
|
||||
use reqwest::{Client, IntoUrl, StatusCode};
|
||||
use std::process::ExitCode;
|
||||
|
||||
use ecb_rates::cli::{Cli, FormatOption};
|
||||
use ecb_rates::models::ExchangeRateResult;
|
||||
use ecb_rates::parsing::parse;
|
||||
use ecb_rates::table::{TableRef, TableTrait as _};
|
||||
use ecb_rates::utils_calc::filter_currencies;
|
||||
use ecb_rates::utils_calc::{change_perspective, filter_currencies};
|
||||
|
||||
async fn get_and_parse(url: impl IntoUrl) -> anyhow::Result<Vec<ExchangeRateResult>> {
|
||||
let client = Client::new();
|
||||
@ -72,6 +73,14 @@ async fn main() -> ExitCode {
|
||||
parsed
|
||||
};
|
||||
|
||||
if let Some(currency) = cli.perspective {
|
||||
let error_occured = change_perspective(&mut parsed, ¤cy).is_none();
|
||||
if error_occured {
|
||||
eprintln!("The currency wasn't in the data from the ECB!");
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
if !cli.currencies.is_empty() {
|
||||
let currencies = cli
|
||||
.currencies
|
||||
|
@ -36,7 +36,7 @@ pub fn helper_table_print<T: TableGet>(
|
||||
|
||||
for (left, right) in table.get_rows().iter() {
|
||||
let left_str = left.as_ref();
|
||||
let right_str = right.to_string();
|
||||
let right_str = format!("{:.5}", right);
|
||||
let padding_amount = width.saturating_sub(left_str.len() + right_str.len());
|
||||
let padding = " ".repeat(padding_amount);
|
||||
writeln!(f, "{}{}{}", left_str.bold().green(), padding, right_str)?;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use std::{borrow::BorrowMut, collections::HashMap};
|
||||
use std::{borrow::BorrowMut, collections::HashMap, ops::Deref};
|
||||
|
||||
use crate::models::ExchangeRateResult;
|
||||
|
||||
@ -19,3 +19,28 @@ pub fn filter_currencies(exchange_rate_results: &mut [ExchangeRateResult], curre
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn change_perspective(
|
||||
exchange_rate_results: &mut [ExchangeRateResult],
|
||||
currency: &str,
|
||||
) -> Option<()> {
|
||||
for rate_res in exchange_rate_results {
|
||||
let currency_rate = rate_res.rates.remove(currency)?;
|
||||
let eur_rate = 1.0 / currency_rate;
|
||||
|
||||
for (_, iter_rate) in rate_res.rates.iter_mut() {
|
||||
*iter_rate = eur_rate * iter_rate.deref();
|
||||
}
|
||||
|
||||
rate_res.rates.insert("EUR".to_string(), eur_rate);
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
|
||||
pub fn invert_rates(exchange_rate_results: &mut [ExchangeRateResult]) {
|
||||
for rate_res in exchange_rate_results {
|
||||
for (_, iter_rate) in rate_res.rates.iter_mut() {
|
||||
*iter_rate = 1.0 / *iter_rate;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user