Skip to content

Commit

Permalink
Migrate from &str to &[u8] in ixdtf (#4918)
Browse files Browse the repository at this point in the history
  • Loading branch information
nekevss authored Jun 4, 2024
1 parent 257b8d8 commit 3a64209
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 208 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions utils/ixdtf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ duration = []

[dependencies]
displaydoc = { workspace = true }
utf8_iter.workspace = true

[dev-dependencies]
serde-json-core = { workspace = true, features = ["std"] }
Expand Down
30 changes: 13 additions & 17 deletions utils/ixdtf/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 13 additions & 17 deletions utils/ixdtf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//!
//! let ixdtf_str = "2024-03-02T08:48:00-05:00[America/New_York]";
//!
//! let result = IxdtfParser::new(ixdtf_str).parse().unwrap();
//! let result = IxdtfParser::from_str(ixdtf_str).parse().unwrap();
//!
//! let date = result.date.unwrap();
//! let time = result.time.unwrap();
Expand All @@ -41,7 +41,7 @@
//! assert_eq!(offset.hour, 5);
//! assert_eq!(offset.minute, 0);
//! assert!(!tz_annotation.critical);
//! assert_eq!(tz_annotation.tz, TimeZoneRecord::Name("America/New_York"));
//! assert_eq!(tz_annotation.tz, TimeZoneRecord::Name("America/New_York".as_bytes()));
//! ```
//!
//! ## Date/Time Strings
Expand Down Expand Up @@ -142,9 +142,7 @@
//! let example_one =
//! "2024-03-02T08:48:00-05:00[u-ca=iso8601][America/New_York]";
//!
//! let mut ixdtf = IxdtfParser::new(example_one);
//!
//! let result = ixdtf.parse();
//! let result = IxdtfParser::from_str(example_one).parse();
//!
//! assert_eq!(result, Err(ParserError::AnnotationKeyLeadingChar));
//! ```
Expand All @@ -158,9 +156,9 @@
//! ```rust
//! use ixdtf::{parsers::IxdtfParser, ParserError};
//!
//! let example_one = "2024-03-02T08:48:00-05:00[u-ca=iso8601][!u-ca=japanese]";
//! let example_two = "2024-03-02T08:48:00-05:00[u-ca=iso8601][!u-ca=japanese]";
//!
//! let result = IxdtfParser::new(example_one).parse();
//! let result = IxdtfParser::from_str(example_two).parse();
//!
//! assert_eq!(result, Err(ParserError::CriticalDuplicateCalendar));
//! ```
Expand All @@ -173,10 +171,10 @@
//! ```rust
//! use ixdtf::{parsers::IxdtfParser, ParserError};
//!
//! let example_one =
//! let example_three =
//! "2024-03-02T08:48:00-05:00[u-ca=iso8601][!answer-to-universe=fortytwo]";
//!
//! let result = IxdtfParser::new(example_one).parse();
//! let result = IxdtfParser::from_str(example_three).parse();
//!
//! assert_eq!(result, Err(ParserError::UnrecognizedCritical));
//! ```
Expand Down Expand Up @@ -207,19 +205,17 @@
//! ```rust
//! use ixdtf::parsers::{IxdtfParser, records::TimeZoneRecord};
//!
//! let example_one = "2024-03-02T08:48:00+01:00[!America/New_York]";
//!
//! let mut ixdtf = IxdtfParser::new(example_one);
//! let example_two = "2024-03-02T08:48:00+01:00[!America/New_York]";
//!
//! let result = ixdtf.parse().unwrap();
//! let result = IxdtfParser::from_str(example_two).parse().unwrap();
//!
//! let tz_annotation = result.tz.unwrap();
//! let offset = result.offset.unwrap();
//!
//! // The time zone annotation and offset conflict with each other, and must therefore be
//! // resolved by the user.
//! assert!(tz_annotation.critical);
//! assert_eq!(tz_annotation.tz, TimeZoneRecord::Name("America/New_York"));
//! assert_eq!(tz_annotation.tz, TimeZoneRecord::Name("America/New_York".as_bytes()));
//! assert_eq!(offset.hour, 1);
//! ```
//!
Expand Down Expand Up @@ -254,8 +250,8 @@
//!
//! let mut answer = None;
//!
//! let _ = IxdtfParser::new(example_with_custom_key).parse_with_annotation_handler(|annotation| {
//! if annotation.key == "answer-to-universe" {
//! let _ = IxdtfParser::from_str(example_with_custom_key).parse_with_annotation_handler(|annotation| {
//! if annotation.key == "answer-to-universe".as_bytes() {
//! answer.get_or_insert(annotation);
//! // Found our value! We don't need `ixdtf` to handle this annotation.
//! return None
Expand All @@ -268,7 +264,7 @@
//! let answer = answer.unwrap();
//!
//! assert!(answer.critical);
//! assert_eq!(answer.value, "fortytwo");
//! assert_eq!(answer.value, "fortytwo".as_bytes());
//! ```
//!
//! It is worth noting that in the above example the annotation above found is a critically flagged
Expand Down
10 changes: 5 additions & 5 deletions utils/ixdtf/src/parsers/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
/// Strictly a parsing intermediary for the checking the common annotation backing.
pub(crate) struct AnnotationSet<'a> {
pub(crate) tz: Option<TimeZoneAnnotation<'a>>,
pub(crate) calendar: Option<&'a str>,
pub(crate) calendar: Option<&'a [u8]>,
}

/// Parse a `TimeZoneAnnotation` `Annotations` set
Expand Down Expand Up @@ -53,15 +53,15 @@ pub(crate) fn parse_annotation_set<'a>(
pub(crate) fn parse_annotations<'a>(
cursor: &mut Cursor<'a>,
mut handler: impl FnMut(Annotation<'a>) -> Option<Annotation<'a>>,
) -> ParserResult<Option<&'a str>> {
) -> ParserResult<Option<&'a [u8]>> {
let mut calendar: Option<Annotation<'a>> = None;

while cursor.check_or(false, is_annotation_open) {
let annotation = handler(parse_kv_annotation(cursor)?);

match annotation {
// Check if the key is the registered key "u-ca".
Some(kv) if kv.key == "u-ca" => {
Some(kv) if kv.key == "u-ca".as_bytes() => {
// Check the calendar
match calendar {
Some(calendar)
Expand Down Expand Up @@ -124,7 +124,7 @@ fn parse_kv_annotation<'a>(cursor: &mut Cursor<'a>) -> ParserResult<Annotation<'
}

/// Parse an `AnnotationKey`.
fn parse_annotation_key<'a>(cursor: &mut Cursor<'a>) -> ParserResult<&'a str> {
fn parse_annotation_key<'a>(cursor: &mut Cursor<'a>) -> ParserResult<&'a [u8]> {
let key_start = cursor.pos();
assert_syntax!(
is_a_key_leading_char(cursor.next_or(ParserError::AnnotationKeyLeadingChar)?),
Expand All @@ -147,7 +147,7 @@ fn parse_annotation_key<'a>(cursor: &mut Cursor<'a>) -> ParserResult<&'a str> {
}

/// Parse an `AnnotationValue`.
fn parse_annotation_value<'a>(cursor: &mut Cursor<'a>) -> ParserResult<&'a str> {
fn parse_annotation_value<'a>(cursor: &mut Cursor<'a>) -> ParserResult<&'a [u8]> {
let value_start = cursor.pos();
cursor.advance();
while let Some(potential_value_char) = cursor.next() {
Expand Down
6 changes: 3 additions & 3 deletions utils/ixdtf/src/parsers/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub(crate) fn parse_date_duration(cursor: &mut Cursor) -> ParserResult<DateDurat

while cursor.check_or(false, |ch| ch.is_ascii_digit()) {
let mut value: u32 = 0;
while cursor.check_or(false, |c| c.is_ascii_digit()) {
while cursor.check_or(false, |ch| ch.is_ascii_digit()) {
let digit = cursor
.next_digit()?
.ok_or_else(|| ParserError::abrupt_end("DateDuration"))?;
Expand Down Expand Up @@ -127,13 +127,13 @@ pub(crate) fn parse_time_duration(cursor: &mut Cursor) -> ParserResult<Option<Ti

cursor.advance();
assert_syntax!(
cursor.check_or(false, |ch| ch.is_ascii_digit()),
cursor.check_or(false, |c| c.is_ascii_digit()),
TimeDurationDesignator,
);

let mut time: (u32, u32, u32, Option<u32>) = (0, 0, 0, None);
let mut previous_unit = TimeUnit::None;
while cursor.check_or(false, |ch| ch.is_ascii_digit()) {
while cursor.check_or(false, |c| c.is_ascii_digit()) {
let mut value: u32 = 0;
while cursor.check_or(false, |c| c.is_ascii_digit()) {
let digit = cursor
Expand Down
2 changes: 1 addition & 1 deletion utils/ixdtf/src/parsers/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub(crate) const fn is_time_designator(ch: char) -> bool {
#[inline]
/// Checks if char is a space.
pub(crate) const fn is_space(ch: char) -> bool {
ch == '\u{0020}'
ch == ' '
}

/// Checks if char is a `DateTimeSeparator`.
Expand Down
Loading

0 comments on commit 3a64209

Please sign in to comment.