Skip to content

Commit 3a64209

Browse files
authored
Migrate from &str to &[u8] in ixdtf (#4918)
1 parent 257b8d8 commit 3a64209

File tree

11 files changed

+226
-208
lines changed

11 files changed

+226
-208
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utils/ixdtf/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ duration = []
2828

2929
[dependencies]
3030
displaydoc = { workspace = true }
31+
utf8_iter.workspace = true
3132

3233
[dev-dependencies]
3334
serde-json-core = { workspace = true, features = ["std"] }

utils/ixdtf/README.md

+13-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utils/ixdtf/src/lib.rs

+13-17
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
//!
2626
//! let ixdtf_str = "2024-03-02T08:48:00-05:00[America/New_York]";
2727
//!
28-
//! let result = IxdtfParser::new(ixdtf_str).parse().unwrap();
28+
//! let result = IxdtfParser::from_str(ixdtf_str).parse().unwrap();
2929
//!
3030
//! let date = result.date.unwrap();
3131
//! let time = result.time.unwrap();
@@ -41,7 +41,7 @@
4141
//! assert_eq!(offset.hour, 5);
4242
//! assert_eq!(offset.minute, 0);
4343
//! assert!(!tz_annotation.critical);
44-
//! assert_eq!(tz_annotation.tz, TimeZoneRecord::Name("America/New_York"));
44+
//! assert_eq!(tz_annotation.tz, TimeZoneRecord::Name("America/New_York".as_bytes()));
4545
//! ```
4646
//!
4747
//! ## Date/Time Strings
@@ -142,9 +142,7 @@
142142
//! let example_one =
143143
//! "2024-03-02T08:48:00-05:00[u-ca=iso8601][America/New_York]";
144144
//!
145-
//! let mut ixdtf = IxdtfParser::new(example_one);
146-
//!
147-
//! let result = ixdtf.parse();
145+
//! let result = IxdtfParser::from_str(example_one).parse();
148146
//!
149147
//! assert_eq!(result, Err(ParserError::AnnotationKeyLeadingChar));
150148
//! ```
@@ -158,9 +156,9 @@
158156
//! ```rust
159157
//! use ixdtf::{parsers::IxdtfParser, ParserError};
160158
//!
161-
//! let example_one = "2024-03-02T08:48:00-05:00[u-ca=iso8601][!u-ca=japanese]";
159+
//! let example_two = "2024-03-02T08:48:00-05:00[u-ca=iso8601][!u-ca=japanese]";
162160
//!
163-
//! let result = IxdtfParser::new(example_one).parse();
161+
//! let result = IxdtfParser::from_str(example_two).parse();
164162
//!
165163
//! assert_eq!(result, Err(ParserError::CriticalDuplicateCalendar));
166164
//! ```
@@ -173,10 +171,10 @@
173171
//! ```rust
174172
//! use ixdtf::{parsers::IxdtfParser, ParserError};
175173
//!
176-
//! let example_one =
174+
//! let example_three =
177175
//! "2024-03-02T08:48:00-05:00[u-ca=iso8601][!answer-to-universe=fortytwo]";
178176
//!
179-
//! let result = IxdtfParser::new(example_one).parse();
177+
//! let result = IxdtfParser::from_str(example_three).parse();
180178
//!
181179
//! assert_eq!(result, Err(ParserError::UnrecognizedCritical));
182180
//! ```
@@ -207,19 +205,17 @@
207205
//! ```rust
208206
//! use ixdtf::parsers::{IxdtfParser, records::TimeZoneRecord};
209207
//!
210-
//! let example_one = "2024-03-02T08:48:00+01:00[!America/New_York]";
211-
//!
212-
//! let mut ixdtf = IxdtfParser::new(example_one);
208+
//! let example_two = "2024-03-02T08:48:00+01:00[!America/New_York]";
213209
//!
214-
//! let result = ixdtf.parse().unwrap();
210+
//! let result = IxdtfParser::from_str(example_two).parse().unwrap();
215211
//!
216212
//! let tz_annotation = result.tz.unwrap();
217213
//! let offset = result.offset.unwrap();
218214
//!
219215
//! // The time zone annotation and offset conflict with each other, and must therefore be
220216
//! // resolved by the user.
221217
//! assert!(tz_annotation.critical);
222-
//! assert_eq!(tz_annotation.tz, TimeZoneRecord::Name("America/New_York"));
218+
//! assert_eq!(tz_annotation.tz, TimeZoneRecord::Name("America/New_York".as_bytes()));
223219
//! assert_eq!(offset.hour, 1);
224220
//! ```
225221
//!
@@ -254,8 +250,8 @@
254250
//!
255251
//! let mut answer = None;
256252
//!
257-
//! let _ = IxdtfParser::new(example_with_custom_key).parse_with_annotation_handler(|annotation| {
258-
//! if annotation.key == "answer-to-universe" {
253+
//! let _ = IxdtfParser::from_str(example_with_custom_key).parse_with_annotation_handler(|annotation| {
254+
//! if annotation.key == "answer-to-universe".as_bytes() {
259255
//! answer.get_or_insert(annotation);
260256
//! // Found our value! We don't need `ixdtf` to handle this annotation.
261257
//! return None
@@ -268,7 +264,7 @@
268264
//! let answer = answer.unwrap();
269265
//!
270266
//! assert!(answer.critical);
271-
//! assert_eq!(answer.value, "fortytwo");
267+
//! assert_eq!(answer.value, "fortytwo".as_bytes());
272268
//! ```
273269
//!
274270
//! It is worth noting that in the above example the annotation above found is a critically flagged

utils/ixdtf/src/parsers/annotations.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{
2121
/// Strictly a parsing intermediary for the checking the common annotation backing.
2222
pub(crate) struct AnnotationSet<'a> {
2323
pub(crate) tz: Option<TimeZoneAnnotation<'a>>,
24-
pub(crate) calendar: Option<&'a str>,
24+
pub(crate) calendar: Option<&'a [u8]>,
2525
}
2626

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

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

6262
match annotation {
6363
// Check if the key is the registered key "u-ca".
64-
Some(kv) if kv.key == "u-ca" => {
64+
Some(kv) if kv.key == "u-ca".as_bytes() => {
6565
// Check the calendar
6666
match calendar {
6767
Some(calendar)
@@ -124,7 +124,7 @@ fn parse_kv_annotation<'a>(cursor: &mut Cursor<'a>) -> ParserResult<Annotation<'
124124
}
125125

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

149149
/// Parse an `AnnotationValue`.
150-
fn parse_annotation_value<'a>(cursor: &mut Cursor<'a>) -> ParserResult<&'a str> {
150+
fn parse_annotation_value<'a>(cursor: &mut Cursor<'a>) -> ParserResult<&'a [u8]> {
151151
let value_start = cursor.pos();
152152
cursor.advance();
153153
while let Some(potential_value_char) = cursor.next() {

utils/ixdtf/src/parsers/duration.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub(crate) fn parse_date_duration(cursor: &mut Cursor) -> ParserResult<DateDurat
6666

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

128128
cursor.advance();
129129
assert_syntax!(
130-
cursor.check_or(false, |ch| ch.is_ascii_digit()),
130+
cursor.check_or(false, |c| c.is_ascii_digit()),
131131
TimeDurationDesignator,
132132
);
133133

134134
let mut time: (u32, u32, u32, Option<u32>) = (0, 0, 0, None);
135135
let mut previous_unit = TimeUnit::None;
136-
while cursor.check_or(false, |ch| ch.is_ascii_digit()) {
136+
while cursor.check_or(false, |c| c.is_ascii_digit()) {
137137
let mut value: u32 = 0;
138138
while cursor.check_or(false, |c| c.is_ascii_digit()) {
139139
let digit = cursor

utils/ixdtf/src/parsers/grammar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub(crate) const fn is_time_designator(ch: char) -> bool {
6666
#[inline]
6767
/// Checks if char is a space.
6868
pub(crate) const fn is_space(ch: char) -> bool {
69-
ch == '\u{0020}'
69+
ch == ' '
7070
}
7171

7272
/// Checks if char is a `DateTimeSeparator`.

0 commit comments

Comments
 (0)