mirror of
				https://github.com/lov3b/ecb-rates.git
				synced 2025-11-03 23:00:21 +01:00 
			
		
		
		
	Break out utils
This commit is contained in:
		@@ -5,6 +5,7 @@ pub mod models;
 | 
			
		||||
pub mod os;
 | 
			
		||||
pub mod parsing;
 | 
			
		||||
pub mod table;
 | 
			
		||||
pub mod utils_calc;
 | 
			
		||||
 | 
			
		||||
pub use holiday::Hollidays;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										21
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								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<Vec<ExchangeRateResult>> {
 | 
			
		||||
    let client = Client::new();
 | 
			
		||||
@@ -14,24 +15,6 @@ async fn get_and_parse(url: impl IntoUrl) -> anyhow::Result<Vec<ExchangeRateResu
 | 
			
		||||
    parse(&xml_content)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn filter_currencies(exchange_rate_results: &mut [ExchangeRateResult], currencies: &[String]) {
 | 
			
		||||
    for exchange_rate in exchange_rate_results {
 | 
			
		||||
        let rates_ptr: *mut HashMap<String, f64> = &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();
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										21
									
								
								src/utils_calc.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/utils_calc.rs
									
									
									
									
									
										Normal file
									
								
							@@ -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<String, f64> = &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);
 | 
			
		||||
            });
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user