ingraph 0.1.0

A declaration-driven indexing framework: records and nodes are written as Rust types, and the storage rows, the dataflow between them and the GraphQL query surface are emitted from the declaration.
Documentation
use super::*;

/// The grammar reads what `mediatime` writes, and a client's own padding beside it.
#[test]
fn a_clock_names_the_position_its_parts_add_up_to() {
  for (spelling, expected) in [
    ("0:00:00.000", 0),
    ("0:00:00", 0),
    ("0:00:00.137", 137_000_000),
    ("00:01:30.5", 90_500_000_000),
    ("0:00:01.000000001", 1_000_000_001),
    ("1:00:00", 3_600_000_000_000),
    (
      "123:45:06.789",
      (123 * 3600 + 45 * 60 + 6) * 1_000_000_000 + 789_000_000,
    ),
    ("-0:00:01.500", -1_500_000_000),
    ("-1:00:00", -3_600_000_000_000),
  ] {
    assert_eq!(nanos(spelling), Ok(expected), "{spelling}");
  }
}

/// The FRACTION is a count of nanoseconds padded out, not a decimal divided down — so `.5` is half a
/// second and `.000000005` is five nanoseconds, and neither is the other scaled by a power of ten.
#[test]
fn the_fraction_is_padded_rather_than_divided() {
  assert_eq!(nanos("0:00:00.5"), Ok(500_000_000));
  assert_eq!(nanos("0:00:00.05"), Ok(50_000_000));
  assert_eq!(nanos("0:00:00.000000005"), Ok(5));
  assert_eq!(nanos("0:00:00.500000000"), nanos("0:00:00.5"));
}

/// Each refusal names the PART that broke, which is the only half of the answer that tells a client
/// what to send instead.
#[test]
fn each_refusal_names_the_rule_it_is_about() {
  for (spelling, expected) in [
    ("", ParseClockError::Shape),
    ("90", ParseClockError::Shape),
    ("00:90", ParseClockError::Shape),
    (":00:00", ParseClockError::Hours),
    ("a:00:00", ParseClockError::Hours),
    // Two digits exactly, and below sixty: a `1:2:3` admitted here would be one string with two
    // readings, since it is also a badly padded `1:02:03`.
    ("0:1:00", ParseClockError::Minutes),
    ("0:001:00", ParseClockError::Minutes),
    ("0:60:00", ParseClockError::Minutes),
    ("0:00:1", ParseClockError::Seconds),
    ("0:00:60", ParseClockError::Seconds),
    ("0:00:00.", ParseClockError::Fraction),
    ("0:00:00.1234567890", ParseClockError::Fraction),
    ("0:00:00.abc", ParseClockError::Fraction),
    // The magnitude, not the shape: the hours are well-formed digits and the position is past
    // ±292 years.
    ("2562048:00:00", ParseClockError::Overflow),
    ("-2562048:00:00", ParseClockError::Overflow),
  ] {
    assert_eq!(nanos(spelling), Err(expected), "{spelling:?}");
  }
}

/// ASCII digits and nothing else. A fullwidth `0` is a decimal digit to Unicode and would give one
/// position a second spelling for no gain.
#[test]
fn only_ascii_digits_are_digits() {
  assert_eq!(nanos("0:00:00"), Err(ParseClockError::Seconds));
  assert_eq!(nanos("٠:00:00"), Err(ParseClockError::Hours));
}

/// The two extremes are inside the grammar rather than outside it, which is what makes the overflow
/// refusal a statement about `i64` and not about the parser.
#[test]
fn the_widest_positions_still_parse() {
  assert_eq!(nanos("2562047:47:16.854775807"), Ok(i64::MAX));
  assert_eq!(nanos("-2562047:47:16.854775808"), Ok(i64::MIN));
}

/// The rendering is this module's own inverse at NANOSECOND resolution, which `mediatime`'s clock is
/// not — it truncates at milliseconds and has none.
#[test]
fn a_rendered_clock_reads_back_as_the_position_it_came_from() {
  for position in [
    0,
    1,
    -1,
    137_000_000,
    90_500_000_000,
    -1_500_000_000,
    i64::MAX,
    i64::MIN,
  ] {
    let rendered = render(position);

    assert_eq!(nanos(rendered.as_str()), Ok(position), "{rendered}");
  }
}

/// The rendered shape is the one the grammar states, padding included — asserted as text, because
/// the round trip above would pass for any pair of mutually inverse functions.
#[test]
fn the_rendering_pads_the_way_the_grammar_reads() {
  assert_eq!(render(0).as_str(), "0:00:00.000000000");
  assert_eq!(render(90_500_000_000).as_str(), "0:01:30.500000000");
  assert_eq!(render(-1_500_000_000).as_str(), "-0:00:01.500000000");
  assert_eq!(render(i64::MIN).as_str(), "-2562047:47:16.854775808");
}

/// Every rendering a real instant produces stays inside the inline representation, so the carrier
/// law costs no allocation at the one place this module builds text.
#[test]
fn a_rendered_clock_stays_inside_the_inline_representation() {
  for position in [0, 90_500_000_000, -1_500_000_000, i64::MAX, i64::MIN] {
    let rendered = render(position);

    assert!(
      rendered.is_inline(),
      "`{rendered}` ({} bytes) spilled to the heap",
      rendered.len()
    );
  }
}