diff --git a/components/datetime/src/provider/pattern/reference/pattern.rs b/components/datetime/src/provider/pattern/reference/pattern.rs index d2616a421e0..a97bf619fc0 100644 --- a/components/datetime/src/provider/pattern/reference/pattern.rs +++ b/components/datetime/src/provider/pattern/reference/pattern.rs @@ -25,6 +25,13 @@ pub struct Pattern { } impl Pattern { + /// Convert a [`Pattern`] to a vector of pattern items. + /// + /// The [`Pattern`] can be restored via the `From` impl. + pub fn into_items(self) -> Vec { + self.items + } + #[cfg(feature = "datagen")] pub(crate) fn items(&self) -> &[PatternItem] { &self.items @@ -39,26 +46,6 @@ impl Pattern { pub(crate) fn to_runtime_pattern(&self) -> runtime::Pattern<'static> { runtime::Pattern::from(self) } - - /// Replace fields with their equivalent canonical forms. - /// - /// For example, replace "GGG" with "G". - #[cfg(feature = "datagen")] - pub fn canonicalize(&mut self) { - use crate::fields::{Field, FieldLength, FieldSymbol}; - - for item in self.items.iter_mut() { - match item { - PatternItem::Field(ref mut field @ Field { - symbol: FieldSymbol::Era, - length: FieldLength::Three - }) => { - field.length = FieldLength::One; - } - _ => {} - } - } - } } impl From> for Pattern { diff --git a/provider/source/src/datetime/neo_skeleton.rs b/provider/source/src/datetime/neo_skeleton.rs index f45926eb460..10cd0237c31 100644 --- a/provider/source/src/datetime/neo_skeleton.rs +++ b/provider/source/src/datetime/neo_skeleton.rs @@ -578,6 +578,32 @@ mod date_skeleton_consistency_tests { length: &'a str, } + fn canonicalize_pattern(pattern: &mut reference::Pattern) { + use icu::datetime::fields::{Field, FieldLength, FieldSymbol}; + use icu::datetime::provider::pattern::PatternItem; + + let mut items = core::mem::take(pattern).into_items(); + for item in items.iter_mut() { + match item { + PatternItem::Field(ref mut field @ Field { + symbol: FieldSymbol::Era, + length: FieldLength::Three + }) => { + field.length = FieldLength::One; + } + // For now, ignore differences between 'y' and 'yy' + PatternItem::Field(ref mut field @ Field { + length: FieldLength::Two, + .. + }) => { + field.length = FieldLength::One; + } + _ => {} + } + } + *pattern = items.into(); + } + /// Returns whether the check was successful. fn check_single_pattern(data: TestCaseFixedArgs, info: TestCaseInfo) -> bool { let parsed_skeleton: reference::Pattern = info.skeleton.parse().unwrap(); @@ -590,10 +616,10 @@ mod date_skeleton_consistency_tests { // Canonicalize the two patterns to make comparison easier let mut selected_pattern = reference::Pattern::from(&selected_pattern); - selected_pattern.canonicalize(); + canonicalize_pattern(&mut selected_pattern); let selected_pattern = runtime::Pattern::from(&selected_pattern).to_string(); let mut expected_pattern: reference::Pattern = info.pattern.parse().unwrap(); - expected_pattern.canonicalize(); + canonicalize_pattern(&mut expected_pattern); let expected_pattern = runtime::Pattern::from(&expected_pattern).to_string(); if expected_pattern != selected_pattern {