Skip to content

Commit 82c5d47

Browse files
authored
Improve docs and code hygiene in icu_datetime (#6498)
Toward #5634
1 parent 56506a7 commit 82c5d47

File tree

15 files changed

+85
-52
lines changed

15 files changed

+85
-52
lines changed

components/calendar/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
[package]
66
name = "icu_calendar"
7-
description = "API for supporting various types of calendars"
7+
description = "Date APIs for Gregorian and non-Gregorian calendars"
88

99
authors.workspace = true
1010
categories.workspace = true

components/datetime/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
[package]
66
name = "icu_datetime"
7-
description = "API for formatting date and time to user readable textual representation"
7+
description = "Human-readable formatting of dates, times, and time zones in hundreds of locales"
88

99
authors.workspace = true
1010
categories.workspace = true

components/datetime/src/builder.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ impl ZoneStyle {
223223
}
224224

225225
/// An error that occurs when creating a [field set](crate::fieldsets) from a builder.
226-
#[derive(Debug, displaydoc::Display)]
226+
// Not Copy: one of the variants contains a non-Copy type
227+
#[derive(Debug, Clone, displaydoc::Display)]
227228
#[ignore_extra_doc_attributes] // lines after the first won't go into `impl Display`
228229
#[non_exhaustive]
229230
pub enum BuilderError {
@@ -247,6 +248,30 @@ pub enum BuilderError {
247248
/// Superfluous options were specified.
248249
///
249250
/// For example, you cannot set a [`YearStyle`] unless the field set contains years.
251+
///
252+
/// The options that were _not_ read are returned back to the user.
253+
///
254+
/// # Examples
255+
///
256+
/// ```
257+
/// use icu::datetime::fieldsets;
258+
/// use icu::datetime::fieldsets::builder::*;
259+
/// use icu::datetime::options::*;
260+
///
261+
/// let mut builder = FieldSetBuilder::new();
262+
/// builder.length = Some(Length::Short);
263+
/// builder.time_precision = Some(TimePrecision::Minute);
264+
/// builder.year_style = Some(YearStyle::WithEra);
265+
///
266+
/// let err = builder.build_composite().unwrap_err();
267+
///
268+
/// let BuilderError::SuperfluousOptions(superfluous_options) = err else {
269+
/// panic!("error type should be SuperfluousOptions");
270+
/// };
271+
///
272+
/// assert!(superfluous_options.year_style.is_some());
273+
/// assert!(superfluous_options.time_precision.is_none());
274+
/// ```
250275
SuperfluousOptions(FieldSetBuilder),
251276
}
252277

components/datetime/src/dynamic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub enum CalendarPeriodFieldSet {
112112
/// A year, as in
113113
/// “2000”.
114114
Y(fieldsets::Y),
115-
// TODO: Add support for week-of-year
115+
// TODO(#5643): Add support for week-of-year
116116
// /// The year and week of the year, as in
117117
// /// “52nd week of 1999”.
118118
// YW(fieldsets::YW),

components/datetime/src/fieldsets.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ macro_rules! impl_date_marker {
711711
type WeekdayNamesV1 = datetime_marker_helper!(@weekdays, $($weekdays_yes)?);
712712
}
713713
impl TimeMarkers for $type_time {
714-
// TODO: Consider making dayperiods optional again
714+
// TODO(#6497): Consider making dayperiods optional
715715
type DayPeriodNamesV1 = datetime_marker_helper!(@dayperiods, yes);
716716
type TimeSkeletonPatternsV1 = datetime_marker_helper!(@times, yes);
717717
type HourInput = datetime_marker_helper!(@input/hour, yes);
@@ -1465,8 +1465,6 @@ pub mod zone {
14651465
zone_essentials = yes,
14661466
);
14671467

1468-
// TODO: Add short/long UTC offset?
1469-
14701468
impl_zone_marker!(
14711469
/// When a display name is unavailable, falls back to the location format:
14721470
///

components/datetime/src/neo.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ where
219219
///
220220
/// This ignores the `calendar_kind` preference and instead uses the static calendar type,
221221
/// and supports calendars that are not expressible as preferences, such as [`JapaneseExtended`](icu_calendar::cal::JapaneseExtended).
222+
///
223+
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
224+
///
225+
/// [📚 Help choosing a constructor](icu_provider::constructors)
222226
#[cfg(feature = "compiled_data")]
223227
pub fn try_new(
224228
prefs: DateTimeFormatterPreferences,
@@ -471,6 +475,28 @@ size_test!(
471475
/// a calendar selected at runtime.
472476
///
473477
/// For more details, please read the [crate root docs][crate].
478+
///
479+
/// # Examples
480+
///
481+
/// Basic usage:
482+
///
483+
/// ```
484+
/// use icu::datetime::fieldsets::YMD;
485+
/// use icu::datetime::input::Date;
486+
/// use icu::datetime::DateTimeFormatter;
487+
/// use icu::locale::locale;
488+
/// use writeable::assert_writeable_eq;
489+
///
490+
/// let formatter = DateTimeFormatter::try_new(
491+
/// locale!("en-u-ca-hebrew").into(),
492+
/// YMD::medium(),
493+
/// )
494+
/// .unwrap();
495+
///
496+
/// let date = Date::try_new_iso(2024, 5, 8).unwrap();
497+
///
498+
/// assert_writeable_eq!(formatter.format(&date), "30 Nisan 5784");
499+
/// ```
474500
#[doc = neo_year_month_day_formatter_size!()]
475501
#[derive(Debug, Clone)]
476502
pub struct DateTimeFormatter<FSet: DateTimeNamesMarker> {
@@ -495,30 +521,6 @@ where
495521
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
496522
///
497523
/// [📚 Help choosing a constructor](icu_provider::constructors)
498-
///
499-
/// # Examples
500-
///
501-
/// Basic usage:
502-
///
503-
/// ```
504-
/// use icu::datetime::fieldsets::YMD;
505-
/// use icu::datetime::input::Date;
506-
/// use icu::datetime::DateTimeFormatter;
507-
/// use icu::locale::locale;
508-
/// use writeable::assert_writeable_eq;
509-
///
510-
/// let formatter = DateTimeFormatter::try_new(
511-
/// locale!("en-u-ca-hebrew").into(),
512-
/// YMD::medium(),
513-
/// )
514-
/// .unwrap();
515-
///
516-
/// let date = Date::try_new_iso(2024, 5, 8).unwrap();
517-
///
518-
/// assert_writeable_eq!(formatter.format(&date), "30 Nisan 5784");
519-
/// ```
520-
///
521-
/// [`AnyCalendarKind`]: icu_calendar::AnyCalendarKind
522524
#[inline(never)]
523525
#[cfg(feature = "compiled_data")]
524526
pub fn try_new(

components/datetime/src/pattern/names.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -952,11 +952,15 @@ where
952952
FSet::Z: ZoneMarkers,
953953
FSet: GetField<CompositeFieldSet>,
954954
{
955-
/// Loads a pattern for the given field set and returns a [`FixedCalendarDateTimeFormatter`].
955+
/// Loads a pattern for the given field set with compiled data and returns a [`FixedCalendarDateTimeFormatter`].
956956
///
957957
/// The names in the current [`FixedCalendarDateTimeNames`] _must_ be sufficient for the field set.
958958
/// If not, the input object will be returned with an error.
959959
///
960+
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
961+
///
962+
/// [📚 Help choosing a constructor](icu_provider::constructors)
963+
///
960964
/// # Examples
961965
///
962966
/// ```
@@ -1154,11 +1158,15 @@ where
11541158
FSet::Z: ZoneMarkers,
11551159
FSet: GetField<CompositeFieldSet>,
11561160
{
1157-
/// Loads a pattern for the given field set and returns a [`DateTimeFormatter`].
1161+
/// Loads a pattern for the given field set with compiled data and returns a [`DateTimeFormatter`].
11581162
///
11591163
/// The names in the current [`DateTimeNames`] _must_ be sufficient for the field set.
11601164
/// If not, the input object will be returned with an error.
11611165
///
1166+
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
1167+
///
1168+
/// [📚 Help choosing a constructor](icu_provider::constructors)
1169+
///
11621170
/// # Examples
11631171
///
11641172
/// ```
@@ -1888,7 +1896,11 @@ impl<C, FSet: DateTimeNamesMarker> FixedCalendarDateTimeNames<C, FSet> {
18881896
/// names
18891897
/// .with_pattern_unchecked(&pattern)
18901898
/// .format(&zone_london_summer),
1891-
/// "Your time zone is: Greenwich Mean Time", // TODO
1899+
/// // Note: The year-round generic name of this zone is Greenwich
1900+
/// // Mean Time, which may be confusing since the zone observes
1901+
/// // daylight savings time. See:
1902+
/// // <https://unicode-org.atlassian.net/issues/CLDR-18378>
1903+
/// "Your time zone is: Greenwich Mean Time",
18921904
/// );
18931905
/// ```
18941906
#[cfg(feature = "compiled_data")]
@@ -1975,7 +1987,11 @@ impl<C, FSet: DateTimeNamesMarker> FixedCalendarDateTimeNames<C, FSet> {
19751987
/// names
19761988
/// .with_pattern_unchecked(&pattern)
19771989
/// .format(&zone_london_summer),
1978-
/// "Your time zone is: GMT", // TODO
1990+
/// // Note: The year-round generic name of this zone is Greenwich
1991+
/// // Mean Time, which may be confusing since the zone observes
1992+
/// // daylight savings time. See:
1993+
/// // <https://unicode-org.atlassian.net/issues/CLDR-18378>
1994+
/// "Your time zone is: GMT",
19791995
/// );
19801996
/// ```
19811997
#[cfg(feature = "compiled_data")]
@@ -2540,7 +2556,6 @@ impl<C: CldrCalendar, FSet: DateTimeNamesMarker> FixedCalendarDateTimeNames<C, F
25402556
&C::MonthNamesV1::bind(provider),
25412557
&WeekdayNamesV1::bind(provider),
25422558
&DayPeriodNamesV1::bind(provider),
2543-
// TODO: Consider making time zone name loading optional here (lots of data)
25442559
&tz::EssentialsV1::bind(provider),
25452560
&tz::LocationsRootV1::bind(provider),
25462561
&tz::LocationsOverrideV1::bind(provider),
@@ -3705,7 +3720,7 @@ impl RawDateTimeNamesBorrowed<'_> {
37053720
weekday_names
37063721
.names
37073722
.get((day as usize) % 7)
3708-
// TODO: make weekday_names length 7 in the type system
3723+
// Note: LinearNames does not guarantee a length of 7.
37093724
.ok_or(GetNameForWeekdayError::NotLoaded)
37103725
}
37113726

components/datetime/src/provider/fields/components.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl Bag {
142142
symbol: FieldSymbol::Year(match year {
143143
Year::Numeric | Year::TwoDigit => fields::Year::Calendar,
144144
Year::NumericWeekOf | Year::TwoDigitWeekOf => {
145-
unimplemented!("fields::Year::WeekOf")
145+
unimplemented!("#5643 fields::Year::WeekOf")
146146
}
147147
}),
148148
length: match year {

components/datetime/src/provider/fields/symbols.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,7 @@ macro_rules! field_type {
396396
);
397397
($(#[$enum_attr:meta])* $i:ident; { $( $(#[$variant_attr:meta])* $key:literal => $val:ident = $idx:expr,)* }; $($ule_name:ident)?) => (
398398
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, yoke::Yokeable, zerofrom::ZeroFrom)]
399-
// FIXME: This should be replaced with a custom derive.
400-
// See: https://github.com/unicode-org/icu4x/issues/1044
399+
// TODO(#1044): This should be replaced with a custom derive.
401400
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
402401
#[cfg_attr(feature = "datagen", databake(path = icu_datetime::fields))]
403402
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]

components/datetime/src/provider/skeleton/helpers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ fn naively_apply_time_zone_name(
142142
}
143143
}
144144

145-
// TODO - This could return a Cow<'a, Pattern>, but it affects every other part of the API to
145+
// Note: This could return a Cow<'a, Pattern>, but it affects every other part of the API to
146146
// add a lifetime here. The pattern returned here could be one that we've already constructed in
147147
// the CLDR as an exotic type, or it could be one that was modified to meet the requirements of
148148
// the components bag.

0 commit comments

Comments
 (0)