Skip to content

Commit a7f4825

Browse files
authored
Fix not validating MonthCode in ISO path (#210)
This PR is a follow up to #208 and adds `MonthCode` validation on the ISO calendar path.
1 parent f2b335c commit a7f4825

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/builtins/core/calendar/types.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ impl ResolvedCalendarFields {
3636
) -> TemporalResult<Self> {
3737
let era_year = EraYear::try_from_partial_date(partial_date)?;
3838
if partial_date.calendar.is_iso() {
39-
let month_code =
40-
resolve_iso_month(partial_date.month_code, partial_date.month, overflow)?;
39+
let month_code = resolve_iso_month(partial_date, overflow)?;
4140
let day = resolve_day(partial_date.day, resolve_type == ResolutionType::YearMonth)?;
4241
let day = if overflow == ArithmeticOverflow::Constrain {
4342
constrain_iso_day(era_year.year, month_code.to_month_integer(), day)
@@ -325,13 +324,12 @@ const fn ascii_digit_to_int(ascii_digit: u8) -> u8 {
325324
}
326325

327326
fn resolve_iso_month(
328-
mc: Option<MonthCode>,
329-
month: Option<u8>,
327+
partial_date: &PartialDate,
330328
overflow: ArithmeticOverflow,
331329
) -> TemporalResult<MonthCode> {
332-
match (mc, month) {
330+
let month_code = match (partial_date.month_code, partial_date.month) {
333331
(None, None) => {
334-
Err(TemporalError::r#type().with_message("Month or monthCode must be provided."))
332+
return Err(TemporalError::r#type().with_message("Month or monthCode must be provided."))
335333
}
336334
(None, Some(month)) => {
337335
if overflow == ArithmeticOverflow::Constrain {
@@ -342,20 +340,19 @@ fn resolve_iso_month(
342340
TemporalError::range().with_message("month value is not in a valid range.")
343341
);
344342
}
345-
month_to_month_code(month)
343+
month_to_month_code(month)?
346344
}
347-
(Some(mc), None) => {
348-
// Check that monthCode is parsable.
349-
Ok(mc)
350-
}
351-
(Some(mc), Some(month)) => {
352-
if month != mc.to_month_integer() {
345+
(Some(month_code), None) => month_code,
346+
(Some(month_code), Some(month)) => {
347+
if month != month_code.to_month_integer() {
353348
return Err(TemporalError::range()
354349
.with_message("month and monthCode could not be resolved."));
355350
}
356-
Ok(mc)
351+
month_code
357352
}
358-
}
353+
};
354+
month_code.validate(&partial_date.calendar)?;
355+
Ok(month_code)
359356
}
360357

361358
#[cfg(test)]

0 commit comments

Comments
 (0)