From e9817754943c0510c46ce1019044593afb9d86fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Volf?= Date: Thu, 19 Sep 2024 22:12:00 +0200 Subject: [PATCH] refactor: use one global static collator --- src/internationalization/mod.rs | 5 +++++ src/models/project.rs | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/internationalization/mod.rs b/src/internationalization/mod.rs index cc2560c..db3035f 100644 --- a/src/internationalization/mod.rs +++ b/src/internationalization/mod.rs @@ -1,12 +1,17 @@ use std::ops::Deref; use std::str::FromStr; +use std::sync::Mutex; use chrono::Locale; +use dioxus::fullstack::once_cell::sync::Lazy; use dioxus_sdk::i18n::Language; +use feruca::Collator; use unic_langid_impl::LanguageIdentifier; const EN_US: &str = include_str!("en_us.json"); const CS_CZ: &str = include_str!("cs_cz.json"); +pub(crate) static COLLATOR: Lazy> = Lazy::new(|| Mutex::new(Collator::default())); + pub(crate) fn get_languages() -> Vec { Vec::from([EN_US, CS_CZ]).into_iter().map(|texts| Language::from_str(texts).unwrap()).collect() } diff --git a/src/models/project.rs b/src/models/project.rs index 2e82637..078d283 100644 --- a/src/models/project.rs +++ b/src/models/project.rs @@ -2,9 +2,9 @@ use std::cmp::Ordering; use chrono::NaiveDateTime; use crate::schema::projects; use diesel::prelude::*; -use feruca::Collator; use serde::{Deserialize, Serialize}; use validator::Validate; +use crate::internationalization::COLLATOR; const TITLE_LENGTH_MIN: u64 = 1; const TITLE_LENGTH_MAX: u64 = 255; @@ -47,7 +47,7 @@ impl PartialOrd for Project { impl Ord for Project { fn cmp(&self, other: &Self) -> Ordering { - Collator::default().collate(self.title(), other.title()) + COLLATOR.lock().unwrap().collate(self.title(), other.title()) } }