Skip to content

Commit

Permalink
closed enum
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian committed Oct 31, 2024
1 parent fb9f30a commit 11bd918
Show file tree
Hide file tree
Showing 50 changed files with 7,783 additions and 7,808 deletions.
2 changes: 1 addition & 1 deletion components/datetime/benches/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn datetime_benches(c: &mut Criterion) {
Date::try_new_iso(2024, 1, 1).unwrap(),
Time::midnight(),
))
.with_zone_variant(ZoneVariant::standard()),
.with_zone_variant(ZoneVariant::Standard),
}
}
})
Expand Down
10 changes: 5 additions & 5 deletions components/datetime/src/fieldset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ macro_rules! impl_zone_marker {
/// let zone = TimeZoneBcp47Id(tinystr!(8, "uschi"))
/// .with_offset("-05".parse().ok())
/// .at_time((Date::try_new_iso(2022, 8, 29).unwrap(), Time::midnight()))
/// .with_zone_variant(ZoneVariant::daylight());
/// .with_zone_variant(ZoneVariant::Daylight);
///
/// assert_try_writeable_eq!(
/// fmt.convert_and_format(&zone),
Expand Down Expand Up @@ -598,7 +598,7 @@ macro_rules! impl_zone_marker {
/// let zone = TimeZoneBcp47Id(tinystr!(8, "uschi"))
/// .with_offset("-05".parse().ok())
/// .at_time((Date::try_new_iso(2022, 8, 29).unwrap(), Time::midnight()))
/// .with_zone_variant(ZoneVariant::daylight());
/// .with_zone_variant(ZoneVariant::Daylight);
///
/// assert_try_writeable_eq!(
/// fmt.format(&zone),
Expand Down Expand Up @@ -995,7 +995,7 @@ impl_zone_marker!(
/// let zone = TimeZoneBcp47Id(tinystr!(8, "brsao"))
/// .with_offset("-03".parse().ok())
/// .at_time((Date::try_new_iso(2022, 1, 29).unwrap(), Time::midnight()))
/// .with_zone_variant(ZoneVariant::standard());
/// .with_zone_variant(ZoneVariant::Standard);
///
/// assert_try_writeable_eq!(
/// fmt.format(&zone),
Expand Down Expand Up @@ -1155,7 +1155,7 @@ impl_zone_marker!(
/// let time = Time::midnight();
/// let time_zone_at_time = time_zone_basic.at_time((date, time));
///
/// let time_zone_full = time_zone_at_time.with_zone_variant(ZoneVariant::standard());
/// let time_zone_full = time_zone_at_time.with_zone_variant(ZoneVariant::Standard);
///
/// let formatter = FixedCalendarDateTimeFormatter::<(), _>::try_new(
/// &locale!("en-US").into(),
Expand Down Expand Up @@ -1214,7 +1214,7 @@ impl_zone_marker!(
/// let zone = TimeZoneBcp47Id(tinystr!(8, "brsao"))
/// .with_offset("-03".parse().ok())
/// .at_time((Date::try_new_iso(2022, 1, 29).unwrap(), Time::midnight()))
/// .with_zone_variant(ZoneVariant::standard());
/// .with_zone_variant(ZoneVariant::Standard);
///
/// assert_try_writeable_eq!(
/// fmt.format(&zone),
Expand Down
10 changes: 4 additions & 6 deletions components/datetime/src/time_zone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,12 +602,10 @@ impl FormatTimeZone for SpecificLocationFormat {
return Ok(Err(FormatTimeZoneError::Fallback));
};

if zone_variant == ZoneVariant::daylight() {
&locations.pattern_daylight
} else if zone_variant == ZoneVariant::standard() {
&locations.pattern_standard
} else {
&locations.pattern_generic
match zone_variant {
ZoneVariant::Standard => &locations.pattern_standard,
ZoneVariant::Daylight => &locations.pattern_daylight,
_ => unreachable!(),
}
.interpolate([location])
.write_to(sink)?;
Expand Down
2 changes: 1 addition & 1 deletion components/datetime/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn parse_zoned_gregorian_from_str(
date: zdt.date,
time: zdt.time,
// For fixture tests, set the zone variant to standard here
zone: zdt.zone.with_zone_variant(ZoneVariant::standard()),
zone: zdt.zone.with_zone_variant(ZoneVariant::Standard),
}
}
Err(e) => panic!("could not parse input: {input}: {e:?}"),
Expand Down
2 changes: 1 addition & 1 deletion components/timezone/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions components/timezone/src/ixdtf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,17 +310,17 @@ impl<'a> Intermediate<'a> {
{
Some(ZoneOffsets { standard, daylight }) => {
if offset == standard {
ZoneVariant::standard()
ZoneVariant::Standard
} else if Some(offset) == daylight {
ZoneVariant::daylight()
ZoneVariant::Daylight
} else {
return Err(ParseError::InvalidOffsetError);
}
}
None => {
// time_zone_id not found; Etc/Unknown?
debug_assert_eq!(time_zone_id.0.as_str(), "unk");
ZoneVariant::standard()
ZoneVariant::Standard
}
};
Ok(time_zone_id
Expand Down Expand Up @@ -431,7 +431,7 @@ impl IxdtfParser {
/// assert_eq!(zoneddatetime.time.nanosecond.number(), 0);
/// assert_eq!(zoneddatetime.zone.time_zone_id(), TimeZoneBcp47Id(tinystr!(8, "uschi")));
/// assert_eq!(zoneddatetime.zone.offset(), Some(UtcOffset::try_from_seconds(-18000).unwrap()));
/// assert_eq!(zoneddatetime.zone.zone_variant(), ZoneVariant::daylight());
/// assert_eq!(zoneddatetime.zone.zone_variant(), ZoneVariant::Daylight);
/// let (_, _) = zoneddatetime.zone.local_time();
/// ```
///
Expand Down Expand Up @@ -574,7 +574,7 @@ impl IxdtfParser {
/// assert_eq!(zoneddatetime.time.nanosecond.number(), 0);
/// assert_eq!(zoneddatetime.zone.time_zone_id(), TimeZoneBcp47Id(tinystr!(8, "uschi")));
/// assert_eq!(zoneddatetime.zone.offset(), Some(UtcOffset::try_from_seconds(-18000).unwrap()));
/// assert_eq!(zoneddatetime.zone.zone_variant(), ZoneVariant::daylight());
/// assert_eq!(zoneddatetime.zone.zone_variant(), ZoneVariant::Daylight);
/// let (_, _) = zoneddatetime.zone.local_time();
/// ```
///
Expand Down Expand Up @@ -631,7 +631,7 @@ impl IxdtfParser {
///
/// assert_eq!(consistent_tz_from_both.zone.time_zone_id(), TimeZoneBcp47Id(tinystr!(8, "uschi")));
/// assert_eq!(consistent_tz_from_both.zone.offset(), Some(UtcOffset::try_from_seconds(-18000).unwrap()));
/// assert_eq!(consistent_tz_from_both.zone.zone_variant(), ZoneVariant::daylight());
/// assert_eq!(consistent_tz_from_both.zone.zone_variant(), ZoneVariant::Daylight);
/// let (_, _) = consistent_tz_from_both.zone.local_time();
///
/// // We know that America/Los_Angeles never used a -05:00 offset at any time of the year 2024
Expand Down
2 changes: 1 addition & 1 deletion components/timezone/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
//! let time_zone_at_time = time_zone.at_time((Date::try_new_iso(2023, 12, 2).unwrap(), Time::midnight()));
//!
//! // Extend to a TimeZoneInfo<Full> by adding a zone variant
//! let time_zone_with_variant = time_zone_at_time.with_zone_variant(ZoneVariant::standard());
//! let time_zone_with_variant = time_zone_at_time.with_zone_variant(ZoneVariant::Standard);
//! ```
// https://github.com/unicode-org/icu4x/blob/main/documents/process/boilerplate.md#library-annotations
Expand Down
47 changes: 9 additions & 38 deletions components/timezone/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

use crate::error::InvalidOffsetError;
use core::str::FromStr;
use tinystr::{tinystr, TinyAsciiStr};
use zerovec::ule::{AsULE, ULE};
use zerovec::{ZeroSlice, ZeroVec};

/// An offset from Coordinated Universal Time (UTC)
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
Expand Down Expand Up @@ -182,48 +179,22 @@ impl FromStr for UtcOffset {
///
/// The semantics vary from time zone to time zone and could represent concepts
/// such as Standard time, Daylight time, Summer time, or Ramadan time.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, ULE)]
#[repr(transparent)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[zerovec::make_ule(ZoneVariantULE)]
#[repr(u8)]
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
#[cfg_attr(feature = "datagen", databake(path = icu_timezone))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[allow(clippy::exhaustive_structs)] // newtype
pub struct ZoneVariant(pub TinyAsciiStr<2>);

impl ZoneVariant {
/// Returns the variant corresponding to `"standard"` in CLDR.
#[non_exhaustive]
pub enum ZoneVariant {
/// The variant corresponding to `"standard"` in CLDR.
///
/// The semantics vary from time zone to time zone. The time zone display
/// name of this variant may or may not be called "Standard Time".
pub const fn standard() -> Self {
Self(tinystr!(2, "st"))
}
/// Returns the variant corresponding to `"daylight"` in CLDR.
Standard = 0,
/// The variant corresponding to `"daylight"` in CLDR.
///
/// The semantics vary from time zone to time zone. The time zone display
/// name of this variant may or may not be called "Daylight Time".
pub const fn daylight() -> Self {
Self(tinystr!(2, "dt"))
}
}

impl AsULE for ZoneVariant {
type ULE = Self;

#[inline]
fn to_unaligned(self) -> Self::ULE {
self
}

#[inline]
fn from_unaligned(unaligned: Self::ULE) -> Self {
unaligned
}
}

impl<'a> zerovec::maps::ZeroMapKV<'a> for ZoneVariant {
type Container = ZeroVec<'a, ZoneVariant>;
type Slice = ZeroSlice<ZoneVariant>;
type GetType = ZoneVariant;
type OwnedType = ZoneVariant;
Daylight = 1,
}
26 changes: 16 additions & 10 deletions ffi/capi/src/timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ pub mod ffi {
.unwrap_or(TimeZoneBcp47Id::unknown()),
offset: UtcOffset::try_from_seconds(offset_seconds).ok(),
zone_variant: Some(if dst {
ZoneVariant::daylight()
ZoneVariant::Daylight
} else {
ZoneVariant::standard()
ZoneVariant::Standard
}),
local_time: None,
})
Expand Down Expand Up @@ -214,9 +214,11 @@ pub mod ffi {
#[diplomat::rust_link(icu::timezone::ZoneVariant, Struct, compact)]
#[diplomat::rust_link(icu::timezone::ZoneVariant::from_str, FnInStruct, hidden)]
pub fn try_set_zone_variant(&mut self, id: &DiplomatStr) -> Option<()> {
self.zone_variant = Some(icu_timezone::ZoneVariant(
tinystr::TinyAsciiStr::try_from_utf8(id).ok()?,
));
self.zone_variant = match id {
b"st" => Some(icu_timezone::ZoneVariant::Standard),
b"dt" => Some(icu_timezone::ZoneVariant::Daylight),
_ => return None,
};
Some(())
}

Expand All @@ -233,22 +235,26 @@ pub mod ffi {
#[diplomat::rust_link(icu::timezone::ZoneVariant, Struct, compact)]
#[diplomat::attr(auto, getter)]
pub fn zone_variant(&self, write: &mut diplomat_runtime::DiplomatWrite) -> Option<()> {
let _infallible = write.write_str(self.zone_variant?.0.as_str());
let _infallible = write.write_str(match self.zone_variant {
Some(icu_timezone::ZoneVariant::Daylight) => "dt",
Some(icu_timezone::ZoneVariant::Standard) => "st",
_ => return None,
});
Some(())
}

/// Sets the `zone_variant` field to "standard" time, which may or may
/// not correspond to a display name with "Standard" in its name.
#[diplomat::rust_link(icu::timezone::ZoneVariant::standard, FnInStruct)]
pub fn set_standard_time(&mut self) {
self.zone_variant = Some(icu_timezone::ZoneVariant::standard())
self.zone_variant = Some(icu_timezone::ZoneVariant::Standard)
}

/// Sets the `zone_variant` field to "daylight" time, which may or may
/// not correspond to a display name with "Daylight" in its name.
#[diplomat::rust_link(icu::timezone::ZoneVariant::daylight, FnInStruct)]
pub fn set_daylight_time(&mut self) {
self.zone_variant = Some(icu_timezone::ZoneVariant::daylight())
self.zone_variant = Some(icu_timezone::ZoneVariant::Daylight)
}

/// Returns whether the `zone_variant` field is standard time.
Expand All @@ -258,7 +264,7 @@ pub mod ffi {
#[diplomat::rust_link(icu::timezone::TimeZoneInfo::zone_variant, FnInStruct, compact)]
#[diplomat::attr(auto, getter)]
pub fn is_standard_time(&self) -> Option<bool> {
Some(self.zone_variant? == icu_timezone::ZoneVariant::standard())
Some(self.zone_variant? == icu_timezone::ZoneVariant::Standard)
}

/// Returns whether the `zone_variant` field is daylight time.
Expand All @@ -268,7 +274,7 @@ pub mod ffi {
#[diplomat::rust_link(icu::timezone::TimeZoneInfo::zone_variant, FnInStruct, compact)]
#[diplomat::attr(auto, getter)]
pub fn is_daylight_time(&self) -> Option<bool> {
Some(self.zone_variant? == icu_timezone::ZoneVariant::daylight())
Some(self.zone_variant? == icu_timezone::ZoneVariant::Daylight)
}

/// Sets the `local_time` field.
Expand Down
4 changes: 2 additions & 2 deletions ffi/capi/src/zoned_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub mod ffi {
.time_zone_id
.with_offset(time_zone.offset)
.at_time((datetime.0.date, datetime.0.time))
.with_zone_variant(time_zone.zone_variant.unwrap_or(ZoneVariant::standard())),
.with_zone_variant(time_zone.zone_variant.unwrap_or(ZoneVariant::Standard)),
};
let _infallible = self.0.format(&zdt).try_write_to(write);
Ok(())
Expand Down Expand Up @@ -145,7 +145,7 @@ pub mod ffi {
.time_zone_id
.with_offset(time_zone.offset)
.at_time((datetime.0.date, datetime.0.time))
.with_zone_variant(time_zone.zone_variant.unwrap_or(ZoneVariant::standard())),
.with_zone_variant(time_zone.zone_variant.unwrap_or(ZoneVariant::Standard)),
};
let _infallible = self.0.convert_and_format(&zdt).try_write_to(write);
Ok(())
Expand Down
Loading

0 comments on commit 11bd918

Please sign in to comment.