use std::cmp::Ordering;
#[derive(Debug, Clone)]
pub struct Collator {
locale: String,
}
#[derive(Debug, Clone, Copy)]
pub struct CollationOptions {
pub strength: CollationStrength,
pub case_sensitive: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CollationStrength {
Primary,
Secondary,
Tertiary,
}
#[derive(Debug, thiserror::Error)]
pub enum CollationError {
#[error("Unsupported locale: {0}")]
UnsupportedLocale(String),
#[error("Invalid collation data")]
InvalidData,
}
impl Collator {
pub fn new(locale: &str) -> Result<Self, CollationError> {
if locale.is_empty() {
return Err(CollationError::UnsupportedLocale(locale.to_string()));
}
Ok(Collator {
locale: locale.to_string(),
})
}
pub fn with_options(locale: &str, _options: CollationOptions) -> Result<Self, CollationError> {
Self::new(locale)
}
pub fn compare(&self, a: &str, b: &str) -> Ordering {
a.cmp(b)
}
pub fn sort_key(&self, _s: &str) -> Vec<u8> {
Vec::new()
}
}
impl Default for Collator {
fn default() -> Self {
Collator {
locale: "en".to_string(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_basic_creation() {
let collator = Collator::new("en").unwrap();
assert_eq!(collator.locale, "en");
}
#[test]
fn test_compare() {
let collator = Collator::new("en").unwrap();
assert_eq!(collator.compare("a", "b"), Ordering::Less);
assert_eq!(collator.compare("b", "a"), Ordering::Greater);
assert_eq!(collator.compare("a", "a"), Ordering::Equal);
}
}