UPDATE: Removes once_Cell dependency

This commit is contained in:
Nicolás Hatcher
2025-07-03 20:23:03 +02:00
committed by Nicolás Hatcher Andrés
parent 8f7798d088
commit b1327d83d4
5 changed files with 29 additions and 22 deletions

1
Cargo.lock generated
View File

@@ -437,7 +437,6 @@ dependencies = [
"chrono-tz", "chrono-tz",
"csv", "csv",
"js-sys", "js-sys",
"once_cell",
"rand", "rand",
"regex", "regex",
"regex-lite", "regex-lite",

View File

@@ -17,7 +17,6 @@ chrono = "0.4"
chrono-tz = "0.10" chrono-tz = "0.10"
regex = { version = "1.0", optional = true} regex = { version = "1.0", optional = true}
regex-lite = { version = "0.1.6", optional = true} regex-lite = { version = "0.1.6", optional = true}
once_cell = "1.16.0"
bitcode = "0.6.3" bitcode = "0.6.3"
csv = "1.3.0" csv = "1.3.0"

View File

@@ -2,15 +2,18 @@ use crate::functions::Function;
use super::Node; use super::Node;
use once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;
use std::sync::OnceLock;
static RE: OnceLock<Regex> = OnceLock::new();
#[allow(clippy::expect_used)] #[allow(clippy::expect_used)]
static RE: Lazy<Regex> = fn get_re() -> &'static Regex {
Lazy::new(|| Regex::new(r":[A-Z]*[0-9]*$").expect("Regex is known to be valid")); 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 { fn is_range_reference(s: &str) -> bool {
RE.is_match(s) get_re().is_match(s)
} }
/* /*

View File

@@ -1,7 +1,6 @@
use std::collections::HashMap; use std::{collections::HashMap, sync::OnceLock};
use bitcode::{Decode, Encode}; use bitcode::{Decode, Encode};
use once_cell::sync::Lazy;
#[derive(Encode, Decode, Clone)] #[derive(Encode, Decode, Clone)]
pub struct Booleans { pub struct Booleans {
@@ -31,14 +30,17 @@ pub struct Language {
pub errors: Errors, pub errors: Errors,
} }
static LANGUAGES: OnceLock<HashMap<String, Language>> = OnceLock::new();
#[allow(clippy::expect_used)] #[allow(clippy::expect_used)]
static LANGUAGES: Lazy<HashMap<String, Language>> = Lazy::new(|| { fn get_languages() -> &'static HashMap<String, Language> {
bitcode::decode(include_bytes!("language.bin")).expect("Failed parsing language file") LANGUAGES.get_or_init(|| {
}); bitcode::decode(include_bytes!("language.bin")).expect("Failed parsing language file")
})
}
pub fn get_language(id: &str) -> Result<&Language, String> { pub fn get_language(id: &str) -> Result<&Language, String> {
let language = LANGUAGES get_languages()
.get(id) .get(id)
.ok_or(format!("Language is not supported: '{id}'"))?; .ok_or_else(|| format!("Language is not supported: '{id}'"))
Ok(language)
} }

View File

@@ -1,7 +1,6 @@
use bitcode::{Decode, Encode}; use std::{collections::HashMap, sync::OnceLock};
use once_cell::sync::Lazy;
use std::collections::HashMap; use bitcode::{Decode, Encode};
#[derive(Encode, Decode, Clone)] #[derive(Encode, Decode, Clone)]
pub struct Locale { pub struct Locale {
@@ -65,12 +64,17 @@ pub struct DecimalFormats {
pub standard: String, pub standard: String,
} }
static LOCALES: OnceLock<HashMap<String, Locale>> = OnceLock::new();
#[allow(clippy::expect_used)] #[allow(clippy::expect_used)]
static LOCALES: Lazy<HashMap<String, Locale>> = fn get_locales() -> &'static HashMap<String, Locale> {
Lazy::new(|| bitcode::decode(include_bytes!("locales.bin")).expect("Failed parsing locale")); LOCALES.get_or_init(|| {
bitcode::decode(include_bytes!("locales.bin")).expect("Failed parsing locale")
})
}
pub fn get_locale(id: &str) -> Result<&Locale, String> { pub fn get_locale(id: &str) -> Result<&Locale, String> {
// TODO: pass the locale once we implement locales in Rust get_locales()
let locale = LOCALES.get(id).ok_or("Invalid locale")?; .get(id)
Ok(locale) .ok_or_else(|| "Invalid locale".to_string())
} }