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

Datetime: narrower internal error types #4864

Merged
merged 2 commits into from
May 7, 2024

Conversation

robertbastian
Copy link
Member

@robertbastian robertbastian commented May 3, 2024

Based on #4873

@robertbastian robertbastian force-pushed the errors branch 10 times, most recently from 95eb927 to 298c9d2 Compare May 7, 2024 14:51
sffc
sffc previously approved these changes May 7, 2024
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.

This is good; I would prefer more eliding of error conversions or not introducing all these different private error types, but it's all private and this achieves the stated goal of having a narrower error type on TryWriteable. Although it's not as narrow as I had hoped :)

Comment on lines +175 to +178
.map_err(|e| match e {
PatternForLengthError::Data(e) => DateTimeError::Data(e),
PatternForLengthError::Pattern(e) => DateTimeError::Pattern(e),
})?;
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: you have a lot of this remapping code; it would be a lot less verbose, and I think not harmful, to just implement a From impl and use ? operators.

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'm explicitly not doing that, because I will introduce more error types in the next PR and then it will become messy.

Copy link
Member Author

Choose a reason for hiding this comment

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

Also when doing this I had to recurse a lot to figure out which variants are actually feasible, I don't want the next person to have to do that as well.

Copy link
Member

Choose a reason for hiding this comment

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

It's hard for me to envision how ? operators make things more messy

Copy link
Member Author

Choose a reason for hiding this comment

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

It's unclear at the call site what happens, you don't see what the error type from the nested call is, and which error variants it has.

Copy link
Member Author

Choose a reason for hiding this comment

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

We can clean this up when we make the public-facing error changes. This code will be changed more in the short term so I'd like to keep things explicit for now.

Copy link
Member

Choose a reason for hiding this comment

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

The fact that the internal functions have narrow error types I think is more than enough explicitness. This try_write_field function is extremely complex, perhaps the most complex function in the whole library, and I think the readability of using ? is more important here. I don't care at each and every call site what the different error variants can be, as long as they map cleanly into DateTimeWriteError, which the ? operator guarantees.

@robertbastian robertbastian marked this pull request as ready for review May 7, 2024 17:55
@robertbastian robertbastian requested review from sffc and removed request for a team, zbraniecki, Manishearth and nordzilla May 7, 2024 17:55
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.

I look forward to using more ? but that is internal and I'd rather land these changes to avoid conflicts. :)

@robertbastian robertbastian merged commit 3e7da41 into unicode-org:main May 7, 2024
30 checks passed
@robertbastian robertbastian deleted the errors branch May 7, 2024 18:10
Comment on lines +351 to +353
// We don't have any calendars with < 14 days per year, and it's unlikely we'll add one
debug_assert!(day_of_year_info.day_of_year >= icu_calendar::week::MIN_UNIT_DAYS);
debug_assert!(day_of_year_info.days_in_prev_year >= icu_calendar::week::MIN_UNIT_DAYS);
Copy link
Member

Choose a reason for hiding this comment

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

Are these assertions new, or did they come from somewhere else?

#4986

Copy link
Member Author

@robertbastian robertbastian Jun 4, 2024

Choose a reason for hiding this comment

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

They protect this result case, and yes, it should have been days_in_year:

if duration_days < MIN_UNIT_DAYS {

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