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

Add Date, Time, and DateTime from str impls via IXDTF #5260

Merged
merged 46 commits into from
Jul 19, 2024

Conversation

sffc
Copy link
Member

@sffc sffc commented Jul 17, 2024

@sffc sffc marked this pull request as ready for review July 17, 2024 23:21
@sffc sffc requested review from zbraniecki, nordzilla, Manishearth and a team as code owners July 17, 2024 23:21
@sffc sffc requested review from nekevss and robertbastian and removed request for a team, zbraniecki, Manishearth and nordzilla July 17, 2024 23:21
components/calendar/src/ixdtf.rs Outdated Show resolved Hide resolved
components/calendar/src/ixdtf.rs Outdated Show resolved Hide resolved
Comment on lines 38 to 61
impl TryFrom<DateRecord> for Date<Iso> {
type Error = RangeError;
fn try_from(value: DateRecord) -> Result<Self, Self::Error> {
Self::try_new_iso_date(value.year, value.month, value.day)
}
}

impl TryFrom<TimeRecord> for Time {
type Error = RangeError;
fn try_from(value: TimeRecord) -> Result<Self, Self::Error> {
Self::try_new(value.hour, value.minute, value.second, value.nanosecond)
}
}

impl AnyCalendar {
#[cfg(feature = "compiled_data")]
fn try_from_ixdtf_record(ixdtf_record: &IxdtfParseRecord) -> Result<Self, FromIxdtfError> {
let calendar_id = ixdtf_record.calendar.unwrap_or(b"iso");
let calendar_kind = crate::AnyCalendarKind::get_for_bcp47_bytes(calendar_id)
.ok_or(FromIxdtfError::UnknownCalendar)?;
let calendar = AnyCalendar::new(calendar_kind);
Ok(calendar)
}
}
Copy link
Member

Choose a reason for hiding this comment

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

hmm the records feel like ixdtf parser implementation types, so I'd rather not expose conversions from them. Date and Time are trivial enough to be done manually, and for AnyCalendar I don't really see the use case

Copy link
Member Author

Choose a reason for hiding this comment

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

I figured since DateRecord and TimeRecord are public types, we should have public conversions. But, actually, it puts ixdtf into icu_datetime's API, so we should probably at least wait until ixdtf has a 1.0 release.

ffi/capi/Cargo.toml Outdated Show resolved Hide resolved
ffi/capi/src/errors.rs Outdated Show resolved Hide resolved
@sffc sffc requested a review from robertbastian July 19, 2024 20:57
Manishearth
Manishearth previously approved these changes Jul 19, 2024
robertbastian
robertbastian previously approved these changes Jul 19, 2024
@@ -12,7 +12,7 @@ use ixdtf::ParserError;
/// An error returned from parsing an IXDTF string to an `icu_calendar` type.
#[derive(Debug)]
#[non_exhaustive]
pub enum FromStrError {
pub enum ParseError {
/// Syntax error in the IXDTF string.
Syntax(ParserError),
Copy link
Member

Choose a reason for hiding this comment

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

follow-up: this should also be ParseError instead of ParserError.

Copy link
Member Author

Choose a reason for hiding this comment

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

#[cfg(any(
feature = "icu_datetime",
feature = "icu_timezone",
feature = "icu_calendar"
))]
pub enum CalendarFromStrError {
pub enum CalendarParseError {
Copy link
Member

Choose a reason for hiding this comment

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

I get how this name came to be, but it's still weird because we're not parsing calendars. Actually the CalendarError above should probably be called DateTimeError, as you get this when constructing a datetime fails. You get this one when parsing a datetime or date or time fails, so I think DateTimeParseError would be better. But this can be a follow-up to fix the other name as well.

Copy link
Member Author

@sffc sffc Jul 19, 2024

Choose a reason for hiding this comment

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

This all comes back to the icu_datetime vs icu_calendar crate name. The icu_datetime crate is formatting for datetimes, so that is the correct name for it. The icu_calendar crate's primary value proposition, and the vast majority of code it contains, involves non-Gregorian calendars. It also happens to export the Date and Time types that we use in icu_datetime for formatting.

By our current naming conventions, DateTimeParseError would be an error originating from parsing a string in the icu_datetime crate, but that is not what this error is.

icu_calendar is more of a utility crate, except it's not, because it depends heavily on CLDR data and algorithms. But maybe if we go with cldr_pattern and/or cldr_unit, we could rename icu_calendar to cldr_datetime or something like that.

Copy link
Member

Choose a reason for hiding this comment

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

By our current naming conventions, DateTimeParseError would be an error originating from parsing a string in the icu_datetime crate, but that is not what this error is.

Errors are not prefixed by crates anymore, errors are named for the operations where they originate.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh?

  • LocaleParseError == icu::locale::ParseError
  • FixedDecimalParseError == fixed_decimal::ParseError
  • FixedDecimalLimitError == fixed_decimal::LimitError
  • TimeZoneInvalidOffsetError == icu::timezone::InvalidOffsetError

...

Copy link
Member

@robertbastian robertbastian Jul 19, 2024

Choose a reason for hiding this comment

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

  • LocaleParseError == icu4x::Locale::from_string's error
  • FixedDecimalParseError == icu4x::FixedDecimal::from_string's error
  • FixedDecimalLimitError == icu4x::FixedDecimal::from_double's error
  • TimeZoneInvalidOffsetError == icu4x::CustomTimeZone::try_set_gmt_offset_seconds's error

...

Copy link
Member Author

Choose a reason for hiding this comment

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

@sffc
Copy link
Member Author

sffc commented Jul 19, 2024

wtf this PR was green just 10 minutes ago, now I come back and it has conflicts already? I already resolved conflicts like 2 other times today.

@sffc sffc dismissed stale reviews from robertbastian and Manishearth via db13889 July 19, 2024 21:45
@sffc sffc merged commit 9a70082 into unicode-org:main Jul 19, 2024
28 checks passed
@sffc sffc deleted the ixdtf-calendar-integration branch July 19, 2024 22:03
sffc pushed a commit that referenced this pull request Aug 6, 2024
Updating the error of `ixdtf` from `ParserError` to `ParseError` was
mentioned in #2127 via feedback from #5260.
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