Skip to content

Commit

Permalink
Renames: FixedCalendarDateTimeNames, cast_into_fset (#6117)
Browse files Browse the repository at this point in the history
Fixes #6108
  • Loading branch information
sffc authored Feb 15, 2025
1 parent bb74a3f commit 59067ca
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 166 deletions.
12 changes: 6 additions & 6 deletions components/datetime/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use icu_calendar::{
use icu_provider::DataError;

#[cfg(doc)]
use crate::pattern::TypedDateTimeNames;
use crate::pattern::FixedCalendarDateTimeNames;
#[cfg(doc)]
use icu_calendar::types::YearInfo;
#[cfg(doc)]
Expand Down Expand Up @@ -90,7 +90,7 @@ pub enum DateTimeWriteError {
/// The [`DecimalFormatter`] has not been loaded.
///
/// This *only* happens if the formatter has been created using
/// [`TypedDateTimeNames::with_pattern_unchecked`], the pattern requires decimal
/// [`FixedCalendarDateTimeNames::with_pattern_unchecked`], the pattern requires decimal
/// formatting, and the decimal formatter was not loaded.
///
/// The output will contain fallback values using Latin numerals.
Expand All @@ -99,7 +99,7 @@ pub enum DateTimeWriteError {
/// The localized names for a field have not been loaded.
///
/// This *only* happens if the formatter has been created using
/// [`TypedDateTimeNames::with_pattern_unchecked`], and the pattern requires names
/// [`FixedCalendarDateTimeNames::with_pattern_unchecked`], and the pattern requires names
/// that were not loaded.
///
/// The output will contain fallback values using field identifiers (such as `tue` for `IsoWeekday::Tuesday`,
Expand All @@ -109,7 +109,7 @@ pub enum DateTimeWriteError {
/// An input field (such as "hour" or "month") is missing.
///
/// This *only* happens if the formatter has been created using
/// [`TypedDateTimeNames::with_pattern_unchecked`], and the pattern requires fields
/// [`FixedCalendarDateTimeNames::with_pattern_unchecked`], and the pattern requires fields
/// that are not returned by the input type.
///
/// The output will contain the string `{X}` instead, where `X` is the symbol for which the input is missing.
Expand All @@ -118,7 +118,7 @@ pub enum DateTimeWriteError {
/// The pattern contains a field that has a valid symbol but invalid length.
///
/// This *only* happens if the formatter has been created using
/// [`TypedDateTimeNames::with_pattern_unchecked`], and the pattern contains
/// [`FixedCalendarDateTimeNames::with_pattern_unchecked`], and the pattern contains
/// a field with a length not supported in formatting.
///
/// The output will contain fallback values similar to [`DateTimeWriteError::NamesNotLoaded`].
Expand All @@ -127,7 +127,7 @@ pub enum DateTimeWriteError {
/// Unsupported field
///
/// This *only* happens if the formatter has been created using
/// [`TypedDateTimeNames::with_pattern_unchecked`], and the pattern contains unsupported fields.
/// [`FixedCalendarDateTimeNames::with_pattern_unchecked`], and the pattern contains unsupported fields.
///
/// The output will contain the string `{unsupported:X}`, where `X` is the symbol of the unsupported field.
#[displaydoc("Unsupported field {0:?}")]
Expand Down
12 changes: 6 additions & 6 deletions components/datetime/src/neo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,20 +764,20 @@ impl<C: CldrCalendar, FSet: DateTimeMarkers> FixedCalendarDateTimeFormatter<C, F
/// );
///
/// // Make a more general formatter:
/// let general_formatter = specific_formatter.with_fset::<DateFieldSet>();
/// let general_formatter = specific_formatter.cast_into_fset::<DateFieldSet>();
///
/// // Test that it still works:
/// assert_writeable_eq!(
/// general_formatter.format(&date),
/// "20 déc. 2024"
/// );
/// ```
pub fn with_fset<FSet2: DateTimeNamesFrom<FSet>>(
pub fn cast_into_fset<FSet2: DateTimeNamesFrom<FSet>>(
self,
) -> FixedCalendarDateTimeFormatter<C, FSet2> {
FixedCalendarDateTimeFormatter {
selection: self.selection,
names: self.names.with_fset(),
names: self.names.cast_into_fset(),
_calendar: PhantomData,
}
}
Expand Down Expand Up @@ -880,18 +880,18 @@ impl<FSet: DateTimeMarkers> DateTimeFormatter<FSet> {
/// );
///
/// // Make a more general formatter:
/// let general_formatter = specific_formatter.with_fset::<DateFieldSet>();
/// let general_formatter = specific_formatter.cast_into_fset::<DateFieldSet>();
///
/// // Test that it still works:
/// assert_writeable_eq!(
/// general_formatter.format(&date),
/// "20 déc. 2024"
/// );
/// ```
pub fn with_fset<FSet2: DateTimeNamesFrom<FSet>>(self) -> DateTimeFormatter<FSet2> {
pub fn cast_into_fset<FSet2: DateTimeNamesFrom<FSet>>(self) -> DateTimeFormatter<FSet2> {
DateTimeFormatter {
selection: self.selection,
names: self.names.with_fset(),
names: self.names.cast_into_fset(),
calendar: self.calendar,
}
}
Expand Down
39 changes: 20 additions & 19 deletions components/datetime/src/pattern/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ use writeable::TryWriteable;
/// It assumes that the pattern is already localized for the customer's locale. Most clients
/// should use [`DateTimeFormatter`] instead of directly formatting with patterns.
///
/// Create one of these via factory methods on [`TypedDateTimeNames`].
/// Create one of these via factory methods on [`FixedCalendarDateTimeNames`].
///
/// [`DateTimePattern`]: super::DateTimePattern
/// [`TypedDateTimeNames`]: super::TypedDateTimeNames
/// [`FixedCalendarDateTimeNames`]: super::FixedCalendarDateTimeNames
/// [`DateTimeFormatter`]: crate::DateTimeFormatter
#[derive(Debug, Copy, Clone)]
pub struct DateTimePatternFormatter<'a, C: CldrCalendar, FSet> {
Expand Down Expand Up @@ -72,13 +72,13 @@ where
/// use icu::datetime::pattern::DateTimePattern;
/// use icu::datetime::pattern::MonthNameLength;
/// use icu::datetime::pattern::YearNameLength;
/// use icu::datetime::pattern::TypedDateTimeNames;
/// use icu::datetime::pattern::FixedCalendarDateTimeNames;
/// use icu::locale::locale;
/// use writeable::assert_try_writeable_eq;
///
/// // Create an instance that can format wide month and era names:
/// let mut names: TypedDateTimeNames<Gregorian, DateFieldSet> =
/// TypedDateTimeNames::try_new(locale!("en-GB").into()).unwrap();
/// let mut names: FixedCalendarDateTimeNames<Gregorian, DateFieldSet> =
/// FixedCalendarDateTimeNames::try_new(locale!("en-GB").into()).unwrap();
/// names
/// .include_month_names(MonthNameLength::Wide)
/// .unwrap()
Expand Down Expand Up @@ -110,14 +110,14 @@ where
/// use icu::datetime::input::Time;
/// use icu::datetime::fieldsets::enums::TimeFieldSet;
/// use icu::datetime::pattern::DateTimePattern;
/// use icu::datetime::pattern::TypedDateTimeNames;
/// use icu::datetime::pattern::FixedCalendarDateTimeNames;
/// use icu::datetime::pattern::DayPeriodNameLength;
/// use icu::locale::locale;
/// use writeable::assert_try_writeable_eq;
///
/// // Create an instance that can format abbreviated day periods:
/// let mut names: TypedDateTimeNames<Gregorian, TimeFieldSet> =
/// TypedDateTimeNames::try_new(locale!("en-US").into()).unwrap();
/// let mut names: FixedCalendarDateTimeNames<Gregorian, TimeFieldSet> =
/// FixedCalendarDateTimeNames::try_new(locale!("en-US").into()).unwrap();
/// names.include_day_period_names(DayPeriodNameLength::Abbreviated).unwrap();
///
/// // Create a pattern from a pattern string:
Expand Down Expand Up @@ -155,7 +155,7 @@ where
/// use icu::calendar::Gregorian;
/// use icu::datetime::fieldsets::enums::ZoneFieldSet;
/// use icu::datetime::pattern::DateTimePattern;
/// use icu::datetime::pattern::TypedDateTimeNames;
/// use icu::datetime::pattern::FixedCalendarDateTimeNames;
/// use icu::locale::locale;
/// use icu::datetime::input::ZonedDateTime;
/// use icu::time::zone::{IanaParser, UtcOffsetCalculator};
Expand All @@ -166,7 +166,7 @@ where
/// let mut london_summer = ZonedDateTime::try_from_str("2024-07-01T00:00:00+01:00[Europe/London]", Gregorian, IanaParser::new(), &UtcOffsetCalculator::new())
/// .unwrap();
///
/// let mut names = TypedDateTimeNames::<Gregorian, ZoneFieldSet>::try_new(
/// let mut names = FixedCalendarDateTimeNames::<Gregorian, ZoneFieldSet>::try_new(
/// locale!("en-GB").into(),
/// )
/// .unwrap();
Expand Down Expand Up @@ -242,7 +242,8 @@ mod tests {
#[test]
fn test_basic_pattern_formatting() {
let locale = locale!("en").into();
let mut names: TypedDateTimeNames<Gregorian> = TypedDateTimeNames::try_new(locale).unwrap();
let mut names: FixedCalendarDateTimeNames<Gregorian> =
FixedCalendarDateTimeNames::try_new(locale).unwrap();
names
.load_month_names(&crate::provider::Baked, MonthNameLength::Wide)
.unwrap()
Expand Down Expand Up @@ -310,8 +311,8 @@ mod tests {
length,
expected,
} = cas;
let mut names: TypedDateTimeNames<Gregorian> =
TypedDateTimeNames::try_new(locale).unwrap();
let mut names: FixedCalendarDateTimeNames<Gregorian> =
FixedCalendarDateTimeNames::try_new(locale).unwrap();
names
.load_year_names(&crate::provider::Baked, length)
.unwrap();
Expand Down Expand Up @@ -376,8 +377,8 @@ mod tests {
length,
expected,
} = cas;
let mut names: TypedDateTimeNames<Gregorian> =
TypedDateTimeNames::try_new(locale).unwrap();
let mut names: FixedCalendarDateTimeNames<Gregorian> =
FixedCalendarDateTimeNames::try_new(locale).unwrap();
names
.load_month_names(&crate::provider::Baked, length)
.unwrap();
Expand Down Expand Up @@ -481,8 +482,8 @@ mod tests {
length,
expected,
} = cas;
let mut names: TypedDateTimeNames<Gregorian> =
TypedDateTimeNames::try_new(locale).unwrap();
let mut names: FixedCalendarDateTimeNames<Gregorian> =
FixedCalendarDateTimeNames::try_new(locale).unwrap();
names
.load_weekday_names(&crate::provider::Baked, length)
.unwrap();
Expand Down Expand Up @@ -566,8 +567,8 @@ mod tests {
length,
expected,
} = cas;
let mut names: TypedDateTimeNames<Gregorian> =
TypedDateTimeNames::try_new(locale).unwrap();
let mut names: FixedCalendarDateTimeNames<Gregorian> =
FixedCalendarDateTimeNames::try_new(locale).unwrap();
names
.load_day_period_names(&crate::provider::Baked, length)
.unwrap();
Expand Down
6 changes: 3 additions & 3 deletions components/datetime/src/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ pub use formatter::FormattedDateTimePattern;
use icu_pattern::SinglePlaceholderPattern;
pub use names::DateTimeNames;
pub use names::DayPeriodNameLength;
pub use names::FixedCalendarDateTimeNames;
pub use names::MonthNameLength;
pub(crate) use names::RawDateTimeNames;
pub(crate) use names::RawDateTimeNamesBorrowed;
pub(crate) use names::TimeZoneDataPayloadsBorrowed;
pub use names::TypedDateTimeNames;
pub use names::WeekdayNameLength;
pub use names::YearNameLength;
pub use pattern::DateTimePattern;
Expand Down Expand Up @@ -64,7 +64,7 @@ pub(crate) enum MonthPlaceholderValue<'a> {
NumericPattern(&'a SinglePlaceholderPattern),
}

/// Error returned from [`TypedDateTimeNames`]'s pattern load methods.
/// Error returned from [`FixedCalendarDateTimeNames`]'s pattern load methods.
#[derive(Debug, Clone, Copy, PartialEq, displaydoc::Display)]
#[non_exhaustive]
pub enum PatternLoadError {
Expand All @@ -83,7 +83,7 @@ pub enum PatternLoadError {
/// The specific type does not support this field.
///
/// This happens for example when trying to load a month field
/// on a [`TypedDateTimeNames<Gregorian, ZoneFieldSet>`].
/// on a [`FixedCalendarDateTimeNames<Gregorian, ZoneFieldSet>`].
#[displaydoc("The specific type does not support the field {0:?}.")]
TypeTooSpecific(ErrorField),
/// An error arising from the [`data provider`](icu_provider) for loading names.
Expand Down
Loading

0 comments on commit 59067ca

Please sign in to comment.