Skip to main content

jiff/fmt/friendly/
parser.rs

1use crate::{
2    error::{fmt::friendly::Error as E, ErrorContext},
3    fmt::{
4        friendly::parser_label,
5        util::{parse_temporal_fraction, DurationUnits},
6        Parsed,
7    },
8    util::{c::Sign, parse},
9    Error, SignedDuration, Span, Unit,
10};
11
12/// A parser for Jiff's "friendly" duration format.
13///
14/// See the [module documentation](super) for more details on the precise
15/// format supported by this parser.
16///
17/// Unlike [`SpanPrinter`](super::SpanPrinter), this parser doesn't have any
18/// configuration knobs. While it may grow some in the future, the approach
19/// taken here is for the parser to support the entire grammar. That is, the
20/// parser can parse anything emitted by `SpanPrinter`. (And indeed, the
21/// parser can even handle things that the printer can't emit due to lack of
22/// configurability. For example, `1hour1m` is a valid friendly duration,
23/// but `SpanPrinter` cannot emit it due to a mixing of verbose and compact
24/// designator labels.)
25///
26/// # Advice
27///
28/// Since this parser has no configuration, there are generally only two reasons
29/// why you might want to use this type specifically:
30///
31/// 1. You need to parse from `&[u8]`.
32/// 2. You need to parse _only_ the "friendly" format.
33///
34/// Otherwise, you can use the `FromStr` implementations on both `Span` and
35/// `SignedDuration`, which automatically support the friendly format in
36/// addition to the ISO 8601 format simultaneously:
37///
38/// ```
39/// use jiff::{SignedDuration, Span, ToSpan};
40///
41/// let span: Span = "5 years, 2 months".parse()?;
42/// assert_eq!(span, 5.years().months(2).fieldwise());
43///
44/// let sdur: SignedDuration = "5 hours, 2 minutes".parse()?;
45/// assert_eq!(sdur, SignedDuration::new(5 * 60 * 60 + 2 * 60, 0));
46///
47/// # Ok::<(), Box<dyn std::error::Error>>(())
48/// ```
49///
50/// # Example
51///
52/// This example shows how to parse a `Span` directly from `&str`:
53///
54/// ```
55/// use jiff::{fmt::friendly::SpanParser, ToSpan};
56///
57/// static PARSER: SpanParser = SpanParser::new();
58///
59/// let string = "1 year, 3 months, 15:00:01.3";
60/// let span = PARSER.parse_span(string)?;
61/// assert_eq!(
62///     span,
63///     1.year().months(3).hours(15).seconds(1).milliseconds(300).fieldwise(),
64/// );
65///
66/// // Negative durations are supported too!
67/// let string = "1 year, 3 months, 15:00:01.3 ago";
68/// let span = PARSER.parse_span(string)?;
69/// assert_eq!(
70///     span,
71///     -1.year().months(3).hours(15).seconds(1).milliseconds(300).fieldwise(),
72/// );
73///
74/// # Ok::<(), Box<dyn std::error::Error>>(())
75/// ```
76#[derive(Clone, Debug, Default)]
77pub struct SpanParser {
78    _private: (),
79}
80
81impl SpanParser {
82    /// Creates a new parser for the "friendly" duration format.
83    ///
84    /// The parser returned uses the default configuration. (Although, at time
85    /// of writing, there are no available configuration options for this
86    /// parser.) This is identical to `SpanParser::default`, but it can be used
87    /// in a `const` context.
88    ///
89    /// # Example
90    ///
91    /// This example shows how to parse a `Span` directly from `&[u8]`:
92    ///
93    /// ```
94    /// use jiff::{fmt::friendly::SpanParser, ToSpan};
95    ///
96    /// static PARSER: SpanParser = SpanParser::new();
97    ///
98    /// let bytes = b"1 year 3 months 15 hours 1300ms";
99    /// let span = PARSER.parse_span(bytes)?;
100    /// assert_eq!(
101    ///     span,
102    ///     1.year().months(3).hours(15).milliseconds(1300).fieldwise(),
103    /// );
104    ///
105    /// # Ok::<(), Box<dyn std::error::Error>>(())
106    /// ```
107    #[inline]
108    pub const fn new() -> SpanParser {
109        SpanParser { _private: () }
110    }
111
112    /// Run the parser on the given string (which may be plain bytes) and,
113    /// if successful, return the parsed `Span`.
114    ///
115    /// See the [module documentation](super) for more details on the specific
116    /// grammar supported by this parser.
117    ///
118    /// # Example
119    ///
120    /// This shows a number of different duration formats that can be parsed
121    /// into a `Span`:
122    ///
123    /// ```
124    /// use jiff::{fmt::friendly::SpanParser, ToSpan};
125    ///
126    /// let spans = [
127    ///     ("40d", 40.days()),
128    ///     ("40 days", 40.days()),
129    ///     ("1y1d", 1.year().days(1)),
130    ///     ("1yr 1d", 1.year().days(1)),
131    ///     ("3d4h59m", 3.days().hours(4).minutes(59)),
132    ///     ("3 days, 4 hours, 59 minutes", 3.days().hours(4).minutes(59)),
133    ///     ("3d 4h 59m", 3.days().hours(4).minutes(59)),
134    ///     ("2h30m", 2.hours().minutes(30)),
135    ///     ("2h 30m", 2.hours().minutes(30)),
136    ///     ("1mo", 1.month()),
137    ///     ("1w", 1.week()),
138    ///     ("1 week", 1.week()),
139    ///     ("1w4d", 1.week().days(4)),
140    ///     ("1 wk 4 days", 1.week().days(4)),
141    ///     ("1m", 1.minute()),
142    ///     ("0.0021s", 2.milliseconds().microseconds(100)),
143    ///     ("0s", 0.seconds()),
144    ///     ("0d", 0.seconds()),
145    ///     ("0 days", 0.seconds()),
146    ///     (
147    ///         "1y1mo1d1h1m1.1s",
148    ///         1.year().months(1).days(1).hours(1).minutes(1).seconds(1).milliseconds(100),
149    ///     ),
150    ///     (
151    ///         "1yr 1mo 1day 1hr 1min 1.1sec",
152    ///         1.year().months(1).days(1).hours(1).minutes(1).seconds(1).milliseconds(100),
153    ///     ),
154    ///     (
155    ///         "1 year, 1 month, 1 day, 1 hour, 1 minute 1.1 seconds",
156    ///         1.year().months(1).days(1).hours(1).minutes(1).seconds(1).milliseconds(100),
157    ///     ),
158    ///     (
159    ///         "1 year, 1 month, 1 day, 01:01:01.1",
160    ///         1.year().months(1).days(1).hours(1).minutes(1).seconds(1).milliseconds(100),
161    ///     ),
162    ///     (
163    ///         "1 yr, 1 month, 1 d, 1 h, 1 min 1.1 second",
164    ///         1.year().months(1).days(1).hours(1).minutes(1).seconds(1).milliseconds(100),
165    ///     ),
166    /// ];
167    ///
168    /// static PARSER: SpanParser = SpanParser::new();
169    /// for (string, span) in spans {
170    ///     let parsed = PARSER.parse_span(string)?;
171    ///     assert_eq!(
172    ///         span.fieldwise(),
173    ///         parsed.fieldwise(),
174    ///         "result of parsing {string:?}",
175    ///     );
176    /// }
177    ///
178    /// # Ok::<(), Box<dyn std::error::Error>>(())
179    /// ```
180    #[inline]
181    pub fn parse_span<I: AsRef<[u8]>>(&self, input: I) -> Result<Span, Error> {
182        #[inline(never)]
183        fn imp(span_parser: &SpanParser, input: &[u8]) -> Result<Span, Error> {
184            let mut builder = DurationUnits::default();
185            let parsed = span_parser.parse(input, &mut builder)?;
186            let parsed = parsed.and_then(|_| builder.to_span())?;
187            parsed.into_full()
188        }
189
190        let input = input.as_ref();
191        imp(self, input).context(E::Failed)
192    }
193
194    /// Run the parser on the given string (which may be plain bytes) and,
195    /// if successful, return the parsed `SignedDuration`.
196    ///
197    /// See the [module documentation](super) for more details on the specific
198    /// grammar supported by this parser.
199    ///
200    /// # Example
201    ///
202    /// This shows a number of different duration formats that can be parsed
203    /// into a `SignedDuration`:
204    ///
205    /// ```
206    /// use jiff::{fmt::friendly::SpanParser, SignedDuration};
207    ///
208    /// let durations = [
209    ///     ("2h30m", SignedDuration::from_secs(2 * 60 * 60 + 30 * 60)),
210    ///     ("2 hrs 30 mins", SignedDuration::from_secs(2 * 60 * 60 + 30 * 60)),
211    ///     ("2 hours 30 minutes", SignedDuration::from_secs(2 * 60 * 60 + 30 * 60)),
212    ///     ("2 hrs 30 minutes", SignedDuration::from_secs(2 * 60 * 60 + 30 * 60)),
213    ///     ("2.5h", SignedDuration::from_secs(2 * 60 * 60 + 30 * 60)),
214    ///     ("1m", SignedDuration::from_mins(1)),
215    ///     ("1.5m", SignedDuration::from_secs(90)),
216    ///     ("0.0021s", SignedDuration::new(0, 2_100_000)),
217    ///     ("0s", SignedDuration::ZERO),
218    ///     ("0.000000001s", SignedDuration::from_nanos(1)),
219    /// ];
220    ///
221    /// static PARSER: SpanParser = SpanParser::new();
222    /// for (string, duration) in durations {
223    ///     let parsed = PARSER.parse_duration(string)?;
224    ///     assert_eq!(duration, parsed, "result of parsing {string:?}");
225    /// }
226    ///
227    /// # Ok::<(), Box<dyn std::error::Error>>(())
228    /// ```
229    #[inline]
230    pub fn parse_duration<I: AsRef<[u8]>>(
231        &self,
232        input: I,
233    ) -> Result<SignedDuration, Error> {
234        #[inline(never)]
235        fn imp(
236            span_parser: &SpanParser,
237            input: &[u8],
238        ) -> Result<SignedDuration, Error> {
239            let mut builder = DurationUnits::default();
240            let parsed = span_parser.parse(input, &mut builder)?;
241            let parsed = parsed.and_then(|_| builder.to_signed_duration())?;
242            parsed.into_full()
243        }
244
245        let input = input.as_ref();
246        imp(self, input).context(E::Failed)
247    }
248
249    /// Run the parser on the given string (which may be plain bytes) and,
250    /// if successful, return the parsed `std::time::Duration`.
251    ///
252    /// See the [module documentation](super) for more details on the specific
253    /// grammar supported by this parser.
254    ///
255    /// # Example
256    ///
257    /// This shows a number of different duration formats that can be parsed
258    /// into a `std::time::Duration`:
259    ///
260    /// ```
261    /// use std::time::Duration;
262    ///
263    /// use jiff::fmt::friendly::SpanParser;
264    ///
265    /// let durations = [
266    ///     ("2h30m", Duration::from_secs(2 * 60 * 60 + 30 * 60)),
267    ///     ("2 hrs 30 mins", Duration::from_secs(2 * 60 * 60 + 30 * 60)),
268    ///     ("2 hours 30 minutes", Duration::from_secs(2 * 60 * 60 + 30 * 60)),
269    ///     ("2 hrs 30 minutes", Duration::from_secs(2 * 60 * 60 + 30 * 60)),
270    ///     ("2.5h", Duration::from_secs(2 * 60 * 60 + 30 * 60)),
271    ///     ("1m", Duration::from_secs(1 * 60)),
272    ///     ("1.5m", Duration::from_secs(90)),
273    ///     ("0.0021s", Duration::new(0, 2_100_000)),
274    ///     ("0s", Duration::ZERO),
275    ///     ("0.000000001s", Duration::from_nanos(1)),
276    /// ];
277    ///
278    /// static PARSER: SpanParser = SpanParser::new();
279    /// for (string, duration) in durations {
280    ///     let parsed = PARSER.parse_unsigned_duration(string)?;
281    ///     assert_eq!(duration, parsed, "result of parsing {string:?}");
282    /// }
283    ///
284    /// # Ok::<(), Box<dyn std::error::Error>>(())
285    /// ```
286    #[inline]
287    pub fn parse_unsigned_duration<I: AsRef<[u8]>>(
288        &self,
289        input: I,
290    ) -> Result<core::time::Duration, Error> {
291        #[inline(never)]
292        fn imp(
293            span_parser: &SpanParser,
294            input: &[u8],
295        ) -> Result<core::time::Duration, Error> {
296            let mut builder = DurationUnits::default();
297            let parsed = span_parser.parse(input, &mut builder)?;
298            let parsed =
299                parsed.and_then(|_| builder.to_unsigned_duration())?;
300            let d = parsed.value;
301            parsed.into_full_with(format_args!("{d:?}"))
302        }
303
304        let input = input.as_ref();
305        imp(self, input).context(E::Failed)
306    }
307
308    #[cfg_attr(feature = "perf-inline", inline(always))]
309    fn parse<'i>(
310        &self,
311        input: &'i [u8],
312        builder: &mut DurationUnits,
313    ) -> Result<Parsed<'i, ()>, Error> {
314        if input.is_empty() {
315            return Err(Error::from(E::Empty));
316        }
317        // Guard prefix sign parsing to avoid the function call, which is
318        // marked unlineable to keep the fast path tighter.
319        let (sign, input) =
320            if !input.first().map_or(false, |&b| matches!(b, b'+' | b'-')) {
321                (None, input)
322            } else {
323                let Parsed { value: sign, input } =
324                    self.parse_prefix_sign(input);
325                (sign, input)
326            };
327
328        let Parsed { value, input } = self.parse_unit_value(input)?;
329        let Some(first_unit_value) = value else {
330            return Err(Error::from(E::ExpectedIntegerAfterSign));
331        };
332
333        let Parsed { input, .. } =
334            self.parse_duration_units(input, first_unit_value, builder)?;
335
336        // As with the prefix sign parsing, guard it to avoid calling the
337        // function.
338        let (sign, input) = if !input.first().map_or(false, is_whitespace) {
339            (sign.unwrap_or(Sign::Positive), input)
340        } else {
341            let parsed = self.parse_suffix_sign(sign, input)?;
342            (parsed.value, parsed.input)
343        };
344        builder.set_sign(sign);
345        Ok(Parsed { value: (), input })
346    }
347
348    #[cfg_attr(feature = "perf-inline", inline(always))]
349    fn parse_duration_units<'i>(
350        &self,
351        mut input: &'i [u8],
352        first_unit_value: u64,
353        builder: &mut DurationUnits,
354    ) -> Result<Parsed<'i, ()>, Error> {
355        let mut parsed_any_after_comma = true;
356        let mut value = first_unit_value;
357        loop {
358            let parsed = self.parse_hms_maybe(input, value)?;
359            input = parsed.input;
360            if let Some(hms) = parsed.value {
361                builder.set_hms(
362                    hms.hour,
363                    hms.minute,
364                    hms.second,
365                    hms.fraction,
366                )?;
367                break;
368            }
369
370            let fraction =
371                if input.first().map_or(false, |&b| b == b'.' || b == b',') {
372                    let parsed = parse_temporal_fraction(input)?;
373                    input = parsed.input;
374                    parsed.value
375                } else {
376                    None
377                };
378
379            // Eat any optional whitespace between the unit value and label.
380            input = self.parse_optional_whitespace(input).input;
381
382            // Parse the actual unit label/designator.
383            let parsed = self.parse_unit_designator(input)?;
384            input = parsed.input;
385            let unit = parsed.value;
386
387            // A comma is allowed to immediately follow the designator.
388            // Since this is a rarer case, we guard it with a check to see
389            // if the comma is there and only then call the function (which is
390            // marked unlineable to try and keep the hot path tighter).
391            if input.first().map_or(false, |&b| b == b',') {
392                input = self.parse_optional_comma(input)?.input;
393                parsed_any_after_comma = false;
394            }
395
396            builder.set_unit_value(unit, value)?;
397            if let Some(fraction) = fraction {
398                builder.set_fraction(fraction)?;
399                // Once we see a fraction, we are done. We don't permit parsing
400                // any more units. That is, a fraction can only occur on the
401                // lowest unit of time.
402                break;
403            }
404
405            // Eat any optional whitespace after the designator (or comma) and
406            // before the next unit value. But if we don't see a unit value,
407            // we don't eat the whitespace.
408            let after_whitespace = self.parse_optional_whitespace(input).input;
409            let parsed = self.parse_unit_value(after_whitespace)?;
410            value = match parsed.value {
411                None => break,
412                Some(value) => value,
413            };
414            input = parsed.input;
415            parsed_any_after_comma = true;
416        }
417        if !parsed_any_after_comma {
418            return Err(Error::from(E::ExpectedOneMoreUnitAfterComma));
419        }
420        Ok(Parsed { value: (), input })
421    }
422
423    /// This possibly parses a `HH:MM:SS[.fraction]`.
424    ///
425    /// This expects that a unit value has been parsed and looks for a `:`
426    /// at `input[0]`. If `:` is found, then this proceeds to parse HMS.
427    /// Otherwise, a `None` value is returned.
428    #[cfg_attr(feature = "perf-inline", inline(always))]
429    fn parse_hms_maybe<'i>(
430        &self,
431        input: &'i [u8],
432        hour: u64,
433    ) -> Result<Parsed<'i, Option<HMS>>, Error> {
434        let Some((&first, tail)) = input.split_first() else {
435            return Ok(Parsed { input, value: None });
436        };
437        if first != b':' {
438            return Ok(Parsed { input, value: None });
439        }
440        let Parsed { input, value } = self.parse_hms(tail, hour)?;
441        Ok(Parsed { input, value: Some(value) })
442    }
443
444    /// This parses a `HH:MM:SS[.fraction]` when it is known/expected to be
445    /// present.
446    ///
447    /// This is also marked as non-inlined since we expect this to be a
448    /// less common case. Where as `parse_hms_maybe` is called unconditionally
449    /// to check to see if the HMS should be parsed.
450    ///
451    /// This assumes that the beginning of `input` immediately follows the
452    /// first `:` in `HH:MM:SS[.fraction]`.
453    #[inline(never)]
454    fn parse_hms<'i>(
455        &self,
456        input: &'i [u8],
457        hour: u64,
458    ) -> Result<Parsed<'i, HMS>, Error> {
459        let Parsed { input, value } = self.parse_unit_value(input)?;
460        let minute = value.ok_or(E::ExpectedMinuteAfterHour)?;
461
462        let (&first, input) =
463            input.split_first().ok_or(E::ExpectedColonAfterMinute)?;
464        if first != b':' {
465            return Err(Error::from(E::ExpectedColonAfterMinute));
466        }
467
468        let Parsed { input, value } = self.parse_unit_value(input)?;
469        let second = value.ok_or(E::ExpectedSecondAfterMinute)?;
470        let (fraction, input) =
471            if input.first().map_or(false, |&b| b == b'.' || b == b',') {
472                let parsed = parse_temporal_fraction(input)?;
473                (parsed.value, parsed.input)
474            } else {
475                (None, input)
476            };
477        let hms = HMS { hour, minute, second, fraction };
478        Ok(Parsed { input, value: hms })
479    }
480
481    /// Parsed a unit value, i.e., an integer.
482    ///
483    /// If no digits (`[0-9]`) were found at the current position of the parser
484    /// then `None` is returned. This means, for example, that parsing a
485    /// duration should stop.
486    ///
487    /// Note that this is safe to call on untrusted input. It will not attempt
488    /// to consume more input than could possibly fit into a parsed integer.
489    ///
490    /// Since this returns a `u64`, it is possible that an integer that cannot
491    /// fit into an `i64` is returned. Callers should handle this. (Indeed,
492    /// `DurationUnits` handles this case.)
493    #[cfg_attr(feature = "perf-inline", inline(always))]
494    fn parse_unit_value<'i>(
495        &self,
496        input: &'i [u8],
497    ) -> Result<Parsed<'i, Option<u64>>, Error> {
498        let (value, input) = parse::u64_prefix(input)?;
499        Ok(Parsed { value, input })
500    }
501
502    /// Parse a unit designator, e.g., `years` or `nano`.
503    ///
504    /// If no designator could be found, including if the given `input` is
505    /// empty, then this return an error.
506    ///
507    /// This does not attempt to handle leading or trailing whitespace.
508    #[cfg_attr(feature = "perf-inline", inline(always))]
509    fn parse_unit_designator<'i>(
510        &self,
511        input: &'i [u8],
512    ) -> Result<Parsed<'i, Unit>, Error> {
513        let (unit, len) =
514            parser_label::find(input).ok_or(E::ExpectedUnitSuffix)?;
515        Ok(Parsed { value: unit, input: &input[len..] })
516    }
517
518    /// Parses an optional prefix sign from the given input.
519    ///
520    /// A prefix sign is either a `+` or a `-`. If neither are found, then
521    /// `None` is returned.
522    #[inline(never)]
523    fn parse_prefix_sign<'i>(
524        &self,
525        input: &'i [u8],
526    ) -> Parsed<'i, Option<Sign>> {
527        let Some(sign) = input.first().copied() else {
528            return Parsed { value: None, input };
529        };
530        let sign = if sign == b'+' {
531            Sign::Positive
532        } else if sign == b'-' {
533            Sign::Negative
534        } else {
535            return Parsed { value: None, input };
536        };
537        Parsed { value: Some(sign), input: &input[1..] }
538    }
539
540    /// Parses an optional suffix sign from the given input.
541    ///
542    /// This requires, as input, the result of parsing a prefix sign since this
543    /// will return an error if both a prefix and a suffix sign were found.
544    ///
545    /// A suffix sign is the string `ago`. Any other string means that there is
546    /// no suffix sign. This will also look for mandatory whitespace and eat
547    /// any additional optional whitespace. i.e., This should be called
548    /// immediately after parsing the last unit designator/label.
549    ///
550    /// Regardless of whether a prefix or suffix sign was found, a definitive
551    /// sign is returned. (When there's no prefix or suffix sign, then the sign
552    /// returned is positive.)
553    #[inline(never)]
554    fn parse_suffix_sign<'i>(
555        &self,
556        prefix_sign: Option<Sign>,
557        mut input: &'i [u8],
558    ) -> Result<Parsed<'i, Sign>, Error> {
559        if !input.first().map_or(false, is_whitespace) {
560            let sign = prefix_sign.unwrap_or(Sign::Positive);
561            return Ok(Parsed { value: sign, input });
562        }
563        // Eat any additional whitespace we find before looking for 'ago'.
564        input = self.parse_optional_whitespace(&input[1..]).input;
565        let (suffix_sign, input) =
566            if let Some(tail) = input.strip_prefix(b"ago") {
567                (Some(Sign::Negative), tail)
568            } else {
569                (None, input)
570            };
571        let sign = match (prefix_sign, suffix_sign) {
572            (Some(_), Some(_)) => {
573                return Err(Error::from(E::ExpectedOneSign));
574            }
575            (Some(sign), None) => sign,
576            (None, Some(sign)) => sign,
577            (None, None) => Sign::Positive,
578        };
579        Ok(Parsed { value: sign, input })
580    }
581
582    /// Parses an optional comma following a unit designator.
583    ///
584    /// If a comma is seen, then it is mandatory that it be followed by
585    /// whitespace.
586    ///
587    /// This also takes care to provide a custom error message if the end of
588    /// input is seen after a comma.
589    ///
590    /// If `input` doesn't start with a comma, then this is a no-op.
591    #[inline(never)]
592    fn parse_optional_comma<'i>(
593        &self,
594        input: &'i [u8],
595    ) -> Result<Parsed<'i, ()>, Error> {
596        let Some((&first, tail)) = input.split_first() else {
597            return Ok(Parsed { value: (), input });
598        };
599        if first != b',' {
600            return Ok(Parsed { value: (), input });
601        }
602
603        let (second, input) = tail
604            .split_first()
605            .ok_or(E::ExpectedWhitespaceAfterCommaEndOfInput)?;
606        if !is_whitespace(second) {
607            return Err(Error::from(E::ExpectedWhitespaceAfterComma {
608                byte: *second,
609            }));
610        }
611        Ok(Parsed { value: (), input })
612    }
613
614    /// Parses zero or more bytes of ASCII whitespace.
615    #[cfg_attr(feature = "perf-inline", inline(always))]
616    fn parse_optional_whitespace<'i>(
617        &self,
618        mut input: &'i [u8],
619    ) -> Parsed<'i, ()> {
620        while input.first().map_or(false, is_whitespace) {
621            input = &input[1..];
622        }
623        Parsed { value: (), input }
624    }
625}
626
627/// A type that represents the parsed components of `HH:MM:SS[.fraction]`.
628#[derive(Debug)]
629struct HMS {
630    hour: u64,
631    minute: u64,
632    second: u64,
633    fraction: Option<u32>,
634}
635
636/// Returns true if the byte is ASCII whitespace.
637#[cfg_attr(feature = "perf-inline", inline(always))]
638fn is_whitespace(byte: &u8) -> bool {
639    matches!(*byte, b' ' | b'\t' | b'\n' | b'\r' | b'\x0C')
640}
641
642#[cfg(feature = "alloc")]
643#[cfg(test)]
644mod tests {
645    use super::*;
646
647    #[test]
648    fn parse_span_basic() {
649        let p = |s: &str| SpanParser::new().parse_span(s).unwrap();
650
651        insta::assert_snapshot!(p("5 years"), @"P5Y");
652        insta::assert_snapshot!(p("5 years 4 months"), @"P5Y4M");
653        insta::assert_snapshot!(p("5 years 4 months 3 hours"), @"P5Y4MT3H");
654        insta::assert_snapshot!(p("5 years, 4 months, 3 hours"), @"P5Y4MT3H");
655
656        insta::assert_snapshot!(p("01:02:03"), @"PT1H2M3S");
657        insta::assert_snapshot!(p("5 days 01:02:03"), @"P5DT1H2M3S");
658        // This is Python's `str(timedelta)` format!
659        insta::assert_snapshot!(p("5 days, 01:02:03"), @"P5DT1H2M3S");
660        insta::assert_snapshot!(p("3yrs 5 days 01:02:03"), @"P3Y5DT1H2M3S");
661        insta::assert_snapshot!(p("3yrs 5 days, 01:02:03"), @"P3Y5DT1H2M3S");
662        insta::assert_snapshot!(
663            p("3yrs 5 days, 01:02:03.123456789"),
664            @"P3Y5DT1H2M3.123456789S",
665        );
666        insta::assert_snapshot!(p("999:999:999"), @"PT999H999M999S");
667    }
668
669    #[test]
670    fn parse_span_fractional() {
671        let p = |s: &str| SpanParser::new().parse_span(s).unwrap();
672
673        insta::assert_snapshot!(p("1.5hrs"), @"PT1H30M");
674        insta::assert_snapshot!(p("1.5mins"), @"PT1M30S");
675        insta::assert_snapshot!(p("1.5secs"), @"PT1.5S");
676        insta::assert_snapshot!(p("1.5msecs"), @"PT0.0015S");
677        insta::assert_snapshot!(p("1.5µsecs"), @"PT0.0000015S");
678
679        insta::assert_snapshot!(p("1d 1.5hrs"), @"P1DT1H30M");
680        insta::assert_snapshot!(p("1h 1.5mins"), @"PT1H1M30S");
681        insta::assert_snapshot!(p("1m 1.5secs"), @"PT1M1.5S");
682        insta::assert_snapshot!(p("1s 1.5msecs"), @"PT1.0015S");
683        insta::assert_snapshot!(p("1ms 1.5µsecs"), @"PT0.0010015S");
684
685        insta::assert_snapshot!(p("1s2000ms"), @"PT3S");
686    }
687
688    #[test]
689    fn parse_span_boundaries() {
690        let p = |s: &str| SpanParser::new().parse_span(s).unwrap();
691
692        insta::assert_snapshot!(p("19998 years"), @"P19998Y");
693        insta::assert_snapshot!(p("19998 years ago"), @"-P19998Y");
694        insta::assert_snapshot!(p("239976 months"), @"P239976M");
695        insta::assert_snapshot!(p("239976 months ago"), @"-P239976M");
696        insta::assert_snapshot!(p("1043497 weeks"), @"P1043497W");
697        insta::assert_snapshot!(p("1043497 weeks ago"), @"-P1043497W");
698        insta::assert_snapshot!(p("7304484 days"), @"P7304484D");
699        insta::assert_snapshot!(p("7304484 days ago"), @"-P7304484D");
700        insta::assert_snapshot!(p("175307616 hours"), @"PT175307616H");
701        insta::assert_snapshot!(p("175307616 hours ago"), @"-PT175307616H");
702        insta::assert_snapshot!(p("10518456960 minutes"), @"PT10518456960M");
703        insta::assert_snapshot!(p("10518456960 minutes ago"), @"-PT10518456960M");
704        insta::assert_snapshot!(p("631107417600 seconds"), @"PT631107417600S");
705        insta::assert_snapshot!(p("631107417600 seconds ago"), @"-PT631107417600S");
706        insta::assert_snapshot!(p("631107417600000 milliseconds"), @"PT631107417600S");
707        insta::assert_snapshot!(p("631107417600000 milliseconds ago"), @"-PT631107417600S");
708        insta::assert_snapshot!(p("631107417600000000 microseconds"), @"PT631107417600S");
709        insta::assert_snapshot!(p("631107417600000000 microseconds ago"), @"-PT631107417600S");
710        insta::assert_snapshot!(p("9223372036854775807 nanoseconds"), @"PT9223372036.854775807S");
711        insta::assert_snapshot!(p("9223372036854775807 nanoseconds ago"), @"-PT9223372036.854775807S");
712
713        insta::assert_snapshot!(p("175307617 hours"), @"PT175307616H60M");
714        insta::assert_snapshot!(p("175307617 hours ago"), @"-PT175307616H60M");
715        insta::assert_snapshot!(p("10518456961 minutes"), @"PT10518456960M60S");
716        insta::assert_snapshot!(p("10518456961 minutes ago"), @"-PT10518456960M60S");
717        insta::assert_snapshot!(p("631107417601 seconds"), @"PT631107417601S");
718        insta::assert_snapshot!(p("631107417601 seconds ago"), @"-PT631107417601S");
719        insta::assert_snapshot!(p("631107417600001 milliseconds"), @"PT631107417600.001S");
720        insta::assert_snapshot!(p("631107417600001 milliseconds ago"), @"-PT631107417600.001S");
721        insta::assert_snapshot!(p("631107417600000001 microseconds"), @"PT631107417600.000001S");
722        insta::assert_snapshot!(p("631107417600000001 microseconds ago"), @"-PT631107417600.000001S");
723        // We don't include nanoseconds here, because that will fail to
724        // parse due to overflowing i64.
725    }
726
727    #[test]
728    fn err_span_basic() {
729        let p = |s: &str| SpanParser::new().parse_span(s).unwrap_err();
730
731        insta::assert_snapshot!(
732            p(""),
733            @r#"failed to parse input in the "friendly" duration format: an empty string is not valid"#,
734        );
735        insta::assert_snapshot!(
736            p(" "),
737            @r#"failed to parse input in the "friendly" duration format: expected duration to start with a unit value (a decimal integer) after an optional sign, but no integer was found"#,
738        );
739        insta::assert_snapshot!(
740            p("a"),
741            @r#"failed to parse input in the "friendly" duration format: expected duration to start with a unit value (a decimal integer) after an optional sign, but no integer was found"#,
742        );
743        insta::assert_snapshot!(
744            p("2 months 1 year"),
745            @r#"failed to parse input in the "friendly" duration format: found value with unit year after unit month, but units must be written from largest to smallest (and they can't be repeated)"#,
746        );
747        insta::assert_snapshot!(
748            p("1 year 1 mont"),
749            @r#"failed to parse input in the "friendly" duration format: parsed value 'P1Y1M', but unparsed input "nt" remains (expected no unparsed input)"#,
750        );
751        insta::assert_snapshot!(
752            p("2 months,"),
753            @r#"failed to parse input in the "friendly" duration format: expected whitespace after comma, but found end of input"#,
754        );
755        insta::assert_snapshot!(
756            p("2 months, "),
757            @r#"failed to parse input in the "friendly" duration format: found comma at the end of duration, but a comma indicates at least one more unit follows"#,
758        );
759        insta::assert_snapshot!(
760            p("2 months ,"),
761            @r#"failed to parse input in the "friendly" duration format: parsed value 'P2M', but unparsed input "," remains (expected no unparsed input)"#,
762        );
763    }
764
765    #[test]
766    fn err_span_sign() {
767        let p = |s: &str| SpanParser::new().parse_span(s).unwrap_err();
768
769        insta::assert_snapshot!(
770            p("1yago"),
771            @r#"failed to parse input in the "friendly" duration format: parsed value 'P1Y', but unparsed input "ago" remains (expected no unparsed input)"#,
772        );
773        insta::assert_snapshot!(
774            p("1 year 1 monthago"),
775            @r#"failed to parse input in the "friendly" duration format: parsed value 'P1Y1M', but unparsed input "ago" remains (expected no unparsed input)"#,
776        );
777        insta::assert_snapshot!(
778            p("+1 year 1 month ago"),
779            @r#"failed to parse input in the "friendly" duration format: expected to find either a prefix sign (+/-) or a suffix sign (`ago`), but found both"#,
780        );
781        insta::assert_snapshot!(
782            p("-1 year 1 month ago"),
783            @r#"failed to parse input in the "friendly" duration format: expected to find either a prefix sign (+/-) or a suffix sign (`ago`), but found both"#,
784        );
785    }
786
787    #[test]
788    fn err_span_overflow_fraction() {
789        let p = |s: &str| SpanParser::new().parse_span(s).unwrap();
790        let pe = |s: &str| SpanParser::new().parse_span(s).unwrap_err();
791
792        insta::assert_snapshot!(
793            // One fewer micro, and this parses okay. The error occurs because
794            // the maximum number of microseconds is subtracted off, and we're
795            // left over with a value that overflows an i64.
796            pe("640330789636854776 micros"),
797            @r#"failed to parse input in the "friendly" duration format: failed to set value for microsecond unit on span: failed to set nanosecond value from fractional component"#,
798        );
799        // one fewer is okay
800        insta::assert_snapshot!(
801            p("640330789636854775 micros"),
802            @"PT640330789636.854775S"
803        );
804
805        insta::assert_snapshot!(
806            // This is like the test above, but actually exercises a slightly
807            // different error path by using an explicit fraction. Here, if
808            // we had x.807 micros, it would parse successfully.
809            pe("640330789636854775.808 micros"),
810            @r#"failed to parse input in the "friendly" duration format: failed to set nanosecond value from fractional component"#,
811        );
812        // one fewer is okay
813        insta::assert_snapshot!(
814            p("640330789636854775.807 micros"),
815            @"PT640330789636.854775807S"
816        );
817    }
818
819    #[test]
820    fn err_span_overflow_units() {
821        let p = |s: &str| SpanParser::new().parse_span(s).unwrap_err();
822
823        insta::assert_snapshot!(
824            p("19999 years"),
825            @r#"failed to parse input in the "friendly" duration format: failed to set value for year unit on span: parameter 'years' with value 19999 is not in the required range of -19998..=19998"#,
826        );
827        insta::assert_snapshot!(
828            p("19999 years ago"),
829            @r#"failed to parse input in the "friendly" duration format: failed to set value for year unit on span: parameter 'years' with value -19999 is not in the required range of -19998..=19998"#,
830        );
831
832        insta::assert_snapshot!(
833            p("239977 months"),
834            @r#"failed to parse input in the "friendly" duration format: failed to set value for month unit on span: parameter 'months' with value 239977 is not in the required range of -239976..=239976"#,
835        );
836        insta::assert_snapshot!(
837            p("239977 months ago"),
838            @r#"failed to parse input in the "friendly" duration format: failed to set value for month unit on span: parameter 'months' with value -239977 is not in the required range of -239976..=239976"#,
839        );
840
841        insta::assert_snapshot!(
842            p("1043498 weeks"),
843            @r#"failed to parse input in the "friendly" duration format: failed to set value for week unit on span: parameter 'weeks' with value 1043498 is not in the required range of -1043497..=1043497"#,
844        );
845        insta::assert_snapshot!(
846            p("1043498 weeks ago"),
847            @r#"failed to parse input in the "friendly" duration format: failed to set value for week unit on span: parameter 'weeks' with value -1043498 is not in the required range of -1043497..=1043497"#,
848        );
849
850        insta::assert_snapshot!(
851            p("7304485 days"),
852            @r#"failed to parse input in the "friendly" duration format: failed to set value for day unit on span: parameter 'days' with value 7304485 is not in the required range of -7304484..=7304484"#,
853        );
854        insta::assert_snapshot!(
855            p("7304485 days ago"),
856            @r#"failed to parse input in the "friendly" duration format: failed to set value for day unit on span: parameter 'days' with value -7304485 is not in the required range of -7304484..=7304484"#,
857        );
858
859        insta::assert_snapshot!(
860            p("9223372036854775808 nanoseconds"),
861            @r#"failed to parse input in the "friendly" duration format: value for nanoseconds is too big (or small) to fit into a signed 64-bit integer"#,
862        );
863        insta::assert_snapshot!(
864            p("9223372036854775808 nanoseconds ago"),
865            @r#"failed to parse input in the "friendly" duration format: failed to set value for nanosecond unit on span: parameter 'nanoseconds' with value -9223372036854775808 is not in the required range of -9223372036854775807..=9223372036854775807"#,
866        );
867    }
868
869    #[test]
870    fn err_span_fraction() {
871        let p = |s: &str| SpanParser::new().parse_span(s).unwrap_err();
872
873        insta::assert_snapshot!(
874            p("1.5 years"),
875            @r#"failed to parse input in the "friendly" duration format: fractional years are not supported"#,
876        );
877        insta::assert_snapshot!(
878            p("1.5 nanos"),
879            @r#"failed to parse input in the "friendly" duration format: fractional nanoseconds are not supported"#,
880        );
881    }
882
883    #[test]
884    fn err_span_hms() {
885        let p = |s: &str| SpanParser::new().parse_span(s).unwrap_err();
886
887        insta::assert_snapshot!(
888            p("05:"),
889            @r#"failed to parse input in the "friendly" duration format: when parsing the `HH:MM:SS` format, expected to parse minute following hour"#,
890        );
891        insta::assert_snapshot!(
892            p("05:06"),
893            @r#"failed to parse input in the "friendly" duration format: when parsing the `HH:MM:SS` format, expected to parse `:` following minute"#,
894        );
895        insta::assert_snapshot!(
896            p("05:06:"),
897            @r#"failed to parse input in the "friendly" duration format: when parsing the `HH:MM:SS` format, expected to parse second following minute"#,
898        );
899        insta::assert_snapshot!(
900            p("2 hours, 05:06:07"),
901            @r#"failed to parse input in the "friendly" duration format: found `HH:MM:SS` after unit hour, but `HH:MM:SS` can only appear after years, months, weeks or days"#,
902        );
903    }
904
905    #[test]
906    fn parse_signed_duration_basic() {
907        let p = |s: &str| SpanParser::new().parse_duration(s).unwrap();
908
909        insta::assert_snapshot!(p("1 hour, 2 minutes, 3 seconds"), @"PT1H2M3S");
910        insta::assert_snapshot!(p("01:02:03"), @"PT1H2M3S");
911        insta::assert_snapshot!(p("999:999:999"), @"PT1015H55M39S");
912    }
913
914    #[test]
915    fn parse_signed_duration_negate() {
916        let p = |s: &str| SpanParser::new().parse_duration(s).unwrap();
917        let perr = |s: &str| SpanParser::new().parse_duration(s).unwrap_err();
918
919        insta::assert_snapshot!(
920            p("9223372036854775807s"),
921            @"PT2562047788015215H30M7S",
922        );
923        insta::assert_snapshot!(
924            perr("9223372036854775808s"),
925            @r#"failed to parse input in the "friendly" duration format: value for seconds is too big (or small) to fit into a signed 64-bit integer"#,
926        );
927        insta::assert_snapshot!(
928            p("-9223372036854775808s"),
929            @"-PT2562047788015215H30M8S",
930        );
931    }
932
933    #[test]
934    fn parse_signed_duration_fractional() {
935        let p = |s: &str| SpanParser::new().parse_duration(s).unwrap();
936
937        insta::assert_snapshot!(p("1.5hrs"), @"PT1H30M");
938        insta::assert_snapshot!(p("1.5mins"), @"PT1M30S");
939        insta::assert_snapshot!(p("1.5secs"), @"PT1.5S");
940        insta::assert_snapshot!(p("1.5msecs"), @"PT0.0015S");
941        insta::assert_snapshot!(p("1.5µsecs"), @"PT0.0000015S");
942
943        insta::assert_snapshot!(p("1h 1.5mins"), @"PT1H1M30S");
944        insta::assert_snapshot!(p("1m 1.5secs"), @"PT1M1.5S");
945        insta::assert_snapshot!(p("1s 1.5msecs"), @"PT1.0015S");
946        insta::assert_snapshot!(p("1ms 1.5µsecs"), @"PT0.0010015S");
947
948        insta::assert_snapshot!(p("1s2000ms"), @"PT3S");
949    }
950
951    #[test]
952    fn parse_signed_duration_boundaries() {
953        let p = |s: &str| SpanParser::new().parse_duration(s).unwrap();
954        let pe = |s: &str| SpanParser::new().parse_duration(s).unwrap_err();
955
956        insta::assert_snapshot!(p("175307616 hours"), @"PT175307616H");
957        insta::assert_snapshot!(p("175307616 hours ago"), @"-PT175307616H");
958        insta::assert_snapshot!(p("10518456960 minutes"), @"PT175307616H");
959        insta::assert_snapshot!(p("10518456960 minutes ago"), @"-PT175307616H");
960        insta::assert_snapshot!(p("631107417600 seconds"), @"PT175307616H");
961        insta::assert_snapshot!(p("631107417600 seconds ago"), @"-PT175307616H");
962        insta::assert_snapshot!(p("631107417600000 milliseconds"), @"PT175307616H");
963        insta::assert_snapshot!(p("631107417600000 milliseconds ago"), @"-PT175307616H");
964        insta::assert_snapshot!(p("631107417600000000 microseconds"), @"PT175307616H");
965        insta::assert_snapshot!(p("631107417600000000 microseconds ago"), @"-PT175307616H");
966        insta::assert_snapshot!(p("9223372036854775807 nanoseconds"), @"PT2562047H47M16.854775807S");
967        insta::assert_snapshot!(p("9223372036854775807 nanoseconds ago"), @"-PT2562047H47M16.854775807S");
968
969        insta::assert_snapshot!(p("175307617 hours"), @"PT175307617H");
970        insta::assert_snapshot!(p("175307617 hours ago"), @"-PT175307617H");
971        insta::assert_snapshot!(p("10518456961 minutes"), @"PT175307616H1M");
972        insta::assert_snapshot!(p("10518456961 minutes ago"), @"-PT175307616H1M");
973        insta::assert_snapshot!(p("631107417601 seconds"), @"PT175307616H1S");
974        insta::assert_snapshot!(p("631107417601 seconds ago"), @"-PT175307616H1S");
975        insta::assert_snapshot!(p("631107417600001 milliseconds"), @"PT175307616H0.001S");
976        insta::assert_snapshot!(p("631107417600001 milliseconds ago"), @"-PT175307616H0.001S");
977        insta::assert_snapshot!(p("631107417600000001 microseconds"), @"PT175307616H0.000001S");
978        insta::assert_snapshot!(p("631107417600000001 microseconds ago"), @"-PT175307616H0.000001S");
979        // We don't include nanoseconds here, because that will fail to
980        // parse due to overflowing i64.
981
982        // The above were copied from the corresponding `Span` test, which has
983        // tighter limits on components. But a `SignedDuration` supports the
984        // full range of `i64` seconds.
985        insta::assert_snapshot!(p("2562047788015215hours"), @"PT2562047788015215H");
986        insta::assert_snapshot!(p("-2562047788015215hours"), @"-PT2562047788015215H");
987        insta::assert_snapshot!(
988            pe("2562047788015216hrs"),
989            @r#"failed to parse input in the "friendly" duration format: accumulated duration overflowed when adding value to unit hour"#,
990        );
991
992        insta::assert_snapshot!(p("153722867280912930minutes"), @"PT2562047788015215H30M");
993        insta::assert_snapshot!(p("153722867280912930minutes ago"), @"-PT2562047788015215H30M");
994        insta::assert_snapshot!(
995            pe("153722867280912931mins"),
996            @r#"failed to parse input in the "friendly" duration format: accumulated duration overflowed when adding value to unit minute"#,
997        );
998
999        insta::assert_snapshot!(p("9223372036854775807seconds"), @"PT2562047788015215H30M7S");
1000        insta::assert_snapshot!(p("-9223372036854775807seconds"), @"-PT2562047788015215H30M7S");
1001        insta::assert_snapshot!(
1002            pe("9223372036854775808s"),
1003            @r#"failed to parse input in the "friendly" duration format: value for seconds is too big (or small) to fit into a signed 64-bit integer"#,
1004        );
1005        insta::assert_snapshot!(
1006            p("-9223372036854775808s"),
1007            @"-PT2562047788015215H30M8S",
1008        );
1009    }
1010
1011    #[test]
1012    fn err_signed_duration_basic() {
1013        let p = |s: &str| SpanParser::new().parse_duration(s).unwrap_err();
1014
1015        insta::assert_snapshot!(
1016            p(""),
1017            @r#"failed to parse input in the "friendly" duration format: an empty string is not valid"#,
1018        );
1019        insta::assert_snapshot!(
1020            p(" "),
1021            @r#"failed to parse input in the "friendly" duration format: expected duration to start with a unit value (a decimal integer) after an optional sign, but no integer was found"#,
1022        );
1023        insta::assert_snapshot!(
1024            p("5"),
1025            @r#"failed to parse input in the "friendly" duration format: expected to find unit designator suffix (e.g., `years` or `secs`) after parsing integer"#,
1026        );
1027        insta::assert_snapshot!(
1028            p("a"),
1029            @r#"failed to parse input in the "friendly" duration format: expected duration to start with a unit value (a decimal integer) after an optional sign, but no integer was found"#,
1030        );
1031        insta::assert_snapshot!(
1032            p("2 minutes 1 hour"),
1033            @r#"failed to parse input in the "friendly" duration format: found value with unit hour after unit minute, but units must be written from largest to smallest (and they can't be repeated)"#,
1034        );
1035        insta::assert_snapshot!(
1036            p("1 hour 1 minut"),
1037            @r#"failed to parse input in the "friendly" duration format: parsed value 'PT1H1M', but unparsed input "ut" remains (expected no unparsed input)"#,
1038        );
1039        insta::assert_snapshot!(
1040            p("2 minutes,"),
1041            @r#"failed to parse input in the "friendly" duration format: expected whitespace after comma, but found end of input"#,
1042        );
1043        insta::assert_snapshot!(
1044            p("2 minutes, "),
1045            @r#"failed to parse input in the "friendly" duration format: found comma at the end of duration, but a comma indicates at least one more unit follows"#,
1046        );
1047        insta::assert_snapshot!(
1048            p("2 minutes ,"),
1049            @r#"failed to parse input in the "friendly" duration format: parsed value 'PT2M', but unparsed input "," remains (expected no unparsed input)"#,
1050        );
1051    }
1052
1053    #[test]
1054    fn err_signed_duration_sign() {
1055        let p = |s: &str| SpanParser::new().parse_duration(s).unwrap_err();
1056
1057        insta::assert_snapshot!(
1058            p("1hago"),
1059            @r#"failed to parse input in the "friendly" duration format: parsed value 'PT1H', but unparsed input "ago" remains (expected no unparsed input)"#,
1060        );
1061        insta::assert_snapshot!(
1062            p("1 hour 1 minuteago"),
1063            @r#"failed to parse input in the "friendly" duration format: parsed value 'PT1H1M', but unparsed input "ago" remains (expected no unparsed input)"#,
1064        );
1065        insta::assert_snapshot!(
1066            p("+1 hour 1 minute ago"),
1067            @r#"failed to parse input in the "friendly" duration format: expected to find either a prefix sign (+/-) or a suffix sign (`ago`), but found both"#,
1068        );
1069        insta::assert_snapshot!(
1070            p("-1 hour 1 minute ago"),
1071            @r#"failed to parse input in the "friendly" duration format: expected to find either a prefix sign (+/-) or a suffix sign (`ago`), but found both"#,
1072        );
1073    }
1074
1075    #[test]
1076    fn err_signed_duration_overflow_fraction() {
1077        let p = |s: &str| SpanParser::new().parse_duration(s).unwrap();
1078        let pe = |s: &str| SpanParser::new().parse_duration(s).unwrap_err();
1079
1080        insta::assert_snapshot!(
1081            // Unlike `Span`, this just overflows because it can't be parsed
1082            // as a 64-bit integer.
1083            pe("9223372036854775808 micros"),
1084            @r#"failed to parse input in the "friendly" duration format: value for microseconds is too big (or small) to fit into a signed 64-bit integer"#,
1085        );
1086        // one fewer is okay
1087        insta::assert_snapshot!(
1088            p("9223372036854775807 micros"),
1089            @"PT2562047788H54.775807S"
1090        );
1091    }
1092
1093    #[test]
1094    fn err_signed_duration_fraction() {
1095        let p = |s: &str| SpanParser::new().parse_duration(s).unwrap_err();
1096
1097        insta::assert_snapshot!(
1098            p("1.5 nanos"),
1099            @r#"failed to parse input in the "friendly" duration format: fractional nanoseconds are not supported"#,
1100        );
1101    }
1102
1103    #[test]
1104    fn err_signed_duration_hms() {
1105        let p = |s: &str| SpanParser::new().parse_duration(s).unwrap_err();
1106
1107        insta::assert_snapshot!(
1108            p("05:"),
1109            @r#"failed to parse input in the "friendly" duration format: when parsing the `HH:MM:SS` format, expected to parse minute following hour"#,
1110        );
1111        insta::assert_snapshot!(
1112            p("05:06"),
1113            @r#"failed to parse input in the "friendly" duration format: when parsing the `HH:MM:SS` format, expected to parse `:` following minute"#,
1114        );
1115        insta::assert_snapshot!(
1116            p("05:06:"),
1117            @r#"failed to parse input in the "friendly" duration format: when parsing the `HH:MM:SS` format, expected to parse second following minute"#,
1118        );
1119        insta::assert_snapshot!(
1120            p("2 hours, 05:06:07"),
1121            @r#"failed to parse input in the "friendly" duration format: found `HH:MM:SS` after unit hour, but `HH:MM:SS` can only appear after years, months, weeks or days"#,
1122        );
1123    }
1124
1125    #[test]
1126    fn parse_unsigned_duration_basic() {
1127        let p = |s: &str| {
1128            let dur = SpanParser::new().parse_unsigned_duration(s).unwrap();
1129            crate::fmt::temporal::SpanPrinter::new()
1130                .unsigned_duration_to_string(&dur)
1131        };
1132
1133        insta::assert_snapshot!(
1134            p("1 hour, 2 minutes, 3 seconds"),
1135            @"PT1H2M3S",
1136        );
1137        insta::assert_snapshot!(p("01:02:03"), @"PT1H2M3S");
1138        insta::assert_snapshot!(p("999:999:999"), @"PT1015H55M39S");
1139        insta::assert_snapshot!(
1140            p("+1hr"),
1141            @"PT1H",
1142        );
1143    }
1144
1145    #[test]
1146    fn parse_unsigned_duration_negate() {
1147        let p = |s: &str| {
1148            let dur = SpanParser::new().parse_unsigned_duration(s).unwrap();
1149            crate::fmt::temporal::SpanPrinter::new()
1150                .unsigned_duration_to_string(&dur)
1151        };
1152        let perr = |s: &str| {
1153            SpanParser::new().parse_unsigned_duration(s).unwrap_err()
1154        };
1155
1156        insta::assert_snapshot!(
1157            p("18446744073709551615s"),
1158            @"PT5124095576030431H15S",
1159        );
1160        insta::assert_snapshot!(
1161            perr("18446744073709551616s"),
1162            @r#"failed to parse input in the "friendly" duration format: number too big to parse into 64-bit integer"#,
1163        );
1164        insta::assert_snapshot!(
1165            perr("-1s"),
1166            @r#"failed to parse input in the "friendly" duration format: cannot parse negative duration into unsigned `std::time::Duration`"#,
1167        );
1168    }
1169
1170    #[test]
1171    fn parse_unsigned_duration_fractional() {
1172        let p = |s: &str| {
1173            let dur = SpanParser::new().parse_unsigned_duration(s).unwrap();
1174            crate::fmt::temporal::SpanPrinter::new()
1175                .unsigned_duration_to_string(&dur)
1176        };
1177
1178        insta::assert_snapshot!(p("1.5hrs"), @"PT1H30M");
1179        insta::assert_snapshot!(p("1.5mins"), @"PT1M30S");
1180        insta::assert_snapshot!(p("1.5secs"), @"PT1.5S");
1181        insta::assert_snapshot!(p("1.5msecs"), @"PT0.0015S");
1182        insta::assert_snapshot!(p("1.5µsecs"), @"PT0.0000015S");
1183
1184        insta::assert_snapshot!(p("1h 1.5mins"), @"PT1H1M30S");
1185        insta::assert_snapshot!(p("1m 1.5secs"), @"PT1M1.5S");
1186        insta::assert_snapshot!(p("1s 1.5msecs"), @"PT1.0015S");
1187        insta::assert_snapshot!(p("1ms 1.5µsecs"), @"PT0.0010015S");
1188
1189        insta::assert_snapshot!(p("1s2000ms"), @"PT3S");
1190    }
1191
1192    #[test]
1193    fn parse_unsigned_duration_boundaries() {
1194        let p = |s: &str| {
1195            let dur = SpanParser::new().parse_unsigned_duration(s).unwrap();
1196            crate::fmt::temporal::SpanPrinter::new()
1197                .unsigned_duration_to_string(&dur)
1198        };
1199        let pe = |s: &str| SpanParser::new().parse_duration(s).unwrap_err();
1200
1201        insta::assert_snapshot!(p("175307616 hours"), @"PT175307616H");
1202        insta::assert_snapshot!(p("10518456960 minutes"), @"PT175307616H");
1203        insta::assert_snapshot!(p("631107417600 seconds"), @"PT175307616H");
1204        insta::assert_snapshot!(p("631107417600000 milliseconds"), @"PT175307616H");
1205        insta::assert_snapshot!(p("631107417600000000 microseconds"), @"PT175307616H");
1206        insta::assert_snapshot!(p("9223372036854775807 nanoseconds"), @"PT2562047H47M16.854775807S");
1207
1208        insta::assert_snapshot!(p("175307617 hours"), @"PT175307617H");
1209        insta::assert_snapshot!(p("10518456961 minutes"), @"PT175307616H1M");
1210        insta::assert_snapshot!(p("631107417601 seconds"), @"PT175307616H1S");
1211        insta::assert_snapshot!(p("631107417600001 milliseconds"), @"PT175307616H0.001S");
1212        insta::assert_snapshot!(p("631107417600000001 microseconds"), @"PT175307616H0.000001S");
1213
1214        // The above were copied from the corresponding `Span` test, which has
1215        // tighter limits on components. But a `std::time::Duration` supports
1216        // the full range of `u64` seconds.
1217        insta::assert_snapshot!(p("5124095576030431hours"), @"PT5124095576030431H");
1218        insta::assert_snapshot!(
1219            pe("5124095576030432hrs"),
1220            @r#"failed to parse input in the "friendly" duration format: accumulated duration overflowed when adding value to unit hour"#,
1221        );
1222
1223        insta::assert_snapshot!(p("307445734561825860minutes"), @"PT5124095576030431H");
1224        insta::assert_snapshot!(
1225            pe("307445734561825861mins"),
1226            @r#"failed to parse input in the "friendly" duration format: accumulated duration overflowed when adding value to unit minute"#,
1227        );
1228
1229        insta::assert_snapshot!(p("18446744073709551615seconds"), @"PT5124095576030431H15S");
1230        insta::assert_snapshot!(
1231            pe("18446744073709551616s"),
1232            @r#"failed to parse input in the "friendly" duration format: number too big to parse into 64-bit integer"#,
1233        );
1234    }
1235
1236    #[test]
1237    fn err_unsigned_duration_basic() {
1238        let p = |s: &str| {
1239            SpanParser::new().parse_unsigned_duration(s).unwrap_err()
1240        };
1241
1242        insta::assert_snapshot!(
1243            p(""),
1244            @r#"failed to parse input in the "friendly" duration format: an empty string is not valid"#,
1245        );
1246        insta::assert_snapshot!(
1247            p(" "),
1248            @r#"failed to parse input in the "friendly" duration format: expected duration to start with a unit value (a decimal integer) after an optional sign, but no integer was found"#,
1249        );
1250        insta::assert_snapshot!(
1251            p("5"),
1252            @r#"failed to parse input in the "friendly" duration format: expected to find unit designator suffix (e.g., `years` or `secs`) after parsing integer"#,
1253        );
1254        insta::assert_snapshot!(
1255            p("a"),
1256            @r#"failed to parse input in the "friendly" duration format: expected duration to start with a unit value (a decimal integer) after an optional sign, but no integer was found"#,
1257        );
1258        insta::assert_snapshot!(
1259            p("2 minutes 1 hour"),
1260            @r#"failed to parse input in the "friendly" duration format: found value with unit hour after unit minute, but units must be written from largest to smallest (and they can't be repeated)"#,
1261        );
1262        insta::assert_snapshot!(
1263            p("1 hour 1 minut"),
1264            @r#"failed to parse input in the "friendly" duration format: parsed value '3660s', but unparsed input "ut" remains (expected no unparsed input)"#,
1265        );
1266        insta::assert_snapshot!(
1267            p("2 minutes,"),
1268            @r#"failed to parse input in the "friendly" duration format: expected whitespace after comma, but found end of input"#,
1269        );
1270        insta::assert_snapshot!(
1271            p("2 minutes, "),
1272            @r#"failed to parse input in the "friendly" duration format: found comma at the end of duration, but a comma indicates at least one more unit follows"#,
1273        );
1274        insta::assert_snapshot!(
1275            p("2 minutes ,"),
1276            @r#"failed to parse input in the "friendly" duration format: parsed value '120s', but unparsed input "," remains (expected no unparsed input)"#,
1277        );
1278    }
1279
1280    #[test]
1281    fn err_unsigned_duration_sign() {
1282        let p = |s: &str| {
1283            SpanParser::new().parse_unsigned_duration(s).unwrap_err()
1284        };
1285
1286        insta::assert_snapshot!(
1287            p("1hago"),
1288            @r#"failed to parse input in the "friendly" duration format: parsed value '3600s', but unparsed input "ago" remains (expected no unparsed input)"#,
1289        );
1290        insta::assert_snapshot!(
1291            p("1 hour 1 minuteago"),
1292            @r#"failed to parse input in the "friendly" duration format: parsed value '3660s', but unparsed input "ago" remains (expected no unparsed input)"#,
1293        );
1294        insta::assert_snapshot!(
1295            p("+1 hour 1 minute ago"),
1296            @r#"failed to parse input in the "friendly" duration format: expected to find either a prefix sign (+/-) or a suffix sign (`ago`), but found both"#,
1297        );
1298        insta::assert_snapshot!(
1299            p("-1 hour 1 minute ago"),
1300            @r#"failed to parse input in the "friendly" duration format: expected to find either a prefix sign (+/-) or a suffix sign (`ago`), but found both"#,
1301        );
1302    }
1303
1304    #[test]
1305    fn err_unsigned_duration_overflow_fraction() {
1306        let p = |s: &str| {
1307            let dur = SpanParser::new().parse_unsigned_duration(s).unwrap();
1308            crate::fmt::temporal::SpanPrinter::new()
1309                .unsigned_duration_to_string(&dur)
1310        };
1311        let pe = |s: &str| {
1312            SpanParser::new().parse_unsigned_duration(s).unwrap_err()
1313        };
1314
1315        insta::assert_snapshot!(
1316            // Unlike `Span`, this just overflows because it can't be parsed
1317            // as a 64-bit integer.
1318            pe("18446744073709551616 micros"),
1319            @r#"failed to parse input in the "friendly" duration format: number too big to parse into 64-bit integer"#,
1320        );
1321        // one fewer is okay
1322        insta::assert_snapshot!(
1323            p("18446744073709551615 micros"),
1324            @"PT5124095576H1M49.551615S"
1325        );
1326    }
1327
1328    #[test]
1329    fn err_unsigned_duration_fraction() {
1330        let p = |s: &str| {
1331            SpanParser::new().parse_unsigned_duration(s).unwrap_err()
1332        };
1333
1334        insta::assert_snapshot!(
1335            p("1.5 nanos"),
1336            @r#"failed to parse input in the "friendly" duration format: fractional nanoseconds are not supported"#,
1337        );
1338    }
1339
1340    #[test]
1341    fn err_unsigned_duration_hms() {
1342        let p = |s: &str| {
1343            SpanParser::new().parse_unsigned_duration(s).unwrap_err()
1344        };
1345
1346        insta::assert_snapshot!(
1347            p("05:"),
1348            @r#"failed to parse input in the "friendly" duration format: when parsing the `HH:MM:SS` format, expected to parse minute following hour"#,
1349        );
1350        insta::assert_snapshot!(
1351            p("05:06"),
1352            @r#"failed to parse input in the "friendly" duration format: when parsing the `HH:MM:SS` format, expected to parse `:` following minute"#,
1353        );
1354        insta::assert_snapshot!(
1355            p("05:06:"),
1356            @r#"failed to parse input in the "friendly" duration format: when parsing the `HH:MM:SS` format, expected to parse second following minute"#,
1357        );
1358        insta::assert_snapshot!(
1359            p("2 hours, 05:06:07"),
1360            @r#"failed to parse input in the "friendly" duration format: found `HH:MM:SS` after unit hour, but `HH:MM:SS` can only appear after years, months, weeks or days"#,
1361        );
1362    }
1363}