Skip to content

Commit

Permalink
Introduce ResolvedCollatorOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
hsivonen committed Nov 7, 2023
1 parent ceb22bc commit d85e6df
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 92 deletions.
4 changes: 2 additions & 2 deletions components/collator/src/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::provider::CollationJamoV1Marker;
use crate::provider::CollationMetadataV1Marker;
use crate::provider::CollationReorderingV1Marker;
use crate::provider::CollationSpecialPrimariesV1Marker;
use crate::{AlternateHandling, CollatorOptions, MaxVariable, Strength};
use crate::{AlternateHandling, CollatorOptions, MaxVariable, ResolvedCollatorOptions, Strength};
use core::cmp::Ordering;
use core::convert::TryFrom;
use icu_normalizer::provider::CanonicalDecompositionDataV1Marker;
Expand Down Expand Up @@ -249,7 +249,7 @@ impl Collator {

/// The resolved options showing how the default options, the requested options,
/// and the options from locale data were combined.
pub fn resolved_options(&self) -> CollatorOptions {
pub fn resolved_options(&self) -> ResolvedCollatorOptions {
self.options.into()
}

Expand Down
1 change: 1 addition & 0 deletions components/collator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ pub use options::CaseLevel;
pub use options::CollatorOptions;
pub use options::MaxVariable;
pub use options::Numeric;
pub use options::ResolvedCollatorOptions;
pub use options::Strength;

#[doc(no_inline)]
Expand Down
46 changes: 34 additions & 12 deletions components/collator/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,28 +351,50 @@ impl CollatorOptions {
}
}

impl From<CollatorOptionsBitField> for CollatorOptions {
fn from(options: CollatorOptionsBitField) -> CollatorOptions {
/// The resolved (actually used) options used by the collator.
///
/// See the documentation of `CollatorOptions`.
#[non_exhaustive]
#[derive(Debug, Copy, Clone)]
pub struct ResolvedCollatorOptions {
/// Resolved strength collation option.
pub strength: Strength,
/// Resolved alternate handling collation option.
pub alternate_handling: AlternateHandling,
/// Resolved case first collation option.
pub case_first: CaseFirst,
/// Resolved max variable collation option.
pub max_variable: MaxVariable,
/// Resolved case level collation option.
pub case_level: CaseLevel,
/// Resolved numeric collation option.
pub numeric: Numeric,
/// Resolved backward second level collation option.
pub backward_second_level: BackwardSecondLevel,
}

impl From<CollatorOptionsBitField> for ResolvedCollatorOptions {
fn from(options: CollatorOptionsBitField) -> ResolvedCollatorOptions {
Self {
strength: Some(options.strength()),
alternate_handling: Some(options.alternate_handling()),
case_first: Some(options.case_first()),
max_variable: Some(options.max_variable()),
case_level: Some(if options.case_level() {
strength: options.strength(),
alternate_handling: options.alternate_handling(),
case_first: options.case_first(),
max_variable: options.max_variable(),
case_level: if options.case_level() {
CaseLevel::On
} else {
CaseLevel::Off
}),
numeric: Some(if options.numeric() {
},
numeric: if options.numeric() {
Numeric::On
} else {
Numeric::Off
}),
backward_second_level: Some(if options.backward_second_level() {
},
backward_second_level: if options.backward_second_level() {
BackwardSecondLevel::On
} else {
BackwardSecondLevel::Off
}),
},
}
}
}
Expand Down
120 changes: 42 additions & 78 deletions components/collator/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1457,19 +1457,13 @@ fn test_case_level() {
fn test_default_resolved_options() {
let collator = Collator::try_new(&Default::default(), CollatorOptions::new()).unwrap();
let resolved = collator.resolved_options();
assert_eq!(resolved.strength, Some(Strength::Tertiary));
assert_eq!(
resolved.alternate_handling,
Some(AlternateHandling::NonIgnorable)
);
assert_eq!(resolved.case_first, Some(CaseFirst::Off));
assert_eq!(resolved.max_variable, Some(MaxVariable::Punctuation));
assert_eq!(resolved.case_level, Some(CaseLevel::Off));
assert_eq!(resolved.numeric, Some(Numeric::Off));
assert_eq!(
resolved.backward_second_level,
Some(BackwardSecondLevel::Off)
);
assert_eq!(resolved.strength, Strength::Tertiary);
assert_eq!(resolved.alternate_handling, AlternateHandling::NonIgnorable);
assert_eq!(resolved.case_first, CaseFirst::Off);
assert_eq!(resolved.max_variable, MaxVariable::Punctuation);
assert_eq!(resolved.case_level, CaseLevel::Off);
assert_eq!(resolved.numeric, Numeric::Off);
assert_eq!(resolved.backward_second_level, BackwardSecondLevel::Off);

assert_eq!(collator.compare("𝕒", "A"), core::cmp::Ordering::Less);
assert_eq!(collator.compare("coté", "côte"), core::cmp::Ordering::Less);
Expand All @@ -1480,19 +1474,13 @@ fn test_data_resolved_options_th() {
let locale: DataLocale = langid!("th").into();
let collator = Collator::try_new(&locale, CollatorOptions::new()).unwrap();
let resolved = collator.resolved_options();
assert_eq!(resolved.strength, Some(Strength::Tertiary));
assert_eq!(
resolved.alternate_handling,
Some(AlternateHandling::Shifted)
);
assert_eq!(resolved.case_first, Some(CaseFirst::Off));
assert_eq!(resolved.max_variable, Some(MaxVariable::Punctuation));
assert_eq!(resolved.case_level, Some(CaseLevel::Off));
assert_eq!(resolved.numeric, Some(Numeric::Off));
assert_eq!(
resolved.backward_second_level,
Some(BackwardSecondLevel::Off)
);
assert_eq!(resolved.strength, Strength::Tertiary);
assert_eq!(resolved.alternate_handling, AlternateHandling::Shifted);
assert_eq!(resolved.case_first, CaseFirst::Off);
assert_eq!(resolved.max_variable, MaxVariable::Punctuation);
assert_eq!(resolved.case_level, CaseLevel::Off);
assert_eq!(resolved.numeric, Numeric::Off);
assert_eq!(resolved.backward_second_level, BackwardSecondLevel::Off);

// There's a separate more comprehensive test for the shifted behavior
assert_eq!(collator.compare("𝕒", "A"), core::cmp::Ordering::Less);
Expand All @@ -1504,19 +1492,13 @@ fn test_data_resolved_options_da() {
let locale: DataLocale = langid!("da").into();
let collator = Collator::try_new(&locale, CollatorOptions::new()).unwrap();
let resolved = collator.resolved_options();
assert_eq!(resolved.strength, Some(Strength::Tertiary));
assert_eq!(
resolved.alternate_handling,
Some(AlternateHandling::NonIgnorable)
);
assert_eq!(resolved.case_first, Some(CaseFirst::UpperFirst));
assert_eq!(resolved.max_variable, Some(MaxVariable::Punctuation));
assert_eq!(resolved.case_level, Some(CaseLevel::Off));
assert_eq!(resolved.numeric, Some(Numeric::Off));
assert_eq!(
resolved.backward_second_level,
Some(BackwardSecondLevel::Off)
);
assert_eq!(resolved.strength, Strength::Tertiary);
assert_eq!(resolved.alternate_handling, AlternateHandling::NonIgnorable);
assert_eq!(resolved.case_first, CaseFirst::UpperFirst);
assert_eq!(resolved.max_variable, MaxVariable::Punctuation);
assert_eq!(resolved.case_level, CaseLevel::Off);
assert_eq!(resolved.numeric, Numeric::Off);
assert_eq!(resolved.backward_second_level, BackwardSecondLevel::Off);

assert_eq!(collator.compare("𝕒", "A"), core::cmp::Ordering::Greater);
assert_eq!(collator.compare("coté", "côte"), core::cmp::Ordering::Less);
Expand All @@ -1527,19 +1509,13 @@ fn test_data_resolved_options_fr_ca() {
let locale: DataLocale = langid!("fr-CA").into();
let collator = Collator::try_new(&locale, CollatorOptions::new()).unwrap();
let resolved = collator.resolved_options();
assert_eq!(resolved.strength, Some(Strength::Tertiary));
assert_eq!(
resolved.alternate_handling,
Some(AlternateHandling::NonIgnorable)
);
assert_eq!(resolved.case_first, Some(CaseFirst::Off));
assert_eq!(resolved.max_variable, Some(MaxVariable::Punctuation));
assert_eq!(resolved.case_level, Some(CaseLevel::Off));
assert_eq!(resolved.numeric, Some(Numeric::Off));
assert_eq!(
resolved.backward_second_level,
Some(BackwardSecondLevel::On)
);
assert_eq!(resolved.strength, Strength::Tertiary);
assert_eq!(resolved.alternate_handling, AlternateHandling::NonIgnorable);
assert_eq!(resolved.case_first, CaseFirst::Off);
assert_eq!(resolved.max_variable, MaxVariable::Punctuation);
assert_eq!(resolved.case_level, CaseLevel::Off);
assert_eq!(resolved.numeric, Numeric::Off);
assert_eq!(resolved.backward_second_level, BackwardSecondLevel::On);

assert_eq!(collator.compare("𝕒", "A"), core::cmp::Ordering::Less);
assert_eq!(
Expand All @@ -1557,19 +1533,13 @@ fn test_manual_and_data_resolved_options_fr_ca() {

let collator = Collator::try_new(&locale, options).unwrap();
let resolved = collator.resolved_options();
assert_eq!(resolved.strength, Some(Strength::Tertiary));
assert_eq!(
resolved.alternate_handling,
Some(AlternateHandling::NonIgnorable)
);
assert_eq!(resolved.case_first, Some(CaseFirst::UpperFirst));
assert_eq!(resolved.max_variable, Some(MaxVariable::Punctuation));
assert_eq!(resolved.case_level, Some(CaseLevel::Off));
assert_eq!(resolved.numeric, Some(Numeric::Off));
assert_eq!(
resolved.backward_second_level,
Some(BackwardSecondLevel::On)
);
assert_eq!(resolved.strength, Strength::Tertiary);
assert_eq!(resolved.alternate_handling, AlternateHandling::NonIgnorable);
assert_eq!(resolved.case_first, CaseFirst::UpperFirst);
assert_eq!(resolved.max_variable, MaxVariable::Punctuation);
assert_eq!(resolved.case_level, CaseLevel::Off);
assert_eq!(resolved.numeric, Numeric::Off);
assert_eq!(resolved.backward_second_level, BackwardSecondLevel::On);

assert_eq!(collator.compare("𝕒", "A"), core::cmp::Ordering::Greater);
assert_eq!(
Expand All @@ -1587,19 +1557,13 @@ fn test_manual_resolved_options_da() {

let collator = Collator::try_new(&locale, options).unwrap();
let resolved = collator.resolved_options();
assert_eq!(resolved.strength, Some(Strength::Tertiary));
assert_eq!(
resolved.alternate_handling,
Some(AlternateHandling::NonIgnorable)
);
assert_eq!(resolved.case_first, Some(CaseFirst::Off));
assert_eq!(resolved.max_variable, Some(MaxVariable::Punctuation));
assert_eq!(resolved.case_level, Some(CaseLevel::Off));
assert_eq!(resolved.numeric, Some(Numeric::Off));
assert_eq!(
resolved.backward_second_level,
Some(BackwardSecondLevel::Off)
);
assert_eq!(resolved.strength, Strength::Tertiary);
assert_eq!(resolved.alternate_handling, AlternateHandling::NonIgnorable);
assert_eq!(resolved.case_first, CaseFirst::Off);
assert_eq!(resolved.max_variable, MaxVariable::Punctuation);
assert_eq!(resolved.case_level, CaseLevel::Off);
assert_eq!(resolved.numeric, Numeric::Off);
assert_eq!(resolved.backward_second_level, BackwardSecondLevel::Off);

assert_eq!(collator.compare("𝕒", "A"), core::cmp::Ordering::Less);
assert_eq!(collator.compare("coté", "côte"), core::cmp::Ordering::Less);
Expand Down

0 comments on commit d85e6df

Please sign in to comment.