Skip to content

Commit

Permalink
Ensure parsing throws with unknown critical annotations (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 authored Jun 25, 2024
1 parent 2b4cbb2 commit ec2f2d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
16 changes: 16 additions & 0 deletions src/components/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -847,4 +847,20 @@ mod tests {
assert!(Date::<()>::from_str(s).is_err())
}
}

// test262/test/built-ins/Temporal/Calendar/prototype/day/argument-string-critical-unknown-annotation.js
#[test]
fn argument_string_critical_unknown_annotation() {
const INVALID_STRINGS: [&str; 6] = [
"1970-01-01[!foo=bar]",
"1970-01-01T00:00[!foo=bar]",
"1970-01-01T00:00[UTC][!foo=bar]",
"1970-01-01T00:00[u-ca=iso8601][!foo=bar]",
"1970-01-01T00:00[UTC][!foo=bar][u-ca=iso8601]",
"1970-01-01T00:00[foo=bar][!_foo-bar0=Dont-Ignore-This-99999999999]",
];
for s in INVALID_STRINGS {
assert!(Date::<()>::from_str(s).is_err())
}
}
}
13 changes: 7 additions & 6 deletions src/parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ fn parse_ixdtf(source: &str, variant: ParseVariant) -> TemporalResult<IxdtfParse
}
None => first_calendar = Some(annotation),
}
return None;
}

// Ignore all invalid annotations
None
// Make the parser handle any unknown annotation.
Some(annotation)
});

let mut result = match variant {
let mut record = match variant {
ParseVariant::YearMonth => parser.parse_year_month_with_annotation_handler(handler),
ParseVariant::MonthDay => parser.parse_month_day_with_annotation_handler(handler),
ParseVariant::DateTime => parser.parse_with_annotation_handler(handler),
Expand All @@ -59,15 +60,15 @@ fn parse_ixdtf(source: &str, variant: ParseVariant) -> TemporalResult<IxdtfParse
}

// Validate that the DateRecord exists.
if result.date.is_none() {
if record.date.is_none() {
return Err(
TemporalError::syntax().with_message("DateTime strings must contain a Date value.")
);
}

result.calendar = first_calendar.map(|v| v.value);
record.calendar = first_calendar.map(|v| v.value);

Ok(result)
Ok(record)
}

/// A utility function for parsing a `DateTime` string
Expand Down

0 comments on commit ec2f2d0

Please sign in to comment.