Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create BoundProvider, NeoFormatter, and TypedNeoFormatter #4877

Merged
merged 31 commits into from
May 9, 2024

Conversation

sffc
Copy link
Member

@sffc sffc commented May 8, 2024

Part of #1317

This PR adds TypedNeoFormatter and NeoFormatter that use a type parameter to handle all datetime formatting.

This PR is productive because:

  1. The reduced number of APIs reduces the total amount of work we need to do to get to the finish line
  2. The resulting API will be more extensible and make easier to add additional skeleta of various shapes
  3. The stack sizes for the formatters are smaller than they have ever been

Some notes:

  • Everything will be powered through skeleta. I will make datetime lengths into skeleta, too, and get rid of that data key and separate code path.
  • To signal a key that is not needed for a particular skeleton: NeverMarker is used for the DataProvider bound, and () is used for storage types.
  • I plan to delete all of the other neo formatters once this lands, maybe in the same PR
  • After I delete the other formatters, I should be able to remove some unreachables / failure cases and internal enum variants, which will be nice. Some will be easier to eliminate than others.
  • Not sure I'm completely happy having two separate type parameters in TypedNeoFormatter (one for the calendar and one for the skeleton); happy for suggestions.
  • This same model will be useful for adding the time zone formatter.

Still draft for now because I need to make anycalendar compile. I know how to do it, it's just annoying.

Copy link
Member

@robertbastian robertbastian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some preliminary thoughts

Comment on lines +797 to +798
/// The specific type does not support this field
TypeTooNarrow(Field),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear which "type" is too "narrow". I think this is the same as UnsupportedField, we should just update the doc string to say "The formatter doesn't support this field". I don't think we need to differentiate between ICU4X not supporting a field, and a selected formatter not supporting a field, ICU4X is just the union of all formatters, but this union could grow.

_calendar: PhantomData<C>,
}

pub trait DateTimeNamesMarker {
type YearNames: MaybePayload<YearNamesV1<'static>> + fmt::Debug;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: put the Debug bound on MaybePayload.

year_symbols: OptionalNames<(), R::YearNames>,
month_symbols: OptionalNames<fields::Month, R::MonthNames>,
weekday_symbols: OptionalNames<fields::Weekday, R::WeekdayNames>,
dayperiod_symbols: OptionalNames<(), R::DayPeriodNames>,
// TODO(#4340): Make the FixedDecimalFormatter optional
fixed_decimal_formatter: Option<FixedDecimalFormatter>,
week_calculator: Option<WeekCalculator>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

week_calculator is also not needed for time formatting, can we store the raw payload as a MaybePayload?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little harder because it is cross-crate. Added a TODO.

.map_err(LoadError::Data)?;
let mut names = RawDateTimeNames::new_without_fixed_decimal_formatter();
names
.load_for_pattern::<<R::Data as TypedNeoSkeletonData<C>>::YearNamesV1Marker, <R::Data as TypedNeoSkeletonData<C>>::MonthNamesV1Marker, <R::Data as NeoSkeletonCommonData>::WeekdayNamesV1Marker, <R::Data as NeoSkeletonCommonData>::DayPeriodNamesV1Marker>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is a LoadError::MissingNames actually possible here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is narrowed down to only WeekCalculator and FixedDecimalFormatter now. I added a TODO to give those the same treatment I gave the others, and then we can eliminate this error variant.

@sffc sffc marked this pull request as ready for review May 9, 2024 17:11
Manishearth
Manishearth previously approved these changes May 9, 2024
Copy link
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discussed design in meeting today, things made sense. would be nice to further document most of the traits here by their purpose and what they interact with (if not done already)

@@ -81,86 +83,6 @@ pub trait CldrCalendar: InternalCldrCalendar {
}
}

#[cfg(feature = "experimental")]
pub(crate) trait YearNamesV1Provider<M: DataMarker> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

/// A collection of marker types associated with all calendars.
#[allow(private_bounds)] // sealed
#[cfg(any(feature = "datagen", feature = "experimental"))]
pub trait CalMarkers<M>: Sealed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: this trait feels backwards to me but i also think this is the best way to do this.

suggestion: more docs maybe, covering how it gets used?

+ DataProvider<RocDatePatternV1Marker>
+ DataProvider<RocYearNamesV1Marker>
+ DataProvider<RocMonthNamesV1Marker>
+ DataProvider<<FullData as CalMarkers<YearNamesV1Marker>>::Buddhist>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: ew. but reasonable.

/// Unlike [`DataProvider`], the provider is bound to a specific key ahead of time.
///
/// [`AnyMarker`]: crate::any::AnyMarker
pub trait BoundDataProvider<M>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(have expressed opinions about docs here on the other PR, fine to have this land as is and then do a followup)

@sffc sffc changed the title Two datetime formatters to rule them all Create BoundProvider, NeoFormatter, and TypedNeoFormatter May 9, 2024
@sffc sffc requested a review from a team as a code owner May 9, 2024 19:42
@sffc sffc merged commit 84986f7 into unicode-org:main May 9, 2024
30 checks passed
@sffc sffc deleted the datetimenames-marker branch May 9, 2024 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants