diff --git a/Cargo.lock b/Cargo.lock index b139b39..196d8f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -140,6 +140,7 @@ dependencies = [ "env_logger", "exitcode", "home-config", + "lazy_static", "log", "regex", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index f70ec64..9fc6309 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ tabled = "0.16.0" thiserror = "2.0.3" thousands = "0.2.0" typetag = "0.2" +lazy_static = "1.5.0" [dev-dependencies] assert_cmd = "2.0.16" diff --git a/src/currency/fiat.rs b/src/currency/fiat.rs index c77b743..3600f2a 100644 --- a/src/currency/fiat.rs +++ b/src/currency/fiat.rs @@ -1,16 +1,20 @@ use crate::fiat_rates::blockchain_info_consumer; +use lazy_static::lazy_static; use serde::{Deserialize, Serialize}; +use std::sync::Mutex; use strum_macros::{Display, EnumString}; use crate::currency::Currency; use crate::fiat_rates::exchange_rate_provider::ExchangeRateProvider; // Static to have an easy way of caching the exchange rates. -static mut EXCHANGE_RATE_PROVIDER: ExchangeRateProvider = - ExchangeRateProvider { - data_source: blockchain_info_consumer::ApiConsumer, - data: None, - }; +lazy_static! { + static ref EXCHANGE_RATE_PROVIDER: Mutex> = + Mutex::new(ExchangeRateProvider { + data_source: blockchain_info_consumer::ApiConsumer, + data: None, + }); +} #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, EnumString, Display)] #[strum(ascii_case_insensitive, serialize_all = "UPPERCASE")] @@ -47,7 +51,10 @@ pub enum Fiat { #[typetag::serde] impl Currency for Fiat { fn btc_value(&self) -> f64 { - unsafe { EXCHANGE_RATE_PROVIDER.btc_value(self) } + EXCHANGE_RATE_PROVIDER + .lock() + .expect("Failed to lock EXCHANGE_RATE_PROVIDER") + .btc_value(self) } fn decimal_places(&self) -> u8 {