Skip to content

Commit 30b9fc4

Browse files
committed
slight changes to with() function and covert fallback macro to regular function
1 parent 8645012 commit 30b9fc4

2 files changed

Lines changed: 48 additions & 59 deletions

File tree

src/builtins/core/date.rs

Lines changed: 40 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -81,62 +81,50 @@ impl PartialDate {
8181
crate::impl_with_fallback_method!(with_fallback_year_month, () PlainYearMonth); // excludes day
8282
crate::impl_with_fallback_method!(with_fallback_date, (with_day: day) PlainDate);
8383
crate::impl_with_fallback_method!(with_fallback_datetime, (with_day:day) PlainDateTime);
84-
crate::impl_with_fallback_method_zoned!(with_fallback_zoneddatetime, (with_day:day), ZonedDateTime);
85-
}
8684

87-
#[doc(hidden)]
88-
#[macro_export]
89-
macro_rules! impl_with_fallback_method_zoned {
90-
($method_name:ident, (with_day: $day:ident), $component_type:ty) => {
91-
pub(crate) fn $method_name(
92-
&self,
93-
fallback: &$component_type,
94-
provider: &impl TimeZoneProvider,
95-
) -> TemporalResult<Self> {
96-
let era = if let Some(era) = self.era {
97-
Some(era)
98-
} else {
99-
let era = fallback.era_with_provider(provider)?;
100-
era.map(|e| {
101-
TinyAsciiStr::<19>::try_from_utf8(e.as_bytes())
102-
.map_err(|e| TemporalError::general(format!("{e}")))
103-
})
104-
.transpose()?
105-
};
106-
107-
let fallback_era_year = fallback.era_year_with_provider(provider)?;
108-
let era_year = self
109-
.era_year
110-
.map_or_else(|| fallback_era_year, |ey| Some(ey));
85+
pub(crate) fn with_fallback_zoneddatetime(
86+
&self,
87+
fallback: &ZonedDateTime,
88+
provider: &impl TimeZoneProvider,
89+
) -> TemporalResult<Self> {
90+
let era = if let Some(era) = self.era {
91+
Some(era)
92+
} else {
93+
let era = fallback.era_with_provider(provider)?;
94+
era.map(|e| {
95+
TinyAsciiStr::<19>::try_from_utf8(e.as_bytes())
96+
.map_err(|e| TemporalError::general(format!("{e}")))
97+
})
98+
.transpose()?
99+
};
111100

112-
let (month, month_code) = match (self.month, self.month_code) {
113-
(Some(month), Some(mc)) => (Some(month), Some(mc)),
114-
(Some(month), None) => (Some(month), Some(month_to_month_code(month)?)),
115-
(None, Some(mc)) => (Some(mc.to_month_integer()).map(Into::into), Some(mc)),
116-
(None, None) => (
117-
Some(fallback.month_with_provider(provider)?).map(Into::into),
118-
Some(fallback.month_code_with_provider(provider)?),
119-
),
120-
};
101+
let fallback_era_year = fallback.era_year_with_provider(provider)?;
102+
let era_year = self.era_year.map_or_else(|| fallback_era_year, Some);
103+
104+
let (month, month_code) = match (self.month, self.month_code) {
105+
(Some(month), Some(mc)) => (Some(month), Some(mc)),
106+
(Some(month), None) => (Some(month), Some(month_to_month_code(month)?)),
107+
(None, Some(mc)) => (Some(mc.to_month_integer()), Some(mc)),
108+
(None, None) => (
109+
Some(fallback.month_with_provider(provider)?),
110+
Some(fallback.month_code_with_provider(provider)?),
111+
),
112+
};
121113

122-
#[allow(clippy::needless_update)]
123-
{
124-
Ok(Self {
125-
year: Some(self.year.unwrap_or(fallback.year_with_provider(provider)?)),
126-
month,
127-
month_code,
128-
$day: Some(
129-
self.day
130-
.unwrap_or(fallback.day_with_provider(provider)?.into()),
131-
),
132-
era,
133-
era_year,
134-
calendar: fallback.calendar().clone(),
135-
..Default::default()
136-
})
137-
}
114+
#[allow(clippy::needless_update)]
115+
{
116+
Ok(Self {
117+
year: Some(self.year.unwrap_or(fallback.year_with_provider(provider)?)),
118+
month,
119+
month_code,
120+
day: Some(self.day.unwrap_or(fallback.day_with_provider(provider)?)),
121+
era,
122+
era_year,
123+
calendar: fallback.calendar().clone(),
124+
..Default::default()
125+
})
138126
}
139-
};
127+
}
140128
}
141129

142130
// Use macro to impl fallback methods to avoid having a trait method.

src/builtins/core/zoneddatetime.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -517,22 +517,23 @@ impl ZonedDateTime {
517517
pub fn with(
518518
&self,
519519
partial: PartialZonedDateTime,
520-
disambiguation: Disambiguation,
521-
offset_option: OffsetDisambiguation,
520+
disambiguation: Option<Disambiguation>,
521+
offset_option: Option<OffsetDisambiguation>,
522522
overflow: Option<ArithmeticOverflow>,
523523
provider: &impl TimeZoneProvider,
524524
) -> TemporalResult<Self> {
525+
let overflow = overflow.unwrap_or_default();
526+
let disambiguation = disambiguation.unwrap_or_default();
527+
let offset_option = offset_option.unwrap_or(OffsetDisambiguation::Reject);
528+
525529
// 23. Let dateTimeResult be ? InterpretTemporalDateTimeFields(calendar, fields, overflow).
526530
let result_date = self.calendar.date_from_partial(
527531
&partial.date.with_fallback_zoneddatetime(self, provider)?,
528-
overflow.unwrap_or(ArithmeticOverflow::Constrain),
532+
ArithmeticOverflow::Constrain,
529533
)?;
530534

531535
let original_iso = self.tz.get_iso_datetime_for(&self.instant, provider)?.time;
532-
let time = original_iso.with(
533-
partial.time,
534-
overflow.unwrap_or(ArithmeticOverflow::Constrain),
535-
)?;
536+
let time = original_iso.with(partial.time, overflow)?;
536537

537538
// 24. Let newOffsetNanoseconds be ! ParseDateTimeUTCOffset(fields.[[OffsetString]]).
538539
let original_offset = self.offset_nanoseconds_with_provider(provider)?;

0 commit comments

Comments
 (0)