diff --git a/Cargo.lock b/Cargo.lock index bca3abd..677cbf7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -437,7 +437,6 @@ dependencies = [ "chrono-tz", "csv", "js-sys", - "once_cell", "rand", "regex", "regex-lite", diff --git a/base/Cargo.toml b/base/Cargo.toml index e43def8..cb97b34 100644 --- a/base/Cargo.toml +++ b/base/Cargo.toml @@ -17,7 +17,6 @@ chrono = "0.4" chrono-tz = "0.10" regex = { version = "1.0", optional = true} regex-lite = { version = "0.1.6", optional = true} -once_cell = "1.16.0" bitcode = "0.6.3" csv = "1.3.0" diff --git a/base/src/expressions/parser/static_analysis.rs b/base/src/expressions/parser/static_analysis.rs index 280ac24..0a87447 100644 --- a/base/src/expressions/parser/static_analysis.rs +++ b/base/src/expressions/parser/static_analysis.rs @@ -2,15 +2,18 @@ use crate::functions::Function; use super::Node; -use once_cell::sync::Lazy; use regex::Regex; +use std::sync::OnceLock; + +static RE: OnceLock = OnceLock::new(); #[allow(clippy::expect_used)] -static RE: Lazy = - Lazy::new(|| Regex::new(r":[A-Z]*[0-9]*$").expect("Regex is known to be valid")); +fn get_re() -> &'static Regex { + RE.get_or_init(|| Regex::new(r":[A-Z]*[0-9]*$").expect("Regex is known to be valid")) +} fn is_range_reference(s: &str) -> bool { - RE.is_match(s) + get_re().is_match(s) } /* diff --git a/base/src/language/mod.rs b/base/src/language/mod.rs index 40fe49e..31a2a11 100644 --- a/base/src/language/mod.rs +++ b/base/src/language/mod.rs @@ -1,7 +1,6 @@ -use std::collections::HashMap; +use std::{collections::HashMap, sync::OnceLock}; use bitcode::{Decode, Encode}; -use once_cell::sync::Lazy; #[derive(Encode, Decode, Clone)] pub struct Booleans { @@ -31,14 +30,17 @@ pub struct Language { pub errors: Errors, } +static LANGUAGES: OnceLock> = OnceLock::new(); + #[allow(clippy::expect_used)] -static LANGUAGES: Lazy> = Lazy::new(|| { - bitcode::decode(include_bytes!("language.bin")).expect("Failed parsing language file") -}); +fn get_languages() -> &'static HashMap { + LANGUAGES.get_or_init(|| { + bitcode::decode(include_bytes!("language.bin")).expect("Failed parsing language file") + }) +} pub fn get_language(id: &str) -> Result<&Language, String> { - let language = LANGUAGES + get_languages() .get(id) - .ok_or(format!("Language is not supported: '{id}'"))?; - Ok(language) + .ok_or_else(|| format!("Language is not supported: '{id}'")) } diff --git a/base/src/locale/mod.rs b/base/src/locale/mod.rs index 3d7ba3c..fe584a6 100644 --- a/base/src/locale/mod.rs +++ b/base/src/locale/mod.rs @@ -1,7 +1,6 @@ -use bitcode::{Decode, Encode}; -use once_cell::sync::Lazy; +use std::{collections::HashMap, sync::OnceLock}; -use std::collections::HashMap; +use bitcode::{Decode, Encode}; #[derive(Encode, Decode, Clone)] pub struct Locale { @@ -65,12 +64,17 @@ pub struct DecimalFormats { pub standard: String, } +static LOCALES: OnceLock> = OnceLock::new(); + #[allow(clippy::expect_used)] -static LOCALES: Lazy> = - Lazy::new(|| bitcode::decode(include_bytes!("locales.bin")).expect("Failed parsing locale")); +fn get_locales() -> &'static HashMap { + LOCALES.get_or_init(|| { + bitcode::decode(include_bytes!("locales.bin")).expect("Failed parsing locale") + }) +} pub fn get_locale(id: &str) -> Result<&Locale, String> { - // TODO: pass the locale once we implement locales in Rust - let locale = LOCALES.get(id).ok_or("Invalid locale")?; - Ok(locale) + get_locales() + .get(id) + .ok_or_else(|| "Invalid locale".to_string()) }