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
//! What the two RATIONAL citizens share: the reduction, and the affix pattern their render facets
//! match against.
//!
//! [`Timebase`](super::Timebase) and [`Rate`](super::Rate) are reciprocal readings of one shape —
//! two `i32` columns holding a fraction — so their storage law, their render facet and the alphabet
//! that facet admits are one set of rules rather than two that happen to agree. This module is where
//! the parts of that with a body live; the parts with only a NAME stay on each type, because a
//! timebase's numerator and a rate's are different questions with the same arithmetic.
//!
//! What is NOT shared is deliberate. Each citizen keeps its own crossing error, its own roster door
//! and its own GraphQL names, because those are the things a client reads and a client is asking
//! about one of them specifically. Sharing a `%` refusal is sharing an implementation; sharing an
//! error type would be telling a client that a rate and a timebase are the same thing.

#[cfg(feature = "sqlx")]
use smol_bytes::{Utf8Bytes, Utf8BytesMut};

#[cfg(test)]
mod tests;

/// Euclid, over a non-negative `a` and a positive `b`.
///
/// The result is POSITIVE for every such pair — including `a = 0`, where the first iteration returns
/// `b` — which is what makes both citizens' two divisions total.
///
/// `mediatime`'s constructors pin `num >= 0` and `den > 0` on both types, so there is no sign
/// handling here and none is missing.
pub(super) const fn gcd(mut a: i32, mut b: i32) -> i32 {
  while b != 0 {
    let remainder = a % b;
    a = b;
    b = remainder;
  }

  a
}

/// Which end of the rendering an affix operand is anchored to.
///
/// `sqlx`'s, with [`pattern`] below it: an affix is a `LIKE` pattern and nothing else in this crate
/// builds one. [`gcd`] above stays ungated because both citizens' `reduced` call it, which is a
/// domain answer rather than a lowering.
#[cfg(feature = "sqlx")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Affix {
  /// The rendering must BEGIN with the operand.
  Prefix,
  /// The rendering must END with it.
  Suffix,
  /// The rendering must hold it anywhere.
  Contains,
}

#[cfg(feature = "sqlx")]
/// One affix operand as a `LIKE` pattern, or the first character that disqualified it.
///
/// # The alphabet check is what removes the `ESCAPE` clause
///
/// A `num/den` rendering is two decimal integers and a slash, so its alphabet is `[0-9/]` and
/// `%`, `_` and `\` cannot occur in one. Refusing them here means no `LIKE` metacharacter can ever
/// reach a pattern, which is what lets both lowerings render a bare `LIKE ?` — the `ESCAPE` literal
/// being one of the few places the three dialects genuinely differ (`'\'` on Postgres, `'\\'` on
/// MySQL).
///
/// The refusal narrows nothing: an operand holding a character the rendering cannot produce names no
/// row either way. What it buys over silently matching nothing is that a client hears why.
///
/// It returns the CHARACTER rather than an error, because the error belongs to whichever citizen was
/// asked — a client filtering rates should not be told about timebases.
///
/// The wildcards are appended AFTER the check, so the two `%` this function writes are the only ones
/// a pattern can hold. The pattern is built INTO the carrier: capacity is exact — the operand plus at
/// most two wildcards — and `Utf8BytesMut` holds 23 bytes inline, which every pattern over this
/// alphabet that a real value could match is inside.
///
/// # Errors
///
/// The first character outside `[0-9/]`.
pub(super) fn pattern(operand: Option<&str>, affix: Affix) -> Result<Option<Utf8Bytes>, char> {
  let Some(operand) = operand else {
    return Ok(None);
  };

  if let Some(outside) = operand
    .chars()
    .find(|character| !character.is_ascii_digit() && *character != '/')
  {
    return Err(outside);
  }

  let mut pattern = Utf8BytesMut::with_capacity(operand.len() + 2);
  if matches!(affix, Affix::Suffix | Affix::Contains) {
    pattern.push('%');
  }
  pattern.push_str(operand);
  if matches!(affix, Affix::Prefix | Affix::Contains) {
    pattern.push('%');
  }

  Ok(Some(pattern.freeze_shared()))
}