Skip to content

Commit

Permalink
Review feedback around pub fields on partials
Browse files Browse the repository at this point in the history
  • Loading branch information
nekevss committed Aug 11, 2024
1 parent d43e2f7 commit 55046d6
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 157 deletions.
43 changes: 21 additions & 22 deletions src/components/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,36 @@ use std::str::FromStr;

use super::{
duration::{normalized::NormalizedDurationRecord, TimeDuration},
MonthCode, MonthDay, Time, YearMonth,
MonthCode, MonthDay, PartialDateTime, Time, YearMonth,
};

// TODO: PrepareTemporalFields expects a type error to be thrown when all partial fields are None/undefined.
/// A partial Date that may or may not be complete.
#[derive(Debug, Default, Clone, Copy)]
pub struct PartialDate {
pub(crate) year: Option<i32>,
pub(crate) month: Option<i32>,
pub(crate) month_code: Option<MonthCode>,
pub(crate) day: Option<i32>,
pub(crate) era: Option<TinyAsciiStr<16>>,
pub(crate) era_year: Option<i32>,
// A potentially set `year` field.
pub year: Option<i32>,
// A potentially set `month` field.
pub month: Option<i32>,
// A potentially set `month_code` field.
pub month_code: Option<MonthCode>,
// A potentially set `day` field.
pub day: Option<i32>,
// A potentially set `era` field.
pub era: Option<TinyAsciiStr<16>>,
// A potentially set `era_year` field.
pub era_year: Option<i32>,
}

impl PartialDate {
pub fn from_parts(
year: Option<i32>,
month: Option<i32>,
month_code: Option<MonthCode>,
day: Option<i32>,
era: Option<TinyAsciiStr<16>>,
era_year: Option<i32>,
) -> Self {
impl From<PartialDateTime> for PartialDate {
fn from(value: PartialDateTime) -> Self {
Self {
year,
month,
month_code,
day,
era,
era_year,
year: value.year,
month: value.month,
month_code: value.month_code,
day: value.day,
era: value.era,
era_year: value.era_year,
}
}
}
Expand Down
160 changes: 50 additions & 110 deletions src/components/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,66 +18,36 @@ use tinystr::TinyAsciiStr;
use super::{
calendar::{CalendarDateLike, GetTemporalCalendar},
duration::normalized::{NormalizedTimeDuration, RelativeRoundResult},
Date, Duration, MonthCode, PartialDate, PartialTime, Time,
Date, Duration, MonthCode, PartialDate, Time,
};

/// A partial DateTime record
#[derive(Debug, Default, Copy, Clone)]
pub struct PartialDateTime {
date: PartialDate,
time: PartialTime,
}

impl PartialDateTime {
/// Creates a `PartialDateTime` from its individual parts.
#[inline]
#[must_use]
#[allow(clippy::too_many_arguments)]
pub fn from_parts(
year: Option<i32>,
month: Option<i32>,
month_code: Option<MonthCode>,
day: Option<i32>,
era: Option<TinyAsciiStr<16>>,
era_year: Option<i32>,
hour: Option<i32>,
minute: Option<i32>,
second: Option<i32>,
millisecond: Option<i32>,
microsecond: Option<i32>,
nanosecond: Option<i32>,
) -> Self {
Self {
date: PartialDate::from_parts(year, month, month_code, day, era, era_year),
time: PartialTime::from_parts(
hour,
minute,
second,
millisecond,
microsecond,
nanosecond,
),
}
}

/// Creates a `PartialDateTime` from a `PartialDate`.
#[inline]
#[must_use]
pub fn from_partial_date(partial: PartialDate) -> Self {
Self {
date: partial,
time: PartialTime::default(),
}
}

/// Creates a `PartialDateTime` from a `PartialTime`.
#[inline]
#[must_use]
pub fn from_partial_time(partial: PartialTime) -> Self {
Self {
date: PartialDate::default(),
time: partial,
}
}
// A potentially set `year` field.
pub year: Option<i32>,
// A potentially set `month` field.
pub month: Option<i32>,
// A potentially set `month_code` field.
pub month_code: Option<MonthCode>,
// A potentially set `day` field.
pub day: Option<i32>,
// A potentially set `era` field.
pub era: Option<TinyAsciiStr<16>>,
// A potentially set `era_year` field.
pub era_year: Option<i32>,
// A potentially set `hour` field.
pub hour: Option<i32>,
// A potentially set `minute` field.
pub minute: Option<i32>,
// A potentially set `second` field.
pub second: Option<i32>,
// A potentially set `millisecond` field.
pub millisecond: Option<i32>,
// A potentially set `microsecond` field.
pub microsecond: Option<i32>,
// A potentially set `nanosecond` field.
pub nanosecond: Option<i32>,
}

/// The native Rust implementation of `Temporal.PlainDateTime`
Expand Down Expand Up @@ -310,7 +280,7 @@ impl DateTime {
overflow: Option<ArithmeticOverflow>,
) -> TemporalResult<Self> {
let fields = TemporalFields::from(self);
let partial_fields = TemporalFields::from(partial_datetime.date);
let partial_fields = TemporalFields::from(PartialDate::from(partial_datetime));

let mut merge_result = fields.merge_fields(&partial_fields, self.calendar())?;

Expand All @@ -320,7 +290,7 @@ impl DateTime {
)?;

let time = self.iso.time.with(
partial_datetime.time,
partial_datetime.into(),
overflow.unwrap_or(ArithmeticOverflow::Constrain),
)?;

Expand Down Expand Up @@ -630,8 +600,8 @@ mod tests {

use crate::{
components::{
calendar::Calendar, duration::DateDuration, DateTime, Duration, MonthCode, PartialDate,
PartialDateTime, PartialTime,
calendar::Calendar, duration::DateDuration, DateTime, Duration, MonthCode,
PartialDateTime,
},
options::{
DifferenceSettings, RoundingIncrement, RoundingOptions, TemporalRoundingMode,
Expand Down Expand Up @@ -686,11 +656,8 @@ mod tests {

// Test year
let partial = PartialDateTime {
date: PartialDate {
year: Some(2019),
..Default::default()
},
time: PartialTime::default(),
year: Some(2019),
..Default::default()
};
let result = pdt.with(partial, None).unwrap();
assert_datetime(
Expand All @@ -700,11 +667,8 @@ mod tests {

// Test month
let partial = PartialDateTime {
date: PartialDate {
month: Some(5),
..Default::default()
},
time: PartialTime::default(),
month: Some(5),
..Default::default()
};
let result = pdt.with(partial, None).unwrap();
assert_datetime(
Expand All @@ -714,11 +678,8 @@ mod tests {

// Test monthCode
let partial = PartialDateTime {
date: PartialDate {
month_code: Some(MonthCode::Five),
..Default::default()
},
time: PartialTime::default(),
month_code: Some(MonthCode::Five),
..Default::default()
};
let result = pdt.with(partial, None).unwrap();
assert_datetime(
Expand All @@ -728,11 +689,8 @@ mod tests {

// Test day
let partial = PartialDateTime {
date: PartialDate {
day: Some(5),
..Default::default()
},
time: PartialTime::default(),
day: Some(5),
..Default::default()
};
let result = pdt.with(partial, None).unwrap();
assert_datetime(
Expand All @@ -742,11 +700,8 @@ mod tests {

// Test hour
let partial = PartialDateTime {
date: PartialDate::default(),
time: PartialTime {
hour: Some(5),
..Default::default()
},
hour: Some(5),
..Default::default()
};
let result = pdt.with(partial, None).unwrap();
assert_datetime(
Expand All @@ -756,11 +711,8 @@ mod tests {

// Test minute
let partial = PartialDateTime {
date: PartialDate::default(),
time: PartialTime {
minute: Some(5),
..Default::default()
},
minute: Some(5),
..Default::default()
};
let result = pdt.with(partial, None).unwrap();
assert_datetime(
Expand All @@ -770,11 +722,8 @@ mod tests {

// Test second
let partial = PartialDateTime {
date: PartialDate::default(),
time: PartialTime {
second: Some(5),
..Default::default()
},
second: Some(5),
..Default::default()
};
let result = pdt.with(partial, None).unwrap();
assert_datetime(
Expand All @@ -784,11 +733,8 @@ mod tests {

// Test second
let partial = PartialDateTime {
date: PartialDate::default(),
time: PartialTime {
millisecond: Some(5),
..Default::default()
},
millisecond: Some(5),
..Default::default()
};
let result = pdt.with(partial, None).unwrap();
assert_datetime(
Expand All @@ -798,11 +744,8 @@ mod tests {

// Test second
let partial = PartialDateTime {
date: PartialDate::default(),
time: PartialTime {
microsecond: Some(5),
..Default::default()
},
microsecond: Some(5),
..Default::default()
};
let result = pdt.with(partial, None).unwrap();
assert_datetime(
Expand All @@ -812,11 +755,8 @@ mod tests {

// Test second
let partial = PartialDateTime {
date: PartialDate::default(),
time: PartialTime {
nanosecond: Some(5),
..Default::default()
},
nanosecond: Some(5),
..Default::default()
};
let result = pdt.with(partial, None).unwrap();
assert_datetime(
Expand Down
46 changes: 21 additions & 25 deletions src/components/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,36 @@ use crate::{
Sign, TemporalError, TemporalResult,
};

use super::{duration::normalized::NormalizedTimeDuration, DateTime};
use super::{duration::normalized::NormalizedTimeDuration, DateTime, PartialDateTime};

use std::str::FromStr;

/// A `PartialTime` represents partially filled `Time` fields.
#[derive(Debug, Default, Clone, Copy)]
pub struct PartialTime {
pub(crate) hour: Option<i32>,
pub(crate) minute: Option<i32>,
pub(crate) second: Option<i32>,
pub(crate) millisecond: Option<i32>,
pub(crate) microsecond: Option<i32>,
pub(crate) nanosecond: Option<i32>,
// A potentially set `hour` field.
pub hour: Option<i32>,
// A potentially set `minute` field.
pub minute: Option<i32>,
// A potentially set `second` field.
pub second: Option<i32>,
// A potentially set `millisecond` field.
pub millisecond: Option<i32>,
// A potentially set `microsecond` field.
pub microsecond: Option<i32>,
// A potentially set `nanosecond` field.
pub nanosecond: Option<i32>,
}

impl PartialTime {
/// Creates a `PartialTime` from its parts.
#[inline]
#[must_use]
pub fn from_parts(
hour: Option<i32>,
minute: Option<i32>,
second: Option<i32>,
millisecond: Option<i32>,
microsecond: Option<i32>,
nanosecond: Option<i32>,
) -> Self {
impl From<PartialDateTime> for PartialTime {
fn from(value: PartialDateTime) -> Self {
Self {
hour,
minute,
second,
millisecond,
microsecond,
nanosecond,
hour: value.hour,
minute: value.minute,
second: value.second,
millisecond: value.millisecond,
microsecond: value.microsecond,
nanosecond: value.nanosecond,
}
}
}
Expand Down

0 comments on commit 55046d6

Please sign in to comment.