Skip to content

Commit

Permalink
More datetime polish (#5690)
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc authored Oct 16, 2024
1 parent eaacd10 commit 297e06b
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 77 deletions.
2 changes: 1 addition & 1 deletion components/datetime/src/format/neo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use super::{
GetNameForDayPeriodError, GetNameForMonthError, GetNameForWeekdayError, GetSymbolForEraError,
MonthPlaceholderValue,
};
use crate::calendar::CldrCalendar;
use crate::external_loaders::*;
use crate::fields::{self, Field, FieldLength, FieldSymbol};
use crate::helpers::size_test;
Expand All @@ -22,6 +21,7 @@ use crate::neo_skeleton::NeoDateTimeSkeleton;
use crate::pattern::PatternItem;
use crate::provider::neo::*;
use crate::provider::time_zones::tz;
use crate::scaffold::*;
use crate::time_zone::ResolvedNeoTimeZoneSkeleton;
use crate::time_zone::TimeZoneDataPayloadsBorrowed;
use core::fmt;
Expand Down
4 changes: 1 addition & 3 deletions components/datetime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@

extern crate alloc;

mod calendar;
mod error;
mod external_loaders;
pub mod fields;
Expand All @@ -108,15 +107,14 @@ pub mod options;
pub mod pattern;
pub mod provider;
pub(crate) mod raw;
pub mod scaffold;
#[doc(hidden)]
#[allow(clippy::exhaustive_structs, clippy::exhaustive_enums)] // private-ish module
#[cfg(feature = "datagen")]
pub mod skeleton;
mod time_zone;
mod tz_registry;

pub use calendar::CldrCalendar;
pub use calendar::InternalCldrCalendar;
pub use error::MismatchedCalendarError;
pub use format::datetime::DateTimeWriteError;
pub use format::neo::{FormattedDateTimePattern, LoadError, SingleLoadError, TypedDateTimeNames};
Expand Down
5 changes: 2 additions & 3 deletions components/datetime/src/neo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

//! High-level entrypoints for Neo DateTime Formatter

use crate::calendar::AnyCalendarProvider;
use crate::external_loaders::*;
use crate::format::datetime::try_write_pattern_items;
use crate::format::datetime::DateTimeWriteError;
Expand All @@ -13,7 +12,7 @@ use crate::input::ExtractedInput;
use crate::neo_marker::DateInputMarkers;
use crate::neo_marker::HasConstComponents;
use crate::neo_marker::{
AllInputMarkers, CalMarkers, ConvertCalendar, DateDataMarkers, DateTimeMarkers, GetField,
AllInputMarkers, ConvertCalendar, DateDataMarkers, DateTimeMarkers, GetField,
IsAnyCalendarKind, IsInCalendar, IsRuntimeComponents, TimeMarkers, TypedDateDataMarkers,
ZoneMarkers,
};
Expand All @@ -23,7 +22,7 @@ use crate::options::preferences::HourCycle;
use crate::provider::neo::*;
use crate::provider::ErasedPackedPatterns;
use crate::raw::neo::*;
use crate::CldrCalendar;
use crate::scaffold::*;
use crate::MismatchedCalendarError;
use core::fmt;
use core::marker::PhantomData;
Expand Down
50 changes: 19 additions & 31 deletions components/datetime/src/neo_marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ use crate::{
format::neo::*,
neo_skeleton::*,
provider::{neo::*, time_zones::tz, *},
CldrCalendar,
scaffold::*,
};
use icu_calendar::{
any_calendar::IntoAnyCalendar,
Expand All @@ -356,18 +356,6 @@ use icu_timezone::{
CustomTimeZone, CustomZonedDateTime, MetazoneId, TimeZoneBcp47Id, UtcOffset, ZoneVariant,
};

// TODO: Figure out where to export these traits
#[doc(inline)]
pub use crate::calendar::CalMarkers;
#[doc(inline)]
pub use crate::calendar::FullDataCalMarkers;
#[doc(inline)]
pub use crate::calendar::NoDataCalMarkers;

pub(crate) mod private {
pub trait Sealed {}
}

/// A type that can be converted into a specific calendar system.
pub trait ConvertCalendar {
/// The converted type. This can be the same as the receiver type.
Expand Down Expand Up @@ -993,7 +981,7 @@ pub trait HasConstZoneComponent {

/// A trait associating types for date formatting in any calendar
/// (input types only).
pub trait DateInputMarkers: private::Sealed {
pub trait DateInputMarkers: UnstableSealed {
/// Marker for resolving the year input field.
type YearInput: Into<Option<YearInfo>>;
/// Marker for resolving the month input field.
Expand All @@ -1008,7 +996,7 @@ pub trait DateInputMarkers: private::Sealed {

/// A trait associating types for date formatting in a specific calendar
/// (data markers only).
pub trait TypedDateDataMarkers<C>: private::Sealed {
pub trait TypedDateDataMarkers<C>: UnstableSealed {
/// Marker for loading date skeleton patterns.
type DateSkeletonPatternsV1Marker: DataMarker<DataStruct = PackedPatternsV1<'static>>;
/// Marker for loading year names.
Expand All @@ -1021,7 +1009,7 @@ pub trait TypedDateDataMarkers<C>: private::Sealed {

/// A trait associating types for date formatting in any calendar
/// (data markers only).
pub trait DateDataMarkers: private::Sealed {
pub trait DateDataMarkers: UnstableSealed {
/// Cross-calendar data markers for date skeleta.
type Skel: CalMarkers<ErasedPackedPatterns>;
/// Cross-calendar data markers for year names.
Expand All @@ -1034,7 +1022,7 @@ pub trait DateDataMarkers: private::Sealed {

/// A trait associating types for time formatting
/// (input types and data markers).
pub trait TimeMarkers: private::Sealed {
pub trait TimeMarkers: UnstableSealed {
/// Marker for resolving the day-of-month input field.
type HourInput: Into<Option<IsoHour>>;
/// Marker for resolving the day-of-week input field.
Expand All @@ -1051,7 +1039,7 @@ pub trait TimeMarkers: private::Sealed {

/// A trait associating types for time zone formatting
/// (input types and data markers).
pub trait ZoneMarkers: private::Sealed {
pub trait ZoneMarkers: UnstableSealed {
/// Marker for resolving the time zone offset input field.
type TimeZoneOffsetInput: Into<Option<UtcOffset>>;
/// Marker for resolving the time zone id input field.
Expand All @@ -1076,7 +1064,7 @@ pub trait ZoneMarkers: private::Sealed {

/// A trait associating constants and types implementing various other traits
/// required for datetime formatting.
pub trait DateTimeMarkers: private::Sealed + DateTimeNamesMarker {
pub trait DateTimeMarkers: UnstableSealed + DateTimeNamesMarker {
/// Associated types for date formatting.
///
/// Should implement [`DateDataMarkers`], [`TypedDateDataMarkers`], and [`DateInputMarkers`].
Expand Down Expand Up @@ -1150,7 +1138,7 @@ where
#[allow(clippy::exhaustive_enums)] // empty marker enum
pub enum NeoNeverMarker {}

impl private::Sealed for NeoNeverMarker {}
impl UnstableSealed for NeoNeverMarker {}

impl DateInputMarkers for NeoNeverMarker {
type YearInput = NeverField;
Expand Down Expand Up @@ -1215,7 +1203,7 @@ pub struct DateTimeCombo<D, T, Z> {
pub fractional_second_digits: Option<FractionalSecondDigits>,
}

impl<D, T, Z> private::Sealed for DateTimeCombo<D, T, Z> {}
impl<D, T, Z> UnstableSealed for DateTimeCombo<D, T, Z> {}

impl<D, T, Z> DateTimeCombo<D, T, Z> {
/// Creates a date/time/zone skeleton with the given formatting length.
Expand Down Expand Up @@ -1833,7 +1821,7 @@ macro_rules! impl_date_or_calendar_period_marker {
$(alignment: $option_alignment_yes,)?
$(year_style: $year_yes,)?
);
impl private::Sealed for $type {}
impl UnstableSealed for $type {}
impl DateTimeNamesMarker for $type {
type YearNames = datetime_marker_helper!(@names/year, $($years_yes)?);
type MonthNames = datetime_marker_helper!(@names/month, $($months_yes)?);
Expand Down Expand Up @@ -2057,7 +2045,7 @@ macro_rules! impl_time_marker {
alignment: yes,
$(fractional_second_digits: $nanosecond_yes,)?
);
impl private::Sealed for $type {}
impl UnstableSealed for $type {}
impl DateTimeNamesMarker for $type {
type YearNames = datetime_marker_helper!(@names/year,);
type MonthNames = datetime_marker_helper!(@names/month,);
Expand Down Expand Up @@ -2202,7 +2190,7 @@ macro_rules! impl_zone_marker {
$type,
sample_length: $sample_length,
);
impl private::Sealed for $type {}
impl UnstableSealed for $type {}
impl DateTimeNamesMarker for $type {
type YearNames = datetime_marker_helper!(@names/year,);
type MonthNames = datetime_marker_helper!(@names/month,);
Expand Down Expand Up @@ -2742,15 +2730,15 @@ impl_zoneddatetime_marker!(
);

/// Trait for components that can be formatted at runtime.
pub trait IsRuntimeComponents: private::Sealed + GetField<NeoComponents> {}
pub trait IsRuntimeComponents: UnstableSealed + GetField<NeoComponents> {}

impl GetField<NeoComponents> for NeoDateSkeleton {
fn get_field(&self) -> NeoComponents {
self.components.into()
}
}

impl private::Sealed for NeoDateSkeleton {}
impl UnstableSealed for NeoDateSkeleton {}

impl IsRuntimeComponents for NeoDateSkeleton {}

Expand Down Expand Up @@ -2805,7 +2793,7 @@ impl_get_field!(NeoDateSkeleton, length, yes);
impl_get_field!(NeoDateSkeleton, alignment, yes);
impl_get_field!(NeoDateSkeleton, year_style, yes);

impl private::Sealed for NeoCalendarPeriodSkeleton {}
impl UnstableSealed for NeoCalendarPeriodSkeleton {}

impl GetField<NeoComponents> for NeoCalendarPeriodSkeleton {
fn get_field(&self) -> NeoComponents {
Expand Down Expand Up @@ -2866,7 +2854,7 @@ impl_get_field!(NeoCalendarPeriodSkeleton, length, yes);
impl_get_field!(NeoCalendarPeriodSkeleton, alignment, yes);
impl_get_field!(NeoCalendarPeriodSkeleton, year_style, yes);

impl private::Sealed for NeoTimeSkeleton {}
impl UnstableSealed for NeoTimeSkeleton {}

impl GetField<NeoComponents> for NeoTimeSkeleton {
fn get_field(&self) -> NeoComponents {
Expand Down Expand Up @@ -2914,7 +2902,7 @@ impl_get_field!(NeoTimeSkeleton, length, yes);
impl_get_field!(NeoTimeSkeleton, alignment, yes);
impl_get_field!(NeoTimeSkeleton, fractional_second_digits, yes);

impl private::Sealed for NeoTimeZoneSkeleton {}
impl UnstableSealed for NeoTimeZoneSkeleton {}

impl GetField<NeoComponents> for NeoTimeZoneSkeleton {
fn get_field(&self) -> NeoComponents {
Expand Down Expand Up @@ -2964,7 +2952,7 @@ impl DateTimeMarkers for NeoTimeZoneSkeleton {
impl_get_field!(NeoTimeZoneSkeleton, never);
impl_get_field!(NeoTimeZoneSkeleton, length, yes);

impl private::Sealed for NeoDateTimeSkeleton {}
impl UnstableSealed for NeoDateTimeSkeleton {}

impl GetField<NeoComponents> for NeoDateTimeSkeleton {
fn get_field(&self) -> NeoComponents {
Expand Down Expand Up @@ -3004,7 +2992,7 @@ impl_get_field!(NeoDateTimeSkeleton, alignment, yes);
impl_get_field!(NeoDateTimeSkeleton, year_style, yes);
impl_get_field!(NeoDateTimeSkeleton, fractional_second_digits, yes);

impl private::Sealed for NeoSkeleton {}
impl UnstableSealed for NeoSkeleton {}

impl GetField<NeoComponents> for NeoSkeleton {
fn get_field(&self) -> NeoComponents {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

//! Scaffolding traits and impls for calendars.

use icu_calendar::any_calendar::AnyCalendarKind;
use icu_calendar::chinese::Chinese;
use icu_calendar::roc::Roc;
Expand All @@ -14,22 +16,10 @@ use icu_calendar::{
use icu_provider::prelude::*;

use crate::provider::{neo::*, *};
use crate::scaffold::UnstableSealed;
use core::marker::PhantomData;
use icu_provider::marker::NeverMarker;

/// Internal-ish trait for sealing `CldrCalendar`.
///
/// The `CldrCalendar` trait is sealed except when the `"experimental"` Cargo
/// feature is enabled. If implementing `CldrCalendar`, you must also
/// implement `UnstableCldrCalendar` and acknowledge the stability policy.
///
/// <div class="stab unstable">
/// 🚧 This trait is considered unstable; it may change at any time, in breaking or non-breaking ways,
/// including in SemVer minor releases. Do not implement this trait in userland.
/// </div>
// TODO(#4338): Decide what to do with this when we retire this crate's "experimental" feature.
pub trait InternalCldrCalendar {}

/// A calendar that can be found in CLDR
///
/// New implementors of this trait will likely also wish to modify `get_era_code_map()`
Expand All @@ -39,7 +29,7 @@ pub trait InternalCldrCalendar {}
/// 🚧 This trait is considered unstable; it may change at any time, in breaking or non-breaking ways,
/// including in SemVer minor releases. Do not implement this trait in userland.
/// </div>
pub trait CldrCalendar: InternalCldrCalendar {
pub trait CldrCalendar: UnstableSealed {
/// The data marker for loading year symbols for this calendar.
type YearNamesV1Marker: DataMarker<DataStruct = YearNamesV1<'static>>;

Expand Down Expand Up @@ -155,35 +145,31 @@ impl CldrCalendar for Roc {
type SkeletaV1Marker = RocDateNeoSkeletonPatternsV1Marker;
}

impl InternalCldrCalendar for () {}
impl InternalCldrCalendar for Buddhist {}
impl InternalCldrCalendar for Chinese {}
impl InternalCldrCalendar for Coptic {}
impl InternalCldrCalendar for Dangi {}
impl InternalCldrCalendar for Ethiopian {}
impl InternalCldrCalendar for Gregorian {}
impl InternalCldrCalendar for Hebrew {}
impl InternalCldrCalendar for Indian {}
impl InternalCldrCalendar for IslamicCivil {}
impl InternalCldrCalendar for IslamicObservational {}
impl InternalCldrCalendar for IslamicTabular {}
impl InternalCldrCalendar for IslamicUmmAlQura {}
impl InternalCldrCalendar for Japanese {}
impl InternalCldrCalendar for JapaneseExtended {}
impl InternalCldrCalendar for Persian {}
impl InternalCldrCalendar for Roc {}

mod private {
pub trait Sealed {}
}
impl UnstableSealed for () {}
impl UnstableSealed for Buddhist {}
impl UnstableSealed for Chinese {}
impl UnstableSealed for Coptic {}
impl UnstableSealed for Dangi {}
impl UnstableSealed for Ethiopian {}
impl UnstableSealed for Gregorian {}
impl UnstableSealed for Hebrew {}
impl UnstableSealed for Indian {}
impl UnstableSealed for IslamicCivil {}
impl UnstableSealed for IslamicObservational {}
impl UnstableSealed for IslamicTabular {}
impl UnstableSealed for IslamicUmmAlQura {}
impl UnstableSealed for Japanese {}
impl UnstableSealed for JapaneseExtended {}
impl UnstableSealed for Persian {}
impl UnstableSealed for Roc {}

/// A collection of marker types associated with all calendars.
///
/// This is used to group together the calendar-specific marker types that produce a common
/// [`DynamicDataMarker`]. For example, this trait can be implemented for [`YearNamesV1Marker`].
///
/// This trait serves as a building block for a cross-calendar [`BoundDataProvider`].
pub trait CalMarkers<M>: private::Sealed
pub trait CalMarkers<M>: UnstableSealed
where
M: DynamicDataMarker,
{
Expand Down Expand Up @@ -228,14 +214,14 @@ where
#[allow(clippy::exhaustive_enums)] // empty enum
pub enum FullDataCalMarkers {}

impl private::Sealed for FullDataCalMarkers {}
impl UnstableSealed for FullDataCalMarkers {}

/// Implementation of [`CalMarkers`] that includes data for no calendars.
#[derive(Debug)]
#[allow(clippy::exhaustive_enums)] // empty enum
pub enum NoDataCalMarkers {}

impl private::Sealed for NoDataCalMarkers {}
impl UnstableSealed for NoDataCalMarkers {}

impl<M> CalMarkers<M> for NoDataCalMarkers
where
Expand Down
25 changes: 25 additions & 0 deletions components/datetime/src/scaffold/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

//! Scaffolding traits and types for the datetime crate.
//!
//! Items in this module are mostly for trait bounds. Most users should not need to reference
//! these items in userland code.

mod calendar;

pub(crate) use calendar::AnyCalendarProvider;
pub use calendar::CalMarkers;
pub use calendar::CldrCalendar;
pub use calendar::FullDataCalMarkers;
pub use calendar::NoDataCalMarkers;

/// Trait marking other traits that are considered unstable and should not generally be
/// implemented outside of the datetime crate.
///
/// <div class="stab unstable">
/// 🚧 This trait is considered unstable; it may change at any time, in breaking or non-breaking ways,
/// including in SemVer minor releases. Do not implement this trait in userland.
/// </div>
pub trait UnstableSealed {}
Loading

0 comments on commit 297e06b

Please sign in to comment.