diff --git a/src/lib.rs b/src/lib.rs index 8c53513..8c2f753 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ pub mod models; pub mod os; pub mod parsing; pub mod table; +pub mod utils_calc; pub use holiday::Hollidays; diff --git a/src/main.rs b/src/main.rs index 4e128bf..644ddfc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,13 @@ use clap::Parser as _; use ecb_rates::cache::{Cache, CacheLine}; use reqwest::{Client, IntoUrl}; -use std::{borrow::BorrowMut, collections::HashMap, process::ExitCode}; +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; async fn get_and_parse(url: impl IntoUrl) -> anyhow::Result> { let client = Client::new(); @@ -14,24 +15,6 @@ async fn get_and_parse(url: impl IntoUrl) -> anyhow::Result = &mut exchange_rate.rates; - exchange_rate - .rates - .keys() - .filter(|x| !currencies.contains(x)) - .for_each(|key_to_remove| { - /* This is safe, since we: - * 1. Already have a mutable reference. - * 2. Don't run the code in paralell - */ - let rates = unsafe { (*rates_ptr).borrow_mut() }; - rates.remove_entry(key_to_remove); - }); - } -} - #[tokio::main(flavor = "current_thread")] async fn main() -> ExitCode { let cli = Cli::parse(); diff --git a/src/utils_calc.rs b/src/utils_calc.rs new file mode 100644 index 0000000..f3a21cb --- /dev/null +++ b/src/utils_calc.rs @@ -0,0 +1,21 @@ +use std::{borrow::BorrowMut, collections::HashMap}; + +use crate::models::ExchangeRateResult; + +pub fn filter_currencies(exchange_rate_results: &mut [ExchangeRateResult], currencies: &[String]) { + for exchange_rate in exchange_rate_results { + let rates_ptr: *mut HashMap = &mut exchange_rate.rates; + exchange_rate + .rates + .keys() + .filter(|x| !currencies.contains(x)) + .for_each(|key_to_remove| { + /* This is safe, since we: + * 1. Already have a mutable reference. + * 2. Don't run the code in paralell + */ + let rates = unsafe { (*rates_ptr).borrow_mut() }; + rates.remove_entry(key_to_remove); + }); + } +}