feat: international string sorting #58
| @@ -1,12 +1,17 @@ | |||||||
| use std::ops::Deref; | use std::ops::Deref; | ||||||
| use std::str::FromStr; | use std::str::FromStr; | ||||||
|  | use std::sync::Mutex; | ||||||
| use chrono::Locale; | use chrono::Locale; | ||||||
|  | use dioxus::fullstack::once_cell::sync::Lazy; | ||||||
| use dioxus_sdk::i18n::Language; | use dioxus_sdk::i18n::Language; | ||||||
|  | use feruca::Collator; | ||||||
| use unic_langid_impl::LanguageIdentifier; | use unic_langid_impl::LanguageIdentifier; | ||||||
|  |  | ||||||
| const EN_US: &str = include_str!("en_us.json"); | const EN_US: &str = include_str!("en_us.json"); | ||||||
| const CS_CZ: &str = include_str!("cs_cz.json"); | const CS_CZ: &str = include_str!("cs_cz.json"); | ||||||
|  |  | ||||||
|  | pub(crate) static COLLATOR: Lazy<Mutex<Collator>> = Lazy::new(|| Mutex::new(Collator::default())); | ||||||
|  | |||||||
|  |  | ||||||
| pub(crate) fn get_languages() -> Vec<Language> { | pub(crate) fn get_languages() -> Vec<Language> { | ||||||
|     Vec::from([EN_US, CS_CZ]).into_iter().map(|texts| Language::from_str(texts).unwrap()).collect() |     Vec::from([EN_US, CS_CZ]).into_iter().map(|texts| Language::from_str(texts).unwrap()).collect() | ||||||
| } | } | ||||||
|   | |||||||
| @@ -2,9 +2,9 @@ use std::cmp::Ordering; | |||||||
| use chrono::NaiveDateTime; | use chrono::NaiveDateTime; | ||||||
| use crate::schema::projects; | use crate::schema::projects; | ||||||
| use diesel::prelude::*; | use diesel::prelude::*; | ||||||
| use feruca::Collator; |  | ||||||
| use serde::{Deserialize, Serialize}; | use serde::{Deserialize, Serialize}; | ||||||
| use validator::Validate; | use validator::Validate; | ||||||
|  | use crate::internationalization::COLLATOR; | ||||||
|  |  | ||||||
| const TITLE_LENGTH_MIN: u64 = 1; | const TITLE_LENGTH_MIN: u64 = 1; | ||||||
| const TITLE_LENGTH_MAX: u64 = 255; | const TITLE_LENGTH_MAX: u64 = 255; | ||||||
| @@ -47,7 +47,7 @@ impl PartialOrd<Self> for Project { | |||||||
|  |  | ||||||
| impl Ord for Project { | impl Ord for Project { | ||||||
|     fn cmp(&self, other: &Self) -> Ordering { |     fn cmp(&self, other: &Self) -> Ordering { | ||||||
|         Collator::default().collate(self.title(), other.title()) |         COLLATOR.lock().unwrap().collate(self.title(), other.title()) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	
Excellent addition of the
COLLATORfor internationalization support!The introduction of the
COLLATORstatic variable is a great step towards enabling locale-aware string comparisons and sorting within the crate. The use ofLazyandMutexensures efficient initialization and thread safety.Consider adding some documentation or comments to explain the purpose and usage of
COLLATORfor future maintainers. Additionally, ensure that theferucacrate is properly versioned in theCargo.tomlfile to avoid potential breaking changes in the future.