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
//! The crossing's own assertions, and the one that matters most is a TYPE rather than a value: an
//! order that crosses totally says so at [`Infallible`](core::convert::Infallible), which is a proof
//! rather than a habit of returning `Ok`.

use core::convert::Infallible;

use crate::{FromGraphqlOrderBy, OrderDirection, TryFromGraphqlOrderBy};

/// A record's completed wire order: what the client wrote, with the key tail already appended.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ShotOrderBy {
  At(OrderDirection),
  Id(OrderDirection),
}

/// The same order lowered — one entry per LEVEL, which for the citizen is four and for the key is
/// one. Spelled as a plain list here because what the crossing is asked to prove is the SHAPE of the
/// step, not any dialect's rendering of it.
#[derive(Debug, Clone, PartialEq, Eq)]
struct ShotSqliteOrder(Vec<&'static str>);

impl FromGraphqlOrderBy<Vec<ShotOrderBy>> for ShotSqliteOrder {
  fn from_graphql_order_by(order: Vec<ShotOrderBy>) -> Self {
    let mut levels = Vec::new();
    for term in order {
      match term {
        // ONE wire word, FOUR levels — the 乙 expansion, in the sequence the citizen means.
        ShotOrderBy::At(_) => {
          levels.extend(["at_nanos", "at_pts", "at_timebase_num", "at_timebase_den"])
        }
        ShotOrderBy::Id(_) => levels.push("id"),
      }
    }
    Self(levels)
  }
}

#[test]
fn one_citizen_word_crosses_into_its_level_sequence() {
  let crossed = ShotSqliteOrder::from_graphql_order_by(vec![
    ShotOrderBy::At(OrderDirection::Asc),
    ShotOrderBy::Id(OrderDirection::Asc),
  ]);
  assert_eq!(
    crossed.0,
    [
      "at_nanos",
      "at_pts",
      "at_timebase_num",
      "at_timebase_den",
      "id"
    ],
    "the wire word count is the citizen's; the level count is the storage's"
  );
}

/// The blanket, reached rather than written: an order implementing only the infallible half answers
/// the fallible one too.
#[test]
fn the_infallible_half_lifts_into_the_fallible_one() {
  let crossed =
    ShotSqliteOrder::try_from_graphql_order_by(vec![ShotOrderBy::Id(OrderDirection::Desc)])
      .expect("a total crossing cannot fail");
  assert_eq!(crossed.0, ["id"]);
}

/// **The type-level pin.** A crossing written as [`FromGraphqlOrderBy`] carries
/// [`Infallible`](core::convert::Infallible) as its error, so a caller that matched on the failure
/// arm would be matching on an uninhabited type — which is what *provably infallible* means here and
/// what a hand-written `Ok(…)` could not say.
///
/// A `const` assertion rather than a runtime one: it is a fact about the impl, and a fact about an
/// impl should be checked where impls are.
const _: () = {
  const fn assert_infallible<S, O>()
  where
    S: TryFromGraphqlOrderBy<O, Error = Infallible>,
  {
  }
  assert_infallible::<ShotSqliteOrder, Vec<ShotOrderBy>>();
};