Skip to content

Conversation

@robertbastian
Copy link
Member

@robertbastian robertbastian commented Dec 15, 2025

Trying to reduce usage of AnyCalendarKind and FormattableAnyCalendarKind.

@robertbastian robertbastian marked this pull request as ready for review December 15, 2025 22:58
@sffc
Copy link
Member

sffc commented Dec 15, 2025

Can you give an overview? What problem does this solve?

/gemini review

@gemini-code-assist
Copy link

Hi @sffc, thanks for the question! This pull request, titled "Clean up FormattableAnyCalendar", aims to streamline the internal handling of calendar preferences within the datetime component.

Problem Solved:
Previously, the system used an intermediate FormattableAnyCalendarKind enum to represent calendar types. This PR refactors the code to directly leverage icu_calendar::preferences::CalendarPreferences for calendar selection and instantiation. This approach offers several benefits:

  1. Direct API Alignment: It aligns the datetime component more closely with the icu_calendar API by directly using CalendarPreferences.
  2. Simplified Logic: It removes the need for an intermediate enum, reducing potential for inconsistencies and simplifying the calendar creation logic.
  3. Improved Fallback Handling: The calendar construction methods (new, try_new_with_buffer_provider, try_new_unstable) now directly implement fallback logic for unsupported calendar algorithms (e.g., Iso8601, specific Hijri types) by modifying the CalendarPreferences and retrying, ensuring a valid calendar is always produced or a more informative error is returned.
  4. Reduced Redundancy: The FormattableAnyCalendar struct no longer stores a separate kind field, as this information can be derived directly from the wrapped AnyCalendar.
  5. Better Error Reporting: The try_from_any_calendar method now returns a Result that includes the original AnyCalendar in case of an unsupported type, providing more context for error handling.

In essence, this change makes the calendar handling more robust, maintainable, and consistent with the broader icu_calendar framework.

Regarding your /gemini review command, please note that I am currently responding to your comment. If you would like a full review of the pull request, please submit the /gemini review command in a new, separate comment.

@sffc
Copy link
Member

sffc commented Dec 15, 2025

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a nice cleanup of FormattableAnyCalendar. It simplifies the implementation by removing the UntaggedFormattableAnyCalendar wrapper and the cached kind field. The logic for selecting a calendar based on preferences is now encapsulated within the FormattableAnyCalendar constructors. The changes are consistent and improve the overall structure. I have one suggestion to reduce code duplication in components/datetime/src/scaffold/calendar.rs.

Copy link
Member

@sffc sffc left a comment

Choose a reason for hiding this comment

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

If we don't need UntaggedFormattableAnyCalendar any more, I'm in favor of that change, and would prefer to see it in a standalone PR. I'm less convinced by the motivation for the other changes.

pub(crate) selection: DateTimeZonePatternSelectionData,
pub(crate) names: RawDateTimeNames<FSet>,
pub(crate) calendar: UntaggedFormattableAnyCalendar,
pub(crate) calendar: FormattableAnyCalendar,
Copy link
Member

Choose a reason for hiding this comment

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

Observation: I think I had introduced UntaggedFormattableAnyCalendar because it reduced the stack size of DateTimeFormatter. But, the tests that enforce stack size are still passing, so maybe there's a new niche where the extra field can fit now.

Copy link
Member Author

Choose a reason for hiding this comment

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

FormattableAnyCalendar is now just untagged

Comment on lines 453 to 455
// unsupported
prefs.calendar_algorithm = None;
return Self::new(prefs);
Copy link
Member

Choose a reason for hiding this comment

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

Observation/Issue: This logic that used to live in 1 place now lives in 3 places

@robertbastian robertbastian requested a review from sffc December 16, 2025 09:33
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.

2 participants