Skip to main content

ingraph_codegen/
duration.rs

1//! The duration grammar at EXPANSION time — `every = "10m"` read into the value the emission
2//! spells.
3//!
4//! # ONE grammar, two crates, and why the copy is here rather than borrowed
5//!
6//! The user's ruling on the duration word is that both sides speak ONE grammar: the runtime filter's
7//! `DurationFilter::parse_operand` and this expansion-time reader accept exactly the same strings
8//! and answer with exactly the same values. `ingraph` cannot lend this crate its copy — `ingraph`
9//! expands THROUGH the derive, which is built on this crate, so the edge would run the wrong way —
10//! so the table is copied and PINNED: `ingraph-derive`'s `duration_parity` suite reads both files
11//! and refuses a byte of drift below the first constant, then reads one corpus through both entries
12//! and refuses a difference in either the value or the acceptance.
13//!
14//! # The two grammars inside the one
15//!
16//! [`jiff::SignedDuration`]'s own `FromStr` runs first — ISO 8601 (`PT1H30M30S`), jiff's friendly
17//! form (`1h 30m 30s`), a leading sign on either — and only its refusal reaches [`wide`], the
18//! normalization table that answers the four CALENDAR units jiff declines for this type. jiff first
19//! settles the overlap (its reading wins every string it accepts) and settles the ERROR (a string
20//! neither grammar takes reports jiff's refusal, which names the unit it found and where).
21//!
22//! **The month and the year are fixed averages** — 30.44 and 365.25 days — kept as a documented
23//! convenience by the user's own word on the months exception, and not calendar arithmetic. `1M` is
24//! a MONTH and `1m` is a minute.
25
26use jiff::SignedDuration;
27
28mod wide;
29
30/// One duration from text, in the grammar both crates speak.
31///
32/// The expansion-time twin of `ingraph`'s `DurationFilter::parse_operand`, character for character
33/// in what it accepts and in what it answers — see the module note for the pin that keeps it so.
34///
35/// # Errors
36///
37/// jiff's own refusal, which names the unit it found and where. The wide table makes no message of
38/// its own: the caller already holds one that is more specific, and a second would only have to be
39/// chosen between. A value the grammar reads but `SignedDuration` cannot hold — `292471208677y` —
40/// refuses at the CROSSING instead, since *the grammar was fine and the value does not fit* is the
41/// more useful answer.
42pub fn parse(text: &str) -> Result<SignedDuration, jiff::Error> {
43  match text.parse() {
44    Ok(operand) => Ok(operand),
45    Err(refusal) => match wide::parse(text) {
46      Some(wide) => SignedDuration::try_from(wide),
47      None => Err(refusal),
48    },
49  }
50}
51
52#[cfg(test)]
53mod tests;