Skip to content

Commit 88961f9

Browse files
authored
API cleanup, visibility updates, and tech debt cleanup (#168)
This PR addresses some issues brought up in #166. The general updates are as follows: - `CalendarDateLike` removed in favor of using `IsoDate` directly. - `CalendarFieldsType` removed - `GetTemporalCalendar` removed - `DateDuration` and `TimeDuration` exported from root with the other builtins - `TzProvider` renamed to `TimeZoneProvider`
1 parent 4025299 commit 88961f9

File tree

15 files changed

+206
-284
lines changed

15 files changed

+206
-284
lines changed

src/components/calendar.rs

Lines changed: 29 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
66
// TODO: It may finally be time to clean up API to use `IsoDate` and `DateDuration` directly.
77

8-
use alloc::borrow::ToOwned;
98
use alloc::string::String;
109
use alloc::vec::Vec;
1110
use core::str::FromStr;
@@ -207,67 +206,6 @@ impl FromStr for Calendar {
207206
}
208207
}
209208

210-
// TODO: Potentially dead code.
211-
/// Designate the type of `CalendarFields` needed
212-
#[derive(Debug, Clone, Copy)]
213-
pub enum CalendarFieldsType {
214-
/// Whether the Fields should return for a PlainDate.
215-
Date,
216-
/// Whether the Fields should return for a PlainYearMonth.
217-
YearMonth,
218-
/// Whether the Fields should return for a PlainMonthDay.
219-
MonthDay,
220-
}
221-
222-
// TODO: Optimize to TinyStr or &str.
223-
impl From<&[String]> for CalendarFieldsType {
224-
fn from(value: &[String]) -> Self {
225-
let year_present = value.contains(&"year".to_owned());
226-
let day_present = value.contains(&"day".to_owned());
227-
228-
if year_present && day_present {
229-
CalendarFieldsType::Date
230-
} else if year_present {
231-
CalendarFieldsType::YearMonth
232-
} else {
233-
CalendarFieldsType::MonthDay
234-
}
235-
}
236-
}
237-
238-
/// The `DateLike` objects that can be provided to the `CalendarProtocol`.
239-
#[derive(Debug)]
240-
pub enum CalendarDateLike<'a> {
241-
/// Represents a `PlainDateTime`.
242-
DateTime(&'a PlainDateTime),
243-
/// Represents a `PlainDate`.
244-
Date(&'a PlainDate),
245-
/// Represents a `PlainYearMonth`.
246-
YearMonth(&'a PlainYearMonth),
247-
/// Represents a `PlainMonthDay`.
248-
MonthDay(&'a PlainMonthDay),
249-
}
250-
251-
impl CalendarDateLike<'_> {
252-
/// Retrieves the internal `IsoDate` field.
253-
#[inline]
254-
#[must_use]
255-
pub fn as_iso_date(&self) -> IsoDate {
256-
match self {
257-
CalendarDateLike::DateTime(dt) => dt.iso.date,
258-
CalendarDateLike::Date(d) => d.iso,
259-
CalendarDateLike::YearMonth(ym) => ym.iso,
260-
CalendarDateLike::MonthDay(md) => md.iso,
261-
}
262-
}
263-
}
264-
265-
/// A trait for retrieving an internal calendar slice.
266-
pub trait GetTemporalCalendar {
267-
/// Returns the `TemporalCalendar` value of the implementor.
268-
fn get_calendar(&self) -> Calendar;
269-
}
270-
271209
// ==== Public `CalendarSlot` methods ====
272210

273211
impl Calendar {
@@ -422,84 +360,80 @@ impl Calendar {
422360
}
423361

424362
/// `CalendarEra`
425-
pub fn era(&self, date_like: &CalendarDateLike) -> TemporalResult<Option<TinyAsciiStr<16>>> {
363+
pub fn era(&self, iso_date: &IsoDate) -> TemporalResult<Option<TinyAsciiStr<16>>> {
426364
if self.is_iso() {
427365
return Ok(None);
428366
}
429-
let calendar_date = self.0.date_from_iso(date_like.as_iso_date().as_icu4x()?);
367+
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
430368
Ok(self.0.year(&calendar_date).standard_era().map(|era| era.0))
431369
}
432370

433371
/// `CalendarEraYear`
434-
pub fn era_year(&self, date_like: &CalendarDateLike) -> TemporalResult<Option<i32>> {
372+
pub fn era_year(&self, iso_date: &IsoDate) -> TemporalResult<Option<i32>> {
435373
if self.is_iso() {
436374
return Ok(None);
437375
}
438-
let calendar_date = self.0.date_from_iso(date_like.as_iso_date().as_icu4x()?);
376+
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
439377
Ok(self.0.year(&calendar_date).era_year())
440378
}
441379

442380
/// `CalendarYear`
443-
pub fn year(&self, date_like: &CalendarDateLike) -> TemporalResult<i32> {
381+
pub fn year(&self, iso_date: &IsoDate) -> TemporalResult<i32> {
444382
if self.is_iso() {
445-
return Ok(date_like.as_iso_date().year);
383+
return Ok(iso_date.year);
446384
}
447-
let calendar_date = self.0.date_from_iso(date_like.as_iso_date().as_icu4x()?);
385+
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
448386
Ok(self.0.year(&calendar_date).extended_year)
449387
}
450388

451389
/// `CalendarMonth`
452-
pub fn month(&self, date_like: &CalendarDateLike) -> TemporalResult<u8> {
390+
pub fn month(&self, iso_date: &IsoDate) -> TemporalResult<u8> {
453391
if self.is_iso() {
454-
return Ok(date_like.as_iso_date().month);
392+
return Ok(iso_date.month);
455393
}
456394

457395
Err(TemporalError::range().with_message("Not yet implemented."))
458396
}
459397

460398
/// `CalendarMonthCode`
461-
pub fn month_code(&self, date_like: &CalendarDateLike) -> TemporalResult<TinyAsciiStr<4>> {
399+
pub fn month_code(&self, iso_date: &IsoDate) -> TemporalResult<TinyAsciiStr<4>> {
462400
if self.is_iso() {
463-
return Ok(date_like.as_iso_date().as_icu4x()?.month().standard_code.0);
401+
return Ok(iso_date.as_icu4x()?.month().standard_code.0);
464402
}
465403

466404
Err(TemporalError::range().with_message("Not yet implemented."))
467405
}
468406

469407
/// `CalendarDay`
470-
pub fn day(&self, date_like: &CalendarDateLike) -> TemporalResult<u8> {
408+
pub fn day(&self, iso_date: &IsoDate) -> TemporalResult<u8> {
471409
if self.is_iso() {
472-
return Ok(date_like.as_iso_date().day);
410+
return Ok(iso_date.day);
473411
}
474412

475413
Err(TemporalError::range().with_message("Not yet implemented."))
476414
}
477415

478416
/// `CalendarDayOfWeek`
479-
pub fn day_of_week(&self, date_like: &CalendarDateLike) -> TemporalResult<u16> {
417+
pub fn day_of_week(&self, iso_date: &IsoDate) -> TemporalResult<u16> {
480418
if self.is_iso() {
481-
return Ok(date_like.as_iso_date().as_icu4x()?.day_of_week() as u16);
419+
return Ok(iso_date.as_icu4x()?.day_of_week() as u16);
482420
}
483421

484422
Err(TemporalError::range().with_message("Not yet implemented."))
485423
}
486424

487425
/// `CalendarDayOfYear`
488-
pub fn day_of_year(&self, date_like: &CalendarDateLike) -> TemporalResult<u16> {
426+
pub fn day_of_year(&self, iso_date: &IsoDate) -> TemporalResult<u16> {
489427
if self.is_iso() {
490-
return Ok(date_like
491-
.as_iso_date()
492-
.as_icu4x()?
493-
.day_of_year_info()
494-
.day_of_year);
428+
return Ok(iso_date.as_icu4x()?.day_of_year_info().day_of_year);
495429
}
496430
Err(TemporalError::range().with_message("Not yet implemented."))?
497431
}
498432

499433
/// `CalendarWeekOfYear`
500-
pub fn week_of_year(&self, date_like: &CalendarDateLike) -> TemporalResult<Option<u16>> {
434+
pub fn week_of_year(&self, iso_date: &IsoDate) -> TemporalResult<Option<u16>> {
501435
if self.is_iso() {
502-
let date = date_like.as_iso_date().as_icu4x()?;
436+
let date = iso_date.as_icu4x()?;
503437

504438
let week_calculator = WeekCalculator::default();
505439

@@ -511,9 +445,9 @@ impl Calendar {
511445
}
512446

513447
/// `CalendarYearOfWeek`
514-
pub fn year_of_week(&self, date_like: &CalendarDateLike) -> TemporalResult<Option<i32>> {
448+
pub fn year_of_week(&self, iso_date: &IsoDate) -> TemporalResult<Option<i32>> {
515449
if self.is_iso() {
516-
let date = date_like.as_iso_date().as_icu4x()?;
450+
let date = iso_date.as_icu4x()?;
517451

518452
let week_calculator = WeekCalculator::default();
519453

@@ -529,42 +463,42 @@ impl Calendar {
529463
}
530464

531465
/// `CalendarDaysInWeek`
532-
pub fn days_in_week(&self, _date_like: &CalendarDateLike) -> TemporalResult<u16> {
466+
pub fn days_in_week(&self, _iso_date: &IsoDate) -> TemporalResult<u16> {
533467
if self.is_iso() {
534468
return Ok(7);
535469
}
536470
Err(TemporalError::range().with_message("Not yet implemented."))
537471
}
538472

539473
/// `CalendarDaysInMonth`
540-
pub fn days_in_month(&self, date_like: &CalendarDateLike) -> TemporalResult<u16> {
474+
pub fn days_in_month(&self, iso_date: &IsoDate) -> TemporalResult<u16> {
541475
if self.is_iso() {
542-
return Ok(date_like.as_iso_date().as_icu4x()?.days_in_month() as u16);
476+
return Ok(iso_date.as_icu4x()?.days_in_month() as u16);
543477
}
544478
Err(TemporalError::range().with_message("Not yet implemented."))
545479
}
546480

547481
/// `CalendarDaysInYear`
548-
pub fn days_in_year(&self, date_like: &CalendarDateLike) -> TemporalResult<u16> {
482+
pub fn days_in_year(&self, iso_date: &IsoDate) -> TemporalResult<u16> {
549483
if self.is_iso() {
550-
return Ok(date_like.as_iso_date().as_icu4x()?.days_in_year());
484+
return Ok(iso_date.as_icu4x()?.days_in_year());
551485
}
552486

553487
Err(TemporalError::range().with_message("Not yet implemented."))
554488
}
555489

556490
/// `CalendarMonthsInYear`
557-
pub fn months_in_year(&self, _date_like: &CalendarDateLike) -> TemporalResult<u16> {
491+
pub fn months_in_year(&self, _iso_date: &IsoDate) -> TemporalResult<u16> {
558492
if self.is_iso() {
559493
return Ok(12);
560494
}
561495
Err(TemporalError::range().with_message("Not yet implemented."))
562496
}
563497

564498
/// `CalendarInLeapYear`
565-
pub fn in_leap_year(&self, date_like: &CalendarDateLike) -> TemporalResult<bool> {
499+
pub fn in_leap_year(&self, iso_date: &IsoDate) -> TemporalResult<bool> {
566500
if self.is_iso() {
567-
return Ok(date_like.as_iso_date().as_icu4x()?.is_in_leap_year());
501+
return Ok(iso_date.as_icu4x()?.is_in_leap_year());
568502
}
569503
Err(TemporalError::range().with_message("Not yet implemented."))
570504
}

src/components/date.rs

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
//! This module implements `Date` and any directly related algorithms.
22
33
use crate::{
4-
components::{
5-
calendar::{Calendar, CalendarDateLike, GetTemporalCalendar},
6-
duration::DateDuration,
7-
Duration, PlainDateTime,
8-
},
4+
components::{calendar::Calendar, duration::DateDuration, Duration, PlainDateTime},
95
iso::{IsoDate, IsoDateTime, IsoTime},
106
options::{
117
ArithmeticOverflow, DifferenceOperation, DifferenceSettings, DisplayCalendar,
@@ -486,75 +482,75 @@ impl PlainDate {
486482
impl PlainDate {
487483
/// Returns the calendar year value.
488484
pub fn year(&self) -> TemporalResult<i32> {
489-
self.calendar.year(&CalendarDateLike::Date(self))
485+
self.calendar.year(&self.iso)
490486
}
491487

492488
/// Returns the calendar month value.
493489
pub fn month(&self) -> TemporalResult<u8> {
494-
self.calendar.month(&CalendarDateLike::Date(self))
490+
self.calendar.month(&self.iso)
495491
}
496492

497493
/// Returns the calendar month code value.
498494
pub fn month_code(&self) -> TemporalResult<TinyAsciiStr<4>> {
499-
self.calendar.month_code(&CalendarDateLike::Date(self))
495+
self.calendar.month_code(&self.iso)
500496
}
501497

502498
/// Returns the calendar day value.
503499
pub fn day(&self) -> TemporalResult<u8> {
504-
self.calendar.day(&CalendarDateLike::Date(self))
500+
self.calendar.day(&self.iso)
505501
}
506502

507503
/// Returns the calendar day of week value.
508504
pub fn day_of_week(&self) -> TemporalResult<u16> {
509-
self.calendar.day_of_week(&CalendarDateLike::Date(self))
505+
self.calendar.day_of_week(&self.iso)
510506
}
511507

512508
/// Returns the calendar day of year value.
513509
pub fn day_of_year(&self) -> TemporalResult<u16> {
514-
self.calendar.day_of_year(&CalendarDateLike::Date(self))
510+
self.calendar.day_of_year(&self.iso)
515511
}
516512

517513
/// Returns the calendar week of year value.
518514
pub fn week_of_year(&self) -> TemporalResult<Option<u16>> {
519-
self.calendar.week_of_year(&CalendarDateLike::Date(self))
515+
self.calendar.week_of_year(&self.iso)
520516
}
521517

522518
/// Returns the calendar year of week value.
523519
pub fn year_of_week(&self) -> TemporalResult<Option<i32>> {
524-
self.calendar.year_of_week(&CalendarDateLike::Date(self))
520+
self.calendar.year_of_week(&self.iso)
525521
}
526522

527523
/// Returns the calendar days in week value.
528524
pub fn days_in_week(&self) -> TemporalResult<u16> {
529-
self.calendar.days_in_week(&CalendarDateLike::Date(self))
525+
self.calendar.days_in_week(&self.iso)
530526
}
531527

532528
/// Returns the calendar days in month value.
533529
pub fn days_in_month(&self) -> TemporalResult<u16> {
534-
self.calendar.days_in_month(&CalendarDateLike::Date(self))
530+
self.calendar.days_in_month(&self.iso)
535531
}
536532

537533
/// Returns the calendar days in year value.
538534
pub fn days_in_year(&self) -> TemporalResult<u16> {
539-
self.calendar.days_in_year(&CalendarDateLike::Date(self))
535+
self.calendar.days_in_year(&self.iso)
540536
}
541537

542538
/// Returns the calendar months in year value.
543539
pub fn months_in_year(&self) -> TemporalResult<u16> {
544-
self.calendar.months_in_year(&CalendarDateLike::Date(self))
540+
self.calendar.months_in_year(&self.iso)
545541
}
546542

547543
/// Returns returns whether the date in a leap year for the given calendar.
548544
pub fn in_leap_year(&self) -> TemporalResult<bool> {
549-
self.calendar.in_leap_year(&CalendarDateLike::Date(self))
545+
self.calendar.in_leap_year(&self.iso)
550546
}
551547

552548
pub fn era(&self) -> TemporalResult<Option<TinyAsciiStr<16>>> {
553-
self.calendar.era(&CalendarDateLike::Date(self))
549+
self.calendar.era(&self.iso)
554550
}
555551

556552
pub fn era_year(&self) -> TemporalResult<Option<i32>> {
557-
self.calendar.era_year(&CalendarDateLike::Date(self))
553+
self.calendar.era_year(&self.iso)
558554
}
559555
}
560556

@@ -570,13 +566,13 @@ impl PlainDate {
570566
pub fn to_date_time(&self, time: Option<PlainTime>) -> TemporalResult<PlainDateTime> {
571567
let time = time.unwrap_or_default();
572568
let iso = IsoDateTime::new(self.iso, time.iso)?;
573-
Ok(PlainDateTime::new_unchecked(iso, self.get_calendar()))
569+
Ok(PlainDateTime::new_unchecked(iso, self.calendar().clone()))
574570
}
575571

576572
/// Converts the current `Date<C>` into a `PlainYearMonth`
577573
#[inline]
578574
pub fn to_year_month(&self) -> TemporalResult<PlainYearMonth> {
579-
self.get_calendar().year_month_from_partial(
575+
self.calendar().year_month_from_partial(
580576
&PartialDate::default().with_fallback_date(self)?,
581577
ArithmeticOverflow::Constrain,
582578
)
@@ -585,7 +581,7 @@ impl PlainDate {
585581
/// Converts the current `Date<C>` into a `PlainMonthDay`
586582
#[inline]
587583
pub fn to_month_day(&self) -> TemporalResult<PlainMonthDay> {
588-
self.get_calendar().month_day_from_partial(
584+
self.calendar().month_day_from_partial(
589585
&PartialDate::default().with_fallback_date(self)?,
590586
ArithmeticOverflow::Constrain,
591587
)
@@ -602,12 +598,6 @@ impl PlainDate {
602598

603599
// ==== Trait impls ====
604600

605-
impl GetTemporalCalendar for PlainDate {
606-
fn get_calendar(&self) -> Calendar {
607-
self.calendar.clone()
608-
}
609-
}
610-
611601
impl From<PlainDateTime> for PlainDate {
612602
fn from(value: PlainDateTime) -> Self {
613603
PlainDate::new_unchecked(value.iso.date, value.calendar().clone())

0 commit comments

Comments
 (0)