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_db::CollationConfig;
use fraiseql_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 const fn new(config: CollationConfig, database_type: DatabaseType) -> Self
pub const 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_db::CollationConfig;
use fraiseql_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(), "utf8 is a valid collation: {result:?}");§Errors
Returns FraiseQLError::Validation if locale is not in the allowed list
and the configured InvalidLocaleStrategy is Reject.
Sourcepub 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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more