pub struct CollationMapper { /* private fields */ }Expand description
Maps user locales to database-specific collation strings.
The mapper takes a global CollationConfig and database type, then translates
user locales (e.g., “fr-FR”) into the appropriate database-specific collation
format (e.g., “fr-FR-x-icu” for PostgreSQL with ICU).
§Examples
use fraiseql_core::config::CollationConfig;
use fraiseql_core::db::{DatabaseType, collation::CollationMapper};
// PostgreSQL with ICU
let config = CollationConfig::default();
let mapper = CollationMapper::new(config.clone(), DatabaseType::PostgreSQL);
assert_eq!(mapper.map_locale("fr-FR").unwrap(), Some("fr-FR-x-icu".to_string()));
// MySQL (general collation, not locale-specific)
let mapper = CollationMapper::new(config, DatabaseType::MySQL);
assert_eq!(mapper.map_locale("fr-FR").unwrap(), Some("utf8mb4_unicode_ci".to_string()));Implementations§
Source§impl CollationMapper
impl CollationMapper
Sourcepub fn new(config: CollationConfig, database_type: DatabaseType) -> Self
pub fn new(config: CollationConfig, database_type: DatabaseType) -> Self
Create a new collation mapper.
§Arguments
config- Global collation configurationdatabase_type- Target database type
Sourcepub fn map_locale(&self, locale: &str) -> Result<Option<String>>
pub fn map_locale(&self, locale: &str) -> Result<Option<String>>
Map user locale to database-specific collation string.
§Arguments
locale- User locale (e.g., “fr-FR”, “ja-JP”)
§Returns
Ok(Some(collation))- Database-specific collation stringOk(None)- Use database default (no COLLATE clause)Err(_)- Invalid locale when strategy isError
§Errors
Returns FraiseQLError::Validation if locale is not in allowed list
and on_invalid_locale is set to Error.
§Examples
use fraiseql_core::config::CollationConfig;
use fraiseql_core::db::{DatabaseType, collation::CollationMapper};
let config = CollationConfig::default();
let mapper = CollationMapper::new(config, DatabaseType::PostgreSQL);
// Valid locale
let collation = mapper.map_locale("fr-FR").unwrap();
assert_eq!(collation, Some("fr-FR-x-icu".to_string()));
// Invalid locale (not in allowed list)
let result = mapper.map_locale("invalid");
assert!(result.is_ok()); // Returns fallback by defaultSourcepub const fn database_type(&self) -> DatabaseType
pub const fn database_type(&self) -> DatabaseType
Get the database type this mapper is configured for.
Sourcepub const fn is_enabled(&self) -> bool
pub const fn is_enabled(&self) -> bool
Check if collation is enabled.
Auto Trait Implementations§
impl Freeze for CollationMapper
impl RefUnwindSafe for CollationMapper
impl Send for CollationMapper
impl Sync for CollationMapper
impl Unpin for CollationMapper
impl UnsafeUnpin for CollationMapper
impl UnwindSafe for CollationMapper
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more