Skip to content

Commit 3a8c258

Browse files
authored
Clean up API ergonomics for calendar methods (#235)
This PR removes a lot of the `TemporalResult` returns from most of the calendar methods. This is made possible on most methods because we no longer need to support Not Yet Implemented errors. There is also an expect added in the conversion from `IsoDate` -> `icu_calendar::Date<Iso>`. This is tested to be valid in a test added to `iso.rs` for the max bounds of `IsoDate`. The core changes in this PR are made to `iso.rs` and `calendar.rs` by extension. The rest is mostly updating all the calendar methods accordingly. Note: calendar methods that are still not fully implemented across various calendars were left as a `TemporalResult`
1 parent 4b0aa9b commit 3a8c258

24 files changed

+424
-486
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ let iso8601_date = PlainDate::try_new(2025, 3, 3, Calendar::default()).unwrap();
2020

2121
// Create a new date with the japanese calendar
2222
let japanese_date = iso8601_date.with_calendar(Calendar::from_str("japanese").unwrap()).unwrap();
23-
let current_era = japanese_date.era().expect("current date converts between both calendars");
24-
assert_eq!(current_era, Some(tinystr!(16, "reiwa")));
23+
assert_eq!(japanese_date.era(), Some(tinystr!(16, "reiwa")));
2524
assert_eq!(japanese_date.era_year().unwrap(), Some(7));
2625
assert_eq!(japanese_date.month().unwrap(), 3)
2726
```

src/builtins/core/calendar.rs

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -363,83 +363,83 @@ impl Calendar {
363363
}
364364

365365
/// `CalendarEra`
366-
pub fn era(&self, iso_date: &IsoDate) -> TemporalResult<Option<TinyAsciiStr<16>>> {
366+
pub fn era(&self, iso_date: &IsoDate) -> Option<TinyAsciiStr<16>> {
367367
if self.is_iso() {
368-
return Ok(None);
368+
return None;
369369
}
370-
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
371-
Ok(self.0.year(&calendar_date).standard_era().map(|era| era.0))
370+
let calendar_date = self.0.date_from_iso(iso_date.to_icu4x());
371+
self.0.year(&calendar_date).standard_era().map(|era| era.0)
372372
}
373373

374374
/// `CalendarEraYear`
375-
pub fn era_year(&self, iso_date: &IsoDate) -> TemporalResult<Option<i32>> {
375+
pub fn era_year(&self, iso_date: &IsoDate) -> Option<i32> {
376376
if self.is_iso() {
377-
return Ok(None);
377+
return None;
378378
}
379-
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
380-
Ok(self.0.year(&calendar_date).era_year())
379+
let calendar_date = self.0.date_from_iso(iso_date.to_icu4x());
380+
self.0.year(&calendar_date).era_year()
381381
}
382382

383383
/// `CalendarYear`
384-
pub fn year(&self, iso_date: &IsoDate) -> TemporalResult<i32> {
384+
pub fn year(&self, iso_date: &IsoDate) -> i32 {
385385
if self.is_iso() {
386-
return Ok(iso_date.year);
386+
return iso_date.year;
387387
}
388-
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
389-
Ok(self.0.year(&calendar_date).extended_year)
388+
let calendar_date = self.0.date_from_iso(iso_date.to_icu4x());
389+
self.0.year(&calendar_date).extended_year
390390
}
391391

392392
/// `CalendarMonth`
393-
pub fn month(&self, iso_date: &IsoDate) -> TemporalResult<u8> {
393+
pub fn month(&self, iso_date: &IsoDate) -> u8 {
394394
if self.is_iso() {
395-
return Ok(iso_date.month);
395+
return iso_date.month;
396396
}
397-
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
398-
Ok(self.0.month(&calendar_date).month_number())
397+
let calendar_date = self.0.date_from_iso(iso_date.to_icu4x());
398+
self.0.month(&calendar_date).month_number()
399399
}
400400

401401
/// `CalendarMonthCode`
402-
pub fn month_code(&self, iso_date: &IsoDate) -> TemporalResult<MonthCode> {
402+
pub fn month_code(&self, iso_date: &IsoDate) -> MonthCode {
403403
if self.is_iso() {
404-
let mc = iso_date.as_icu4x()?.month().standard_code.0;
405-
return Ok(MonthCode(mc));
404+
let mc = iso_date.to_icu4x().month().standard_code.0;
405+
return MonthCode(mc);
406406
}
407-
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
408-
Ok(MonthCode(self.0.month(&calendar_date).standard_code.0))
407+
let calendar_date = self.0.date_from_iso(iso_date.to_icu4x());
408+
MonthCode(self.0.month(&calendar_date).standard_code.0)
409409
}
410410

411411
/// `CalendarDay`
412-
pub fn day(&self, iso_date: &IsoDate) -> TemporalResult<u8> {
412+
pub fn day(&self, iso_date: &IsoDate) -> u8 {
413413
if self.is_iso() {
414-
return Ok(iso_date.day);
414+
return iso_date.day;
415415
}
416-
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
417-
Ok(self.0.day_of_month(&calendar_date).0)
416+
let calendar_date = self.0.date_from_iso(iso_date.to_icu4x());
417+
self.0.day_of_month(&calendar_date).0
418418
}
419419

420420
/// `CalendarDayOfWeek`
421-
pub fn day_of_week(&self, iso_date: &IsoDate) -> TemporalResult<u16> {
421+
pub fn day_of_week(&self, iso_date: &IsoDate) -> u16 {
422422
if self.is_iso() {
423-
return Ok(iso_date.as_icu4x()?.day_of_week() as u16);
423+
return iso_date.to_icu4x().day_of_week() as u16;
424424
}
425-
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
425+
let calendar_date = self.0.date_from_iso(iso_date.to_icu4x());
426426
// TODO: Understand ICU4X's decision for `IsoWeekDay` to be `i8`
427-
Ok(self.0.day_of_week(&calendar_date) as u16)
427+
self.0.day_of_week(&calendar_date) as u16
428428
}
429429

430430
/// `CalendarDayOfYear`
431-
pub fn day_of_year(&self, iso_date: &IsoDate) -> TemporalResult<u16> {
431+
pub fn day_of_year(&self, iso_date: &IsoDate) -> u16 {
432432
if self.is_iso() {
433-
return Ok(iso_date.as_icu4x()?.day_of_year_info().day_of_year);
433+
return iso_date.to_icu4x().day_of_year_info().day_of_year;
434434
}
435-
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
436-
Ok(self.0.day_of_year_info(&calendar_date).day_of_year)
435+
let calendar_date = self.0.date_from_iso(iso_date.to_icu4x());
436+
self.0.day_of_year_info(&calendar_date).day_of_year
437437
}
438438

439439
/// `CalendarWeekOfYear`
440440
pub fn week_of_year(&self, iso_date: &IsoDate) -> TemporalResult<Option<u16>> {
441441
if self.is_iso() {
442-
let date = iso_date.as_icu4x()?;
442+
let date = iso_date.to_icu4x();
443443
let week_calculator = WeekCalculator::default();
444444
let week_of = date.week_of_year(&week_calculator);
445445
return Ok(Some(week_of.week as u16));
@@ -451,7 +451,7 @@ impl Calendar {
451451
/// `CalendarYearOfWeek`
452452
pub fn year_of_week(&self, iso_date: &IsoDate) -> TemporalResult<Option<i32>> {
453453
if self.is_iso() {
454-
let date = iso_date.as_icu4x()?;
454+
let date = iso_date.to_icu4x();
455455

456456
let week_calculator = WeekCalculator::default();
457457

@@ -477,39 +477,39 @@ impl Calendar {
477477
}
478478

479479
/// `CalendarDaysInMonth`
480-
pub fn days_in_month(&self, iso_date: &IsoDate) -> TemporalResult<u16> {
480+
pub fn days_in_month(&self, iso_date: &IsoDate) -> u16 {
481481
if self.is_iso() {
482-
return Ok(iso_date.as_icu4x()?.days_in_month() as u16);
482+
return iso_date.to_icu4x().days_in_month() as u16;
483483
}
484-
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
485-
Ok(self.0.days_in_month(&calendar_date) as u16)
484+
let calendar_date = self.0.date_from_iso(iso_date.to_icu4x());
485+
self.0.days_in_month(&calendar_date) as u16
486486
}
487487

488488
/// `CalendarDaysInYear`
489-
pub fn days_in_year(&self, iso_date: &IsoDate) -> TemporalResult<u16> {
489+
pub fn days_in_year(&self, iso_date: &IsoDate) -> u16 {
490490
if self.is_iso() {
491-
return Ok(iso_date.as_icu4x()?.days_in_year());
491+
return iso_date.to_icu4x().days_in_year();
492492
}
493-
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
494-
Ok(self.0.days_in_year(&calendar_date))
493+
let calendar_date = self.0.date_from_iso(iso_date.to_icu4x());
494+
self.0.days_in_year(&calendar_date)
495495
}
496496

497497
/// `CalendarMonthsInYear`
498-
pub fn months_in_year(&self, iso_date: &IsoDate) -> TemporalResult<u16> {
498+
pub fn months_in_year(&self, iso_date: &IsoDate) -> u16 {
499499
if self.is_iso() {
500-
return Ok(12);
500+
return 12;
501501
}
502-
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
503-
Ok(self.0.months_in_year(&calendar_date) as u16)
502+
let calendar_date = self.0.date_from_iso(iso_date.to_icu4x());
503+
self.0.months_in_year(&calendar_date) as u16
504504
}
505505

506506
/// `CalendarInLeapYear`
507-
pub fn in_leap_year(&self, iso_date: &IsoDate) -> TemporalResult<bool> {
507+
pub fn in_leap_year(&self, iso_date: &IsoDate) -> bool {
508508
if self.is_iso() {
509-
return Ok(iso_date.as_icu4x()?.is_in_leap_year());
509+
return iso_date.to_icu4x().is_in_leap_year();
510510
}
511-
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
512-
Ok(self.0.is_in_leap_year(&calendar_date))
511+
let calendar_date = self.0.date_from_iso(iso_date.to_icu4x());
512+
self.0.is_in_leap_year(&calendar_date)
513513
}
514514

515515
/// Returns the identifier of this calendar slot.

0 commit comments

Comments
 (0)