From d85e6dfd09eebf88014b71e4d0a4ef99200d818f Mon Sep 17 00:00:00 2001 From: Henri Sivonen Date: Tue, 7 Nov 2023 14:59:08 +0200 Subject: [PATCH] Introduce ResolvedCollatorOptions --- components/collator/src/comparison.rs | 4 +- components/collator/src/lib.rs | 1 + components/collator/src/options.rs | 46 +++++++--- components/collator/tests/tests.rs | 120 +++++++++----------------- 4 files changed, 79 insertions(+), 92 deletions(-) diff --git a/components/collator/src/comparison.rs b/components/collator/src/comparison.rs index fe57dc23fff..31a27cef368 100644 --- a/components/collator/src/comparison.rs +++ b/components/collator/src/comparison.rs @@ -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; @@ -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() } diff --git a/components/collator/src/lib.rs b/components/collator/src/lib.rs index 07bc959a426..23eda4d1ea2 100644 --- a/components/collator/src/lib.rs +++ b/components/collator/src/lib.rs @@ -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)] diff --git a/components/collator/src/options.rs b/components/collator/src/options.rs index 262bf177b2c..8371bf87e54 100644 --- a/components/collator/src/options.rs +++ b/components/collator/src/options.rs @@ -351,28 +351,50 @@ impl CollatorOptions { } } -impl From 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 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 - }), + }, } } } diff --git a/components/collator/tests/tests.rs b/components/collator/tests/tests.rs index f3c282a782f..85263912234 100644 --- a/components/collator/tests/tests.rs +++ b/components/collator/tests/tests.rs @@ -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); @@ -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); @@ -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); @@ -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!( @@ -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!( @@ -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);